From 477bdb98f8a3e8250de80e1c88b4cab85a82e7ab Mon Sep 17 00:00:00 2001 From: Nash Kaminski Date: Sat, 28 Oct 2017 22:19:13 -0500 Subject: [PATCH 1/4] Implement bi-directional DiagnosticPort_Formatter --- stratasys/formatter.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/stratasys/formatter.py b/stratasys/formatter.py index fe186bb..5a67942 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,27 @@ 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 = "" - - # TODO - implements me + 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 def to_destination(self, data): - formatted = "" + formatted = "\"" for b in data: - formatted += binascii.hexlify(b) + ", " + formatted += binascii.hexlify(b) + " " - return formatted[0:-2] + return formatted[0:-1] + "\"" From 8e8c957ed8886f39a26e1f71e594985b8c0e6166 Mon Sep 17 00:00:00 2001 From: Nash Kaminski Date: Sat, 28 Oct 2017 22:23:37 -0500 Subject: [PATCH 2/4] Document changes via comments --- stratasys/formatter.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stratasys/formatter.py b/stratasys/formatter.py index 5a67942..7906b53 100644 --- a/stratasys/formatter.py +++ b/stratasys/formatter.py @@ -18,6 +18,9 @@ class DiagnosticPort_Formatter(Formatter): def __init__(self): self.rx = re.compile('^[0-9]{6}: ((?:[0-9a-f-A-F]{2} ?)+).*?$',re.MULTILINE) +#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 + def from_source(self, data): formatted = b'' idx = 1 @@ -33,6 +36,7 @@ def from_source(self, data): 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 = "\"" From 4e2024ed38894bf8e02e8480aec2e44c92ea44de Mon Sep 17 00:00:00 2001 From: Nash Kaminski Date: Sat, 28 Oct 2017 22:52:15 -0500 Subject: [PATCH 3/4] Add -D command line option to produce input and output suitable for directly feeding to the uPrint 'er' and 'ew' diagnostic commands. --- stratasys-cli.py | 19 +++++++++++++------ stratasys/formatter.py | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) 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 7906b53..2e79121 100644 --- a/stratasys/formatter.py +++ b/stratasys/formatter.py @@ -41,6 +41,6 @@ def to_destination(self, data): formatted = "\"" for b in data: - formatted += binascii.hexlify(b) + " " + formatted += binascii.hexlify(chr(b)) + " " return formatted[0:-1] + "\"" From 91598f738fcf86c0b9a762301b6090f4cfe40160 Mon Sep 17 00:00:00 2001 From: Nash Kaminski Date: Sat, 28 Oct 2017 22:57:37 -0500 Subject: [PATCH 4/4] Document -D command line argument --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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