reset-gps.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 os
  21. import serial
  22. import sys
  23. import re
  24. import time
  25. from os import listdir
  26. from os.path import isfile, join
  27. from os import system
  28. GPS_BAUD = 115200
  29. FREQ_SECOND = 3
  30. BITMASK = 31
  31. DEVGPS = "/dev/ttyGPS"
  32. DEBUG = True
  33. VERBOSE = True
  34. ###
  35. ###
  36. ser = serial.Serial(DEVGPS, GPS_BAUD, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
  37. ###
  38. ###
  39. if DEBUG: print "send:", "AT+CGPS=1"
  40. ser.write("AT+CGPS=1\r")
  41. ser.flush()
  42. resp = ser.readline()
  43. while (len(resp) > 0) and (resp[0] == '$'):
  44. resp = ser.readline()
  45. if not re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  46. resp = ser.readline()
  47. while (len(resp) > 0) and (resp[0] == '$'):
  48. resp = ser.readline()
  49. if not re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  50. if DEBUG: print "got:", resp
  51. time.sleep(1)
  52. ###
  53. ###
  54. if DEBUG: print "send:", "AT+CGPSINFOCFG=" + str(FREQ_SECOND) + "," + str(BITMASK)
  55. ser.write("AT+CGPSINFOCFG=" + str(FREQ_SECOND) + "," + str(BITMASK) + "\r")
  56. ser.flush()
  57. resp = ser.readline()
  58. while (len(resp) > 0) and (resp[0] == '$'):
  59. resp = ser.readline()
  60. if not re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  61. resp = ser.readline()
  62. while (len(resp) > 0) and (resp[0] == '$'):
  63. resp = ser.readline()
  64. if not re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  65. if DEBUG: print "got:", resp
  66. time.sleep(1)
  67. if DEBUG: print "closing connection"
  68. ser.close()
  69. sys.exit(0);
  70. ###
  71. ###
  72. #if DEBUG: print "send:", "AT"
  73. #
  74. #ser.write("\rAT\r")
  75. #ser.flush()
  76. #
  77. #resp = ser.readline()
  78. #if not re.search(r'[aA][tT]', resp):
  79. # print "ERROR: got response: '" + str(resp) + "', expecting 'OK', exiting"
  80. # sys.exit(-1)
  81. #
  82. #resp = ser.readline()
  83. #if not re.search(r'[oO][kK]', resp):
  84. # print "ERROR: got response: '" + str(resp) + "', expecting 'OK', exiting"
  85. # sys.exit(-1)
  86. #
  87. #if DEBUG: print "got:", resp
  88. #
  89. ###
  90. ###
  91. if DEBUG: print "send:", "AT+CGPSINFOCFG=0"
  92. ser.write("AT+CGPSINFOCFG=0\r")
  93. ser.flush()
  94. resp = ser.readline()
  95. while (len(resp) > 0) and (resp[0] == '$'):
  96. resp = ser.readline()
  97. if not re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  98. resp = ser.readline()
  99. if not re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  100. if DEBUG: print "got:", resp
  101. time.sleep(1)
  102. ###
  103. ###
  104. if DEBUG: print "send:", "AT+CGPS=0"
  105. ser.write("AT+CGPS=0\r")
  106. ser.flush()
  107. resp = ser.readline()
  108. if not re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  109. resp = ser.readline()
  110. if not re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  111. if DEBUG: print "got:", resp
  112. time.sleep(2)
  113. ###
  114. ###
  115. if DEBUG: print "send:", "AT+CGPS=1"
  116. ser.write("AT+CGPS=1\r")
  117. ser.flush()
  118. resp = ser.readline()
  119. if not re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  120. resp = ser.readline()
  121. if not re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  122. if DEBUG: print "got:", resp
  123. time.sleep(1)
  124. ###
  125. ###
  126. if DEBUG: print "send:", "AT+CGPSINFOCFG=" + str(FREQ_SECOND) + "," + str(BITMASK)
  127. ser.write("AT+CGPSINFOCFG=" + str(FREQ_SECOND) + "," + str(BITMASK) + "\r")
  128. ser.flush()
  129. resp = ser.readline()
  130. if not re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  131. resp = ser.readline()
  132. if not re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
  133. if DEBUG: print "got:", resp
  134. time.sleep(1)
  135. ###
  136. ###
  137. if DEBUG: print "closing connection"
  138. ser.close()