setup-serial.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. DEVDIR = "/dev"
  10. ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyUSB', f)]
  11. ret = system("rm -f /dev/ttyPIU /dev/ttyGPRS /dev/ttyDIU /dev/ttyGPS")
  12. for devfn in ttyfn:
  13. print devfn
  14. ser = serial.Serial(join(DEVDIR,devfn), 115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  15. ser.write("\rAT\r")
  16. ser.flush()
  17. x0 = ser.readline()
  18. print "at n (0):", x0
  19. x1 = ser.readline()
  20. print "at n (1):", x1
  21. x2 = ser.readline()
  22. print "at n (2):", x2
  23. time.sleep(1)
  24. x0 = x0.strip()
  25. x1 = x1.strip()
  26. x2 = x2.strip()
  27. if (x0 == "ok" or x0 == "OK" or
  28. x1 == "ok" or x1 == "OK" or
  29. x2 == "ok" or x2 == "OK"):
  30. src_tty = join(DEVDIR,devfn)
  31. dst_tty = join(DEVDIR,"ttyGPRS")
  32. #print join(DEVDIR,devfn), "->", join(DEVDIR,"ttyGPRS")
  33. print src_tty, "->", dst_tty
  34. system("ln -s " + src_tty + " " + dst_tty)
  35. if (x0[0:4] == "/?: " or
  36. x1[0:4] == "/?: " or
  37. x2[0:4] == "/?: "):
  38. src_tty = join(DEVDIR,devfn)
  39. dst_tty = join(DEVDIR,"ttyPIU")
  40. #print join(DEVDIR,devfn), "->", join(DEVDIR,"ttyPIU")
  41. print src_tty, "->", dst_tty
  42. system("ln -s " + src_tty + " " + dst_tty)