setup-serial.py 905 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. DEVDIR = "/dev"
  9. ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyUSB', f)]
  10. for devfn in ttyfn:
  11. print devfn
  12. ser = serial.Serial(join(DEVDIR,devfn), 115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  13. ser.write("\rAT\r")
  14. ser.flush()
  15. x0 = ser.readline()
  16. print "at n (0):", x0
  17. x1 = ser.readline()
  18. print "at n (1):", x1
  19. x2 = ser.readline()
  20. print "at n (2):", x2
  21. time.sleep(1)
  22. x0 = x0.strip()
  23. x1 = x1.strip()
  24. x2 = x2.strip()
  25. if (x0 == "ok" or x0 == "OK" or
  26. x1 == "ok" or x1 == "OK" or
  27. x2 == "ok" or x2 == "OK"):
  28. print join(DEVDIR,devfn), "->", join(DEVDIR,"ttyGPRS")
  29. if (x0[0:4] == "/?: " or
  30. x1[0:4] == "/?: " or
  31. x2[0:4] == "/?: "):
  32. print join(DEVDIR,devfn), "->", join(DEVDIR,"ttyPIU")