Advertisement
jcomeau_ictx

vx7read.py

Sep 24th, 2013
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import sys, os, serial, time
  3. DEV = '/dev'
  4. PORTS = [os.path.join(DEV, port) for port in os.listdir(DEV)
  5. if port.startswith('ttyUSB')]
  6. ACK = chr(6)
  7. DATASIZE = 16211
  8. def read(port, count, checksum = False):
  9. debug('attempting to read %d bytes from %s' % (count, port))
  10. data = port.read(count)
  11. debug('%d bytes of data read: %s' % (len(data), data[:40].encode('hex')))
  12. if not checksum:
  13. time.sleep(0.2) # delay before ACK
  14. port.write(ACK)
  15. ack = port.read(1)
  16. if not ack == ACK:
  17. print >>sys.stderr, '%s seen when ACK (%s) expected' % (repr(ack), repr(ACK))
  18. sys.exit(1)
  19. return data
  20. def write(filename, data):
  21. if filename is None:
  22. if sys.stdin.isatty():
  23. print data.encode('hex')
  24. else:
  25. sys.stdout.write(data)
  26. else:
  27. outfile = open(filename, 'wb')
  28. outfile.write(data)
  29. outfile.close()
  30. def debug(message):
  31. if __debug__:
  32. if message:
  33. print >>sys.stderr, message
  34. def comm(filename = None, port = None):
  35. port = serial.Serial(port or PORTS[0], baudrate = 19200,
  36. stopbits = 2, timeout = 20)
  37. port.flushInput()
  38. raw_input('''Instructions:
  39. 1) While holding MON-F, power on VX-7R
  40. 2) Hit <Enter> on computer keyboard
  41. 3) Within 30 seconds, hit BAND key on VX-7R ''')
  42. data = read(port, 10)
  43. data += read(port, 8)
  44. data += read(port, DATASIZE - 10 - 8, checksum = True)
  45. port.close()
  46. write(filename, data)
  47. if __name__ == '__main__':
  48. comm(*sys.argv[1:])
Advertisement
RAW Paste Data Copied
Advertisement