setup-serial.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyACM', f)]
  15. for devfn in ttyfn:
  16. if DEBUG: print devfn
  17. ser = serial.Serial(join(DEVDIR,devfn), 9600, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  18. x0 = ser.readline()
  19. if DEBUG: print "at n (0):", x0
  20. x1 = ser.readline()
  21. if DEBUG: print "at n (1):", x1
  22. x2 = ser.readline()
  23. if DEBUG: print "at n (2):", x2
  24. time.sleep(1)
  25. if (re.match(r'^\$GP', x0) or
  26. re.match(r'^\$GP', x1) or
  27. re.match(r'^\$GP', x2)):
  28. src_tty = join(DEVDIR,devfn)
  29. dst_tty = join(DEVDIR,"ttyGPS")
  30. if VERBOSE:
  31. print src_tty, "->", dst_tty
  32. system("ln -s " + src_tty + " " + dst_tty)
  33. ser.close()
  34. ###################
  35. ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyUSB', f)]
  36. for devfn in ttyfn:
  37. if DEBUG: print devfn
  38. ser = serial.Serial(join(DEVDIR,devfn), 115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  39. ser.write("\rAT\r")
  40. ser.flush()
  41. x0 = ser.readline()
  42. if DEBUG: print "at n (0):", x0
  43. x1 = ser.readline()
  44. if DEBUG: print "at n (1):", x1
  45. x2 = ser.readline()
  46. if DEBUG: print "at n (2):", x2
  47. time.sleep(1)
  48. x0 = x0.strip()
  49. x1 = x1.strip()
  50. x2 = x2.strip()
  51. if (x0 == "ok" or x0 == "OK" or
  52. x1 == "ok" or x1 == "OK" or
  53. x2 == "ok" or x2 == "OK"):
  54. src_tty = join(DEVDIR,devfn)
  55. dst_tty = join(DEVDIR,"ttyGPRS")
  56. if VERBOSE:
  57. print src_tty, "->", dst_tty
  58. system("ln -s " + src_tty + " " + dst_tty)
  59. if (x0[0:4] == "/?: " or
  60. x1[0:4] == "/?: " or
  61. x2[0:4] == "/?: "):
  62. src_tty = join(DEVDIR,devfn)
  63. dst_tty = join(DEVDIR,"ttyPIU")
  64. if VERBOSE:
  65. print src_tty, "->", dst_tty
  66. system("ln -s " + src_tty + " " + dst_tty)
  67. ser.close()