setup-serial.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python
  2. import serial
  3. import sys
  4. import re
  5. import time
  6. from os import listdir
  7. from os.path import isfile, join
  8. from os import system
  9. DEBUG = False
  10. VERBOSE = True
  11. ret = system("rm -f /dev/ttyPIU /dev/ttyGPRS /dev/ttyDIU /dev/ttyGPS")
  12. DEVDIR = "/dev"
  13. ###################
  14. # sniff for our GPS
  15. #
  16. ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyACM', f)]
  17. for devfn in ttyfn:
  18. if DEBUG: print devfn
  19. ser = serial.Serial(join(DEVDIR,devfn), 9600, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  20. x0 = ser.readline()
  21. if DEBUG: print "at n (0):", x0
  22. x1 = ser.readline()
  23. if DEBUG: print "at n (1):", x1
  24. x2 = ser.readline()
  25. if DEBUG: print "at n (2):", x2
  26. time.sleep(1)
  27. if (re.match(r'^\$GP', x0) or
  28. re.match(r'^\$GP', x1) or
  29. re.match(r'^\$GP', x2)):
  30. src_tty = join(DEVDIR,devfn)
  31. dst_tty = join(DEVDIR,"ttyGPS")
  32. if VERBOSE:
  33. print src_tty, "->", dst_tty
  34. system("ln -s " + src_tty + " " + dst_tty)
  35. ser.close()
  36. ###################
  37. ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyUSB', f)]
  38. # sniff for GPRS and PIU
  39. #
  40. for devfn in ttyfn:
  41. if DEBUG: print devfn
  42. ser = serial.Serial(join(DEVDIR,devfn), 115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  43. ser.write("\rAT\r")
  44. ser.flush()
  45. x0 = ser.readline()
  46. if DEBUG: print "at n (0):", x0
  47. x1 = ser.readline()
  48. if DEBUG: print "at n (1):", x1
  49. x2 = ser.readline()
  50. if DEBUG: print "at n (2):", x2
  51. time.sleep(1)
  52. x0 = x0.strip()
  53. x1 = x1.strip()
  54. x2 = x2.strip()
  55. if (x0 == "ok" or x0 == "OK" or
  56. x1 == "ok" or x1 == "OK" or
  57. x2 == "ok" or x2 == "OK"):
  58. src_tty = join(DEVDIR,devfn)
  59. dst_tty = join(DEVDIR,"ttyGPRS")
  60. if VERBOSE:
  61. print src_tty, "->", dst_tty
  62. system("ln -s " + src_tty + " " + dst_tty)
  63. if (x0[0:4] == "/?: " or
  64. x1[0:4] == "/?: " or
  65. x2[0:4] == "/?: "):
  66. src_tty = join(DEVDIR,devfn)
  67. dst_tty = join(DEVDIR,"ttyPIU")
  68. if VERBOSE:
  69. print src_tty, "->", dst_tty
  70. system("ln -s " + src_tty + " " + dst_tty)
  71. ser.close()