first version of morning edition in python
This commit is contained in:
88
readmsw.py
Normal file
88
readmsw.py
Normal file
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Read TM-T88V memory switch values back from the printer via USB.
|
||||
GS ( E fn=4 asks the printer to transmit its current memory switch settings.
|
||||
"""
|
||||
import sys, time, select
|
||||
|
||||
ESC = b'\x1b'
|
||||
GS = b'\x1d'
|
||||
DEV = "/dev/usb/lp0"
|
||||
|
||||
INIT = ESC + b'\x40'
|
||||
CUT = GS + b'\x56\x00'
|
||||
|
||||
def gs_e(fn: int, data: bytes = b'') -> bytes:
|
||||
payload = bytes([fn]) + data
|
||||
pL = len(payload) & 0xFF
|
||||
pH = (len(payload) >> 8) & 0xFF
|
||||
return GS + b'\x28\x45' + bytes([pL, pH]) + payload
|
||||
|
||||
# fn=4: Transmit memory switch (all groups)
|
||||
QUERY_MSW = gs_e(4, b'')
|
||||
# fn=6: Transmit customized values
|
||||
QUERY_CUSTOM = gs_e(6, b'')
|
||||
|
||||
try:
|
||||
f = open(DEV, "r+b", buffering=0)
|
||||
except PermissionError:
|
||||
print(f"Permission denied on {DEV} -- run: sudo chmod a+rw {DEV}")
|
||||
sys.exit(1)
|
||||
|
||||
print("Querying memory switches (fn=4)...")
|
||||
f.write(INIT + QUERY_MSW)
|
||||
f.flush()
|
||||
time.sleep(0.5)
|
||||
|
||||
# Try to read response
|
||||
response = b''
|
||||
deadline = time.time() + 2.0
|
||||
while time.time() < deadline:
|
||||
r, _, _ = select.select([f], [], [], 0.2)
|
||||
if r:
|
||||
chunk = f.read(64)
|
||||
if chunk:
|
||||
response += chunk
|
||||
else:
|
||||
if response:
|
||||
break
|
||||
|
||||
if response:
|
||||
print(f"Memory switch response ({len(response)} bytes):")
|
||||
print(" hex:", response.hex())
|
||||
print(" dec:", list(response))
|
||||
print()
|
||||
# GS ( E fn=4 response format: header + 8 bytes (one per MSW group)
|
||||
# Find the data bytes after any header
|
||||
for i, b in enumerate(response):
|
||||
print(f" byte[{i}] = 0x{b:02x} ({b:08b}b)")
|
||||
else:
|
||||
print("No response to fn=4 query.")
|
||||
|
||||
print()
|
||||
print("Querying customized values (fn=6)...")
|
||||
f.write(QUERY_CUSTOM)
|
||||
f.flush()
|
||||
time.sleep(0.5)
|
||||
|
||||
response2 = b''
|
||||
deadline = time.time() + 2.0
|
||||
while time.time() < deadline:
|
||||
r, _, _ = select.select([f], [], [], 0.2)
|
||||
if r:
|
||||
chunk = f.read(64)
|
||||
if chunk:
|
||||
response2 += chunk
|
||||
else:
|
||||
if response2:
|
||||
break
|
||||
|
||||
if response2:
|
||||
print(f"Customized value response ({len(response2)} bytes):")
|
||||
print(" hex:", response2.hex())
|
||||
for i, b in enumerate(response2):
|
||||
print(f" byte[{i}] = 0x{b:02x} ({b:08b}b)")
|
||||
else:
|
||||
print("No response to fn=6 query.")
|
||||
|
||||
f.close()
|
||||
Reference in New Issue
Block a user