Bläddra i källkod

detecting GPS module

clementinecomputing 6 år sedan
förälder
incheckning
495b2f4c68
1 ändrade filer med 48 tillägg och 9 borttagningar
  1. 48 9
      busunit/scripts/setup-serial.py

+ 48 - 9
busunit/scripts/setup-serial.py

@@ -8,25 +8,60 @@ from os import listdir
 from os.path import isfile, join
 from os import system
 
+DEBUG = False
+VERBOSE = True
+
+ret = system("rm -f /dev/ttyPIU /dev/ttyGPRS /dev/ttyDIU /dev/ttyGPS")
 
 DEVDIR = "/dev"
 
+###################
+
+ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyACM', f)]
+for devfn in ttyfn:
+  if DEBUG: print devfn
+  ser = serial.Serial(join(DEVDIR,devfn), 9600, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
+
+
+  x0 = ser.readline()
+  if DEBUG: print "at n (0):", x0
+  x1 = ser.readline()
+  if DEBUG: print "at n (1):", x1
+  x2 = ser.readline()
+  if DEBUG: print "at n (2):", x2
+  time.sleep(1)
+
+  if (re.match(r'^\$GP', x0) or
+      re.match(r'^\$GP', x1) or
+      re.match(r'^\$GP', x2)):
+    src_tty = join(DEVDIR,devfn)
+    dst_tty = join(DEVDIR,"ttyGPS")
+
+    if VERBOSE:
+      print src_tty, "->", dst_tty
+
+    system("ln -s " + src_tty + " " + dst_tty)
+
+  ser.close()
+
+
+###################
+
 ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyUSB', f)]
 
-ret = system("rm -f /dev/ttyPIU /dev/ttyGPRS /dev/ttyDIU /dev/ttyGPS")
 
 for devfn in ttyfn:
-  print devfn
+  if DEBUG: print devfn
   ser = serial.Serial(join(DEVDIR,devfn), 115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)
 
   ser.write("\rAT\r")
   ser.flush()
   x0 = ser.readline()
-  print "at n (0):", x0
+  if DEBUG: print "at n (0):", x0
   x1 = ser.readline()
-  print "at n (1):", x1
+  if DEBUG: print "at n (1):", x1
   x2 = ser.readline()
-  print "at n (2):", x2
+  if DEBUG: print "at n (2):", x2
   time.sleep(1)
 
   x0 = x0.strip()
@@ -38,8 +73,9 @@ for devfn in ttyfn:
       x2 == "ok" or x2 == "OK"):
     src_tty = join(DEVDIR,devfn)
     dst_tty = join(DEVDIR,"ttyGPRS")
-    #print join(DEVDIR,devfn), "->", join(DEVDIR,"ttyGPRS")
-    print src_tty, "->", dst_tty
+
+    if VERBOSE:
+      print src_tty, "->", dst_tty
 
     system("ln -s " + src_tty + " " + dst_tty)
 
@@ -48,9 +84,12 @@ for devfn in ttyfn:
       x2[0:4] == "/?: "):
     src_tty = join(DEVDIR,devfn)
     dst_tty = join(DEVDIR,"ttyPIU")
-    #print join(DEVDIR,devfn), "->", join(DEVDIR,"ttyPIU")
 
-    print src_tty, "->", dst_tty
+    if VERBOSE:
+      print src_tty, "->", dst_tty
+
     system("ln -s " + src_tty + " " + dst_tty)
 
+  ser.close()
+