Browse Source

setup-serial (wip) to setup /dev/tty* symbolic links by querying serial connections

* bill_communication.c - minor fixes, taking out debug messages
clementinecomputing 6 years ago
parent
commit
e9d397ddd6
2 changed files with 50 additions and 10 deletions
  1. 8 10
      busunit/billdb/bill_communication.c
  2. 42 0
      busunit/scripts/setup-serial.py

+ 8 - 10
busunit/billdb/bill_communication.c

@@ -572,9 +572,6 @@ int main(int argc, char **argv)
             continue;
             continue;
         }
         }
 
 
-        //DEBUG
-        printf("## cp.nfd %i\n", (int)nfds);
-
         for(i=0; i < nfds; i++)
         for(i=0; i < nfds; i++)
         {
         {
             if( fds[i].fd == server_fd )
             if( fds[i].fd == server_fd )
@@ -582,20 +579,21 @@ int main(int argc, char **argv)
 
 
                 if(fds[i].revents & POLLIN)
                 if(fds[i].revents & POLLIN)
                 {
                 {
-
                     read_return = recv(fds[i].fd, input_line + input_idx, sizeof(input_line) - input_idx, 0);
                     read_return = recv(fds[i].fd, input_line + input_idx, sizeof(input_line) - input_idx, 0);
 
 
-                    //If the socket has closed politely (0), or had an error other than EINTR...
+                    // If the socket has closed politely (0), or had an error other than EINTR...
+                    //
                     if( (read_return == 0) || ((read_return < 0) && (errno != EINTR)) )
                     if( (read_return == 0) || ((read_return < 0) && (errno != EINTR)) )
                     {
                     {
 
 
-                      //DEBUG
-                      printf("## bang\n");
-
                         close(server_fd);
                         close(server_fd);
                         server_fd = -1;
                         server_fd = -1;
 
 
-            usleep(POLL_TIMEOUT * 1000);
+                        // EXPERIMENTAL: this might need to go when moving to production.
+                        // It's here to try and reduce the polling frequency when no connection
+                        // is present
+                        //
+                        usleep(POLL_TIMEOUT * 1000);
 
 
                         break;
                         break;
                     }
                     }
@@ -689,6 +687,7 @@ int main(int argc, char **argv)
             }
             }
             else if( fds[i].fd == commhub_fd )
             else if( fds[i].fd == commhub_fd )
             {  
             {  
+
                 //If we've lost connection, break this loop and poll all over again
                 //If we've lost connection, break this loop and poll all over again
                 if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))  
                 if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))  
                 {
                 {
@@ -699,7 +698,6 @@ int main(int argc, char **argv)
 
 
                 if(fds[i].revents & POLLIN)
                 if(fds[i].revents & POLLIN)
                 {
                 {
-//                    printf("Trying to read from hub...\n");
                     read_return = get_message(commhub_fd, &incoming_msg);
                     read_return = get_message(commhub_fd, &incoming_msg);
 
 
                     if( read_return < 0 )
                     if( read_return < 0 )

+ 42 - 0
busunit/scripts/setup-serial.py

@@ -0,0 +1,42 @@
+#!/usr/bin/python
+
+import serial
+import sys
+import re
+import time
+from os import listdir
+from os.path import isfile, join
+
+DEVDIR = "/dev"
+
+ttyfn = [f for f in listdir(DEVDIR) if re.match(r'^ttyUSB', f)]
+
+for devfn in ttyfn:
+  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
+  x1 = ser.readline()
+  print "at n (1):", x1
+  x2 = ser.readline()
+  print "at n (2):", x2
+  time.sleep(1)
+
+  x0 = x0.strip()
+  x1 = x1.strip()
+  x2 = x2.strip()
+
+  if (x0 == "ok" or x0 == "OK" or
+      x1 == "ok" or x1 == "OK" or
+      x2 == "ok" or x2 == "OK"):
+    print join(DEVDIR,devfn), "->", join(DEVDIR,"ttyGPRS")
+
+  if (x0[0:4] == "/?: " or
+      x1[0:4] == "/?: " or
+      x2[0:4] == "/?: "):
+    print join(DEVDIR,devfn), "->", join(DEVDIR,"ttyPIU")
+
+