-
Notifications
You must be signed in to change notification settings - Fork 1
/
zebra-bridge.py
executable file
·74 lines (57 loc) · 2.2 KB
/
zebra-bridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
import serial # pip3 install pyserial
import argparse
import logging as log
import socket
import signal
def main():
parser = argparse.ArgumentParser(
description='A simple program for redirecting ZPL print jobs from'
' a virtual COM port to a networked Zebra printer'
' over TCP.')
parser.add_argument('--port', '-p', type=int, default=9100,
help='TCP port the printer listens on'
' (default: %(default)d)')
parser.add_argument('COM',
help='the virtual serial port created by com0com')
parser.add_argument('host',
help="the printer's IP address")
global args
args = parser.parse_args()
log.basicConfig(level=log.INFO,
format='%(asctime)s %(levelname)-8s %(message)s')
ser = serial.Serial(args.COM)
log.info(f"Listening on {ser.name}")
received_data = bytearray("aaa", 'ascii')
sending = False
sock = None
signal.signal(signal.SIGINT, signal.default_int_handler)
try:
while True:
c = ser.read(1)
if sending:
sock.send(c) # TODO care about the return value ??
for i in range(2):
received_data[i] = received_data[i+1]
received_data[2] = ord(c)
if sending and received_data == bytearray("^XZ", 'ascii'):
# already sent ^XZ
log.info("Closing TCP connection")
sock.close()
sock = None
sending = False
if not sending and received_data == bytearray("^XA", 'ascii'):
sending = True
log.info("Starting TCP connection")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((args.host, args.port))
sock.send(b"^XA")
except KeyboardInterrupt:
# TODO ctrl+c keeps waiting for a character on the COM port?!
# Windows only??
log.info("Keyboard Interrupt")
ser.close()
if sock is not None:
sock.close()
if __name__ == '__main__':
main()