Port greenctld to python3

spacecruft
Jeff Moe 2022-10-01 13:35:48 -06:00
parent 7b4ccb4a69
commit 82d5091f7a
2 changed files with 37 additions and 31 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# vim:set ts=4 sw=4 sts=4 ai et smarttab:
# greenctld, a hamlib-compatible driver for the Green Heron Engineering RT-21
@ -33,16 +33,16 @@ class DummyRotor(object):
el = 0
def set_pos(self, az, el):
print '==> %d,%d' % (az, el)
print('==> %d,%d' % (az, el))
self.az = az
self.el = el
def get_pos(self):
print '<== %d,%d' % (self.az, self.el)
print('<== %d,%d' % (self.az, self.el))
return (self.az, self.el,)
def stop(self):
print "==> Stop"
print("==> Stop")
class GreenHeronRotor(object):
'''
@ -54,21 +54,21 @@ class GreenHeronRotor(object):
def __init__(self, az_device, el_device, baud, timeout):
self.az_serial = serial.Serial(az_device, baudrate=baud, timeout=timeout)
self.el_serial = serial.Serial(el_device, baudrate=baud, timeout=timeout)
print '--- Serial timeout set to', timeout
print('--- Serial timeout set to', timeout)
def stop(self):
print "==> Stop"
print("==> Stop")
self.az_serial.write(';')
self.el_serial.write(';')
def set_pos(self, az, el):
print '==> %d,%d' % (az, el)
print('==> %d,%d' % (az, el))
self.az_serial.write('AP1%03d\r;' % az)
self.el_serial.write('AP1%03d\r;' % el)
time.sleep(0.1)
def __parse_response(self, buf):
match = re.match(r'^([0-9][0-9][0-9]);$', buf)
match = re.match(r'^([0-9][0-9][0-9]);$', buf.decode())
if not match:
return -1
ret = match.groups()[0]
@ -83,24 +83,24 @@ class GreenHeronRotor(object):
self.az_serial.flushInput()
self.el_serial.flushInput()
self.az_serial.write('AI1;')
self.el_serial.write('AI1;')
self.az_serial.write(b'AI1;')
self.el_serial.write(b'AI1;')
az_buf = self.az_serial.read(4)
el_buf = self.el_serial.read(4)
if len(az_buf) != 4 or len(el_buf) != 4:
print '!!! Serial read failure, received %s and %s' % (repr(az_buf), repr(el_buf))
print('!!! Serial read failure, received %s and %s' % (repr(az_buf), repr(el_buf)))
return False
az = self.__parse_response(az_buf)
el = self.__parse_response(el_buf)
if az < 0 or el < 0:
print '!!! Failed to parse response, received %s and %s' % (repr(az_buf), repr(el_buf))
print('!!! Failed to parse response, received %s and %s' % (repr(az_buf), repr(el_buf)))
return False
print '<== %d,%d' % (az, el)
print('<== %d,%d' % (az, el))
return (az, el,)
class TCPServer(object):
@ -128,7 +128,7 @@ class TCPServer(object):
self.listener.bind((ip, port))
self.listener.listen(4)
addr = self.listener.getsockname()
print '--- Listening for connections on %s:%d' % (addr[0], addr[1])
print('--- Listening for connections on %s:%d' % (addr[0], addr[1]))
def close_client(self, fd):
self.rotor.stop()
@ -144,7 +144,12 @@ class TCPServer(object):
if cmd == '':
return
print '<-- %s' % repr(cmd)
# "\\dump_state" is sent by satnogs hamlib, need to ignore it and send okay state
if cmd == '\\dump_state':
fd.send('RPRT 0\n')
return
print('<-- %s' % repr(cmd))
# "q", to quit
if cmd == 'q':
@ -154,7 +159,7 @@ class TCPServer(object):
# "S", to stop the current rotation
if cmd == 'S':
self.rotor.stop()
print '--> RPRT 0'
print('--> RPRT 0')
fd.send('RPRT 0\n')
return
@ -162,11 +167,11 @@ class TCPServer(object):
if cmd == 'p':
pos = self.rotor.get_pos()
if not pos:
print '--> RPRT -6'
print('--> RPRT -6')
fd.send('RPRT -6\n')
else:
az, el = pos
print '--> %d,%d' % (az, el)
print('--> %d,%d' % (az, el))
fd.send('%.6f\n%.6f\n' % (az, el))
return
@ -179,7 +184,7 @@ class TCPServer(object):
az = int(float(az))
el = int(float(el))
except:
print '--> RPRT -8 (could not parse)'
print('--> RPRT -8 (could not parse)')
fd.send('RPRT -8\n')
return
@ -187,29 +192,29 @@ class TCPServer(object):
az = 359
if az > 359:
print '--> RPRT -1 (az too large)'
print('--> RPRT -1 (az too large)')
fd.send('RPRT -1\n')
return
if el > 90:
print '--> RPRT -1 (el too large)'
print('--> RPRT -1 (el too large)')
fd.send('RPRT -1\n')
return
self.rotor.set_pos(az, el)
print '--> RPRT 0'
print('--> RPRT 0')
fd.send('RPRT 0\n')
return
# Nothing else is supported
print '--> RPRT -4 (unknown command)'
print('--> RPRT -4 (unknown command)')
fd.send('RPRT -4\n')
def read_client(self, fd):
buf = fd.recv(1024)
if len(buf) == 0:
print '<-- EOF'
print('<-- EOF')
self.close_client(fd)
return
@ -227,11 +232,11 @@ class TCPServer(object):
self.parse_client_command(fd, cmd)
# Check if the client sent a "q", to quit
if not self.client_buf.has_key(fd):
if fd not in self.client_buf:
return
def __run_once(self):
rlist = [ self.listener ] + self.client_buf.keys()
rlist = [ self.listener ] + list(self.client_buf.keys())
wlist = []
xlist = []
@ -244,24 +249,24 @@ class TCPServer(object):
new_fd.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024*16)
new_fd.setblocking(False)
self.client_buf[new_fd] = ''
print '<-- Connect %s:%d' % (addr[0], addr[1])
print('<-- Connect %s:%d' % (addr[0], addr[1]))
else:
try:
self.read_client(fd)
except Exception as e:
print 'Unhandled exception, killing client and issuing motor stop command:'
print('Unhandled exception, killing client and issuing motor stop command:')
traceback.print_exc()
self.close_client(fd)
print
print()
def loop(self):
while True:
self.__run_once()
if __name__ == '__main__':
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)
parser = argparse.ArgumentParser()
parser.add_argument('--az-device', '-a', type=str, required=True, help='Serial device for azimuth')
@ -279,7 +284,7 @@ if __name__ == '__main__':
rotor = GreenHeronRotor(args.az_device, args.el_device, args.speed, args.timeout)
if args.get_pos:
print rotor.get_pos()
print(rotor.get_pos())
sys.exit(0)
server = TCPServer(args.port, rotor)

1
requirements.txt 100644
View File

@ -0,0 +1 @@
pyserial