diff --git a/README.md b/README.md index 5dde9c9..689707e 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,14 @@ The EEPROM uid should end with '23'. You may have to reverse the byte order. Say If you provide the '-r' option, arguments to pass to stratasys-cli will be printed to help you recreate the cartridge. -The input file must be a binary file. +If you provide the '-D' option, the input file will be interpreted as an ASCII formatted file, +containing lines of the form produced by the printers 'er' command, namely: + +``` +000096: 00 00 00 00 00 00 00 00 53 54 52 41 54 41 53 59 ........STRATASY +``` + +Otherwise, the input file must be a binary file. ### Create your own cartridge @@ -50,7 +57,10 @@ The EEPROM uid use to end with '23'. You may have to reverse it. Say you have `233a38b1020000c0`, you should reverse it to be `c0000002b1383a23`. The generated file will be 113 bytes in size. You can complete the file with zeroes -if you want to make it 512 bytes long, the usual EEPROM size. +if you want to make it 512 bytes long, the usual EEPROM size.a + +Supplying the '-D' option will result in an output file containing a double-quoted string +of space delimited bytes, expressed in hexadecimal. Otherwise, the output will be a binary file. ### List supported material diff --git a/stratasys-cli.py b/stratasys-cli.py index 557ef9a..e3c17ec 100755 --- a/stratasys-cli.py +++ b/stratasys-cli.py @@ -16,11 +16,13 @@ from stratasys import manager from stratasys import crypto from stratasys import checksum +from stratasys.formatter import DiagnosticPort_Formatter from stratasys.setupcode import * class StratasysConsoleApp(): def __init__(self): self.argparse = self.build_argparser() + self.diag_formatter = DiagnosticPort_Formatter() def run(self): args = self.argparse.parse_args() @@ -41,6 +43,7 @@ def build_argparser(self): # Options used for both reading / writing eeprom eeprom_parser.add_argument("-t", "--machine-type", action="store", choices=["fox", "fox2", "prodigy", "quantum", "uprint", "uprintse"], help="Machine type (Fox T-class, Prodigy P-class, Quantum, uPrint, uPrint SE)", required=True) eeprom_parser.add_argument("-e", "--eeprom-uid", action="store", dest="eeprom_uid", required=True, help="Format: [a-f0-9]{14}23, example: 11010a01ba325d23") + eeprom_parser.add_argument("-D", "--diag-format", action="store_true", dest="diag_format", help="Read input/produce output in the ASCII format used over the printer diagnostic port") # Input or output options io_group = eeprom_parser.add_mutually_exclusive_group(required=True) @@ -196,15 +199,19 @@ def _eeprom_create(self, args): if args.use_ascii == True: eeprom = self._make_ascii(args, cart, eeprom, machine_number) else: mode += "b" - f = open(args.output_file, mode) - f.write(eeprom) - f.close() + with open(args.output_file, mode) as f: + if(args.diag_format and (not args.use_ascii)): + f.write(self.diag_formatter.to_destination(eeprom)) + else: + f.write(eeprom) return def _eeprom_info(self, args): - f = open(args.input_file, "rb") - cartridge_crypted = bytearray(f.read()) - f.close() + with open(args.input_file, "rb") as f: + if(args.diag_format): + cartridge_crypted = bytearray(self.diag_formatter.from_source(f.read())) + else: + cartridge_crypted = bytearray(f.read()) m = manager.Manager(crypto.Desx_Crypto(), checksum.Crc16_Checksum()) machine_number = machine.get_number_from_type(args.machine_type) diff --git a/stratasys/formatter.py b/stratasys/formatter.py index fe186bb..2e79121 100644 --- a/stratasys/formatter.py +++ b/stratasys/formatter.py @@ -1,6 +1,8 @@ # # See the LICENSE file # +import re +import binascii class Formatter: def __init__(self): @@ -14,19 +16,31 @@ def to_destination(self, data): class DiagnosticPort_Formatter(Formatter): def __init__(self): - pass + self.rx = re.compile('^[0-9]{6}: ((?:[0-9a-f-A-F]{2} ?)+).*?$',re.MULTILINE) - def from_source(self, data): - formatted = "" +#Reads a series of newline delimited lines of the format: +#000096: 00 00 00 00 00 00 00 00 53 54 52 41 54 41 53 59 ........STRATASY - # TODO - implements me + def from_source(self, data): + formatted = b'' + idx = 1 + for match in self.rx.finditer(data): + try: + #Get a line of data with whitespace removed + line = match.group(1).replace(' ', '') + formatted += binascii.unhexlify(line) + idx=idx+1 + except IndexError: + print("Error on line %s when reading diag port formatted data" % (idx,)) + raise return formatted +#Produces a double-quoted, space separated string suitable for providing to the uPrint's 'ew' diagnostic command def to_destination(self, data): - formatted = "" + formatted = "\"" for b in data: - formatted += binascii.hexlify(b) + ", " + formatted += binascii.hexlify(chr(b)) + " " - return formatted[0:-2] + return formatted[0:-1] + "\""