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