setup-serial.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. import serial
  21. import sys
  22. import re
  23. import time
  24. from os import listdir
  25. from os.path import isfile, join
  26. from os import system
  27. DEBUG = True
  28. VERBOSE = True
  29. ret = system("rm -f /dev/ttyPIU /dev/ttyGPRS /dev/ttyDIU /dev/ttyGPS")
  30. DEVDIR = "/dev"
  31. ###################
  32. # sniff for our GPS
  33. #
  34. ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyACM', f)]
  35. for devfn in ttyfn:
  36. if DEBUG: print devfn
  37. ser = serial.Serial(join(DEVDIR,devfn), 9600, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  38. x0 = ser.readline()
  39. if DEBUG: print "at n (0):", x0
  40. x1 = ser.readline()
  41. if DEBUG: print "at n (1):", x1
  42. x2 = ser.readline()
  43. if DEBUG: print "at n (2):", x2
  44. time.sleep(1)
  45. if (re.match(r'^\$GP', x0) or
  46. re.match(r'^\$GP', x1) or
  47. re.match(r'^\$GP', x2)):
  48. src_tty = join(DEVDIR,devfn)
  49. dst_tty = join(DEVDIR,"ttyGPS")
  50. if VERBOSE:
  51. print src_tty, "->", dst_tty
  52. system("ln -s " + src_tty + " " + dst_tty)
  53. ser.close()
  54. ###################
  55. ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyUSB', f)]
  56. # sniff for GPRS and PIU
  57. #
  58. for devfn in ttyfn:
  59. if DEBUG: print devfn
  60. ser = serial.Serial(join(DEVDIR,devfn), 115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  61. ser.write("\rAT\r")
  62. ser.flush()
  63. x0 = ser.readline()
  64. if DEBUG: print "at n (0):", x0
  65. x1 = ser.readline()
  66. if DEBUG: print "at n (1):", x1
  67. x2 = ser.readline()
  68. if DEBUG: print "at n (2):", x2
  69. time.sleep(1)
  70. x0 = x0.strip()
  71. x1 = x1.strip()
  72. x2 = x2.strip()
  73. if (x0 == "ok" or x0 == "OK" or
  74. x1 == "ok" or x1 == "OK" or
  75. x2 == "ok" or x2 == "OK"):
  76. src_tty = join(DEVDIR,devfn)
  77. dst_tty = join(DEVDIR,"ttyGPRS")
  78. if VERBOSE:
  79. print src_tty, "->", dst_tty
  80. system("ln -s " + src_tty + " " + dst_tty)
  81. if (x0[0:4] == "/?: " or
  82. x1[0:4] == "/?: " or
  83. x2[0:4] == "/?: "):
  84. src_tty = join(DEVDIR,devfn)
  85. dst_tty = join(DEVDIR,"ttyPIU")
  86. if VERBOSE:
  87. print src_tty, "->", dst_tty
  88. system("ln -s " + src_tty + " " + dst_tty)
  89. ser.close()