Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
sdk:scripts:gps2serial-gnsstogps [2021/03/19 14:00] – created dodenhoeftsdk:scripts:gps2serial-gnsstogps [2021/03/19 14:08] (current) dodenhoeft
Line 1: Line 1:
 ====== SDK Script gps2serial-GNSStoGPS.are ====== ====== SDK Script gps2serial-GNSStoGPS.are ======
 <code c gps2serial-gnsstogps.are> <code c gps2serial-gnsstogps.are>
 +    /* DESC: This script sends the local GPS NMEA stream to the serial interface.
 +     * DESC: It checks for the correct CR/LF end of line characters and convert messages $GN to $GP 
 +     * Copyright (C) 2013-2021) NetModule AG, Switzerland
 +     */
 +    /* Options to configure by user*/
 +    SER_SPEED = (int) argv[2]; /* Serial Baud Rate 4800, 9600, 19200, 38400, 57600, 115200 */
 +    SER_DATABIT = 8; /*  number of data bits (5, 6, 7, 8) */
 +    SER_STOPBIT = 1; /*  number of stop bits (1, 2) */
 +    SER_PARITY = 0; /* parity (0=no parity, 1=odd parity, 2=even parity) */
 +    SER_FLOW = 0; /*  flow control (0=none, 1=xon/xoff, 2=hardware) */
 +    DEV = "SERIAL1"; /*  name of the Serial Interface */
  
-/* DESC: This script sends the local GPS NMEA stream to the serial interface. 
-    * DESC: It checks for the correct CR/LF end of line characters and convert messages $GN to $GP  
-    * Copyright (C) 2013-2021) NetModule AG, Switzerland 
-    */ 
-     /* Options to configure by user*/ 
-    SER_SPEED = (int) argv[2]; /* Serial Baud Rate 4800, 9600, 19200, 38400, 57600, 115200 */ 
-    SER_DATABIT = 8; /*  number of data bits (5, 6, 7, 8) */ 
-    SER_STOPBIT = 1; /*  number of stop bits (1, 2) */  
-    SER_PARITY = 0; /* parity (0=no parity, 1=odd parity, 2=even parity) */ 
-    SER_FLOW = 0; / flow control (0=none, 1=xon/xoff, 2=hardware) */ 
-    DEV = "SERIAL1"; / name of the Serial Interface */ 
-  
     /* default values */     /* default values */
     local_ip = "127.0.0.1";     local_ip = "127.0.0.1";
     local_port = (int) nb_config_get("gpsd.0.port");     local_port = (int) nb_config_get("gpsd.0.port");
-     +
     gpsd = -1;     gpsd = -1;
-            +
     // start serial connection     // start serial connection
-    int start_serial()  +    int start_serial() { 
-    +       if (nb_config_get("serial.0.status") != "2") { 
-     if (nb_config_get("serial.0.status") != "2") { +          nb_syslog("serial not assinged to SDK\n"); 
-         nb_syslog("serial not assinged to SDK\n"); +          exit(2); 
-            exit(2); +       
-        +       // printf("set serial attributes\n"); 
-        // printf("set serial attributes\n"); +       if ((rc = nb_serial_setattr(DEV, SER_SPEED, SER_DATABIT, SER_STOPBIT, SER_PARITY, SER_FLOW)) != 0) { 
-        if ((rc = nb_serial_setattr(DEV, SER_SPEED, SER_DATABIT, SER_STOPBIT, SER_PARITY, SER_FLOW)) != 0) { +          nb_syslog("ERROR: unable to set serial attributes (rc %d) to %s\n", rc, DEV); 
-            nb_syslog("ERROR: unable to set serial attributes (rc %d) to %s\n", rc, DEV); +          return -1; 
-            return -1; +       
-        +       // nb_syslog("Open serial device %s\n", DEV); 
-        // nb_syslog("Open serial device %s\n", DEV); +       if ((fd = nb_serial_open(DEV)) < 0) { 
-        if ((fd = nb_serial_open(DEV)) < 0) { +          nb_syslog("ERROR: can't open serial port %s\n", DEV); 
-            nb_syslog("ERROR: can't open serial port %s\n", DEV); +          return -1; 
-            return -1; +       
-        +       return fd;
-        return fd;+
     }     }
     // restart GPS deamon     // restart GPS deamon
-    int gpsd_restart ()  +    int gpsd_restart() { 
-    +       nb_syslog("Restarting GPS daemon"); 
-     nb_syslog("Restarting GPS daemon"); + 
-      +       nb_config_set("gpsd.0.status=0"); 
-     nb_config_set("gpsd.0.status=0"); +       do { 
-                    do { +          sleep(3); 
-                                    sleep(3); +       } while (nb_config_done != 0) 
-                    } while ( nb_config_done != 0) + 
-      +       nb_config_set("gpsd.0.status=1"); 
-     nb_config_set("gpsd.0.status=1"); +       do { 
-                    do { +          sleep(5); 
-                     sleep(5); +       } while (nb_config_done != 0) 
-                    } while ( nb_config_done != 0) +       return 0;
-     return 0;+
     }     }
- +
     // start TCP Socket for GPS deamon loop      // start TCP Socket for GPS deamon loop 
-    int gpsd_connect () +    int gpsd_connect() { 
-    +       nb_syslog("Connecting to GPS daemon"); 
-     nb_syslog("Connecting to GPS daemon"); + 
-      +       gpsd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
-     gpsd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); +       if (gpsd < 0) { 
-     if (gpsd < 0) { +          nb_syslog("ERROR: Unable to open socket"); 
-     nb_syslog("ERROR: Unable to open socket"); +          exit(-1); 
-     exit(-1); +       
-     +       for (attempt = 0; attempt <= 15; attempt++) { 
-     for (attempt = 0; attempt <= 15; attempt++) { +          sleep(15); 
-     sleep(15); + 
-      +          if (connect(gpsd, local_ip, local_port) < 0) { 
-     if (connect(gpsd, local_ip, local_port) < 0) { +             nb_syslog("Could not connect to daemon"); 
-     nb_syslog("Could not connect to daemon"); +             if (attempt == 12) { 
-     if (attempt == 12) { +                nb_syslog("Unable to connect, restarting daemon"); 
-     nb_syslog("Unable to connect, restarting daemon"); +                gpsd_restart(); 
-     gpsd_restart(); +             
-     +          } else { 
-     } else { +             break; 
-     break; +          
-     +       
-     +       nb_syslog("Connected to daemon, requesting NMEA"); 
-     nb_syslog("Connected to daemon, requesting NMEA"); +       send(gpsd, "R=1\n"); 
-     send(gpsd, "R=1\n"); + 
-      +       nb_syslog("Processing NMEA"); 
-     nb_syslog("Processing NMEA"); + 
-      +       return gpsd;
-     return gpsd;+
     }     }
-      + 
-    string checksum(string s) +    string checksum(string s) { 
-    +       pos = strchr(s, "*"); 
-     pos = strchr(s, "*"); + 
-      +       if (is_void(pos)) 
-       if (is_void(pos)) +          return s; /* no checksum to correct */ 
-     return s; /* no checksum to correct */ + 
-      +       cs = 0; 
-     cs = 0; +       chars = explode(s); 
-     chars = explode(s); +       for (i = 1; i < pos; i++) { 
-            for (i = 1; i < pos; i++) { /* skip $ and *XX */  +          /* skip $ and *XX */ 
-         c = ord(chars[i]); +          c = ord(chars[i]); 
-                cs ^= c; +          cs ^= c; 
-            +       
-            r = sprintf("%s%02X%s", substr(s, 0, pos + 1), cs, substr(s, pos + 3)); +       r = sprintf("%s%02X%s", substr(s, 0, pos + 1), cs, substr(s, pos + 3)); 
-     return r;+       return r;
     }     }
-     +
     /* main() */     /* main() */
-     +
     if (argc < 2) {     if (argc < 2) {
-        usage();+       usage();
     }     }
-      +
-     +
     /* starting serial */     /* starting serial */
     nb_syslog("starting serial");     nb_syslog("starting serial");
-    if ((serial_fd = start_serial()) < 0 ) { +    if ((serial_fd = start_serial()) < 0) { 
-        nb_syslog("Could not start serial, exiting\n"); +       nb_syslog("Could not start serial, exiting\n"); 
-        exit(4);+       exit(4);
     }     }
  
-      
     /* process NMEA stream */     /* process NMEA stream */
     nb_syslog("Processing NMEA data");     nb_syslog("Processing NMEA data");
-    while(1) { +    while (1) { 
-     /* connect to gpsd */ +       /* connect to gpsd */ 
-     if(gpsd < 0) { +       if (gpsd < 0) { 
-     gpsd = gpsd_connect(gpsd); +          gpsd = gpsd_connect(gpsd); 
-     if(gpsd < 0) { +          if (gpsd < 0) { 
-     nb_syslog("ERROR: Unable to connect to daemon"); +             nb_syslog("ERROR: Unable to connect to daemon"); 
-     sleep(5); +             sleep(5); 
-     continue; +             continue; 
-     +          
-     +       
-      + 
-     /* wait for client socket data  */ +       /* wait for client socket data  */ 
-     rv = select(gpsd, 3); +       rv = select(gpsd, 3); 
-      + 
-     if(rv == -1) { +       if (rv == -1) { 
-     nb_syslog("ERROR: select failed, re-connecting"); +          nb_syslog("ERROR: select failed, re-connecting"); 
-     close(gpsd); +          close(gpsd); 
-     gpsd = -1; +          gpsd = -1; 
-     continue; +          continue; 
-     } else if(rv == 0) { +       } else if (rv == 0) { 
-     /* nothing received */ +          /* nothing received */ 
-     continue; +          continue; 
-     +       
-      + 
-      +       data = recv(gpsd); 
-    data = recv(gpsd); +       len = strlen(data); 
-     len = strlen(data); +       /* Check for correct framing: CR+LF */ 
-     /* Check for correct framing: CR+LF */ +       if (len > 2) { 
-     if(len > 2) { +          if (strrchr(data, "\r") != len - 2 || strrchr(data, "\n") != len - 1) { 
-     if(strrchr(data, "\r") != len-2 || strrchr(data, "\n") != len-1) { +             nb_syslog("ERROR: NMEA framing wrong"); 
-     nb_syslog("ERROR: NMEA framing wrong"); +             close(gpsd); 
-     close(gpsd); +             gpsd = -1; 
-     gpsd = -1; +             gpsd_restart(); 
-     gpsd_restart(); +             continue; 
-     continue; +          
-     +       
-     + 
-      +       if (len == 0) { 
-     if(len == 0) { +          nb_syslog("ERROR: no data, re-connecting"); 
-     nb_syslog("ERROR: no data, re-connecting"); +          close(gpsd); 
-     close(gpsd); +          gpsd = -1; 
-     gpsd = -1; +          continue; 
-     continue; +       
-     +       /* Convert message $GN to $GP for backward compatibility */ 
-     /* Convert message $GN to $GP for backward compatibility */ +       if (strstr(data, "$GN") == 0) { 
-     if(strstr(data, "$GN") == 0) {; +          
-     a_arr = explode(data); +          a_arr = explode(data); 
-     a_arr[2] = "P"; +          a_arr[2] = "P"; 
-     data = checksum(implode(a_arr)); +          data = checksum(implode(a_arr)); 
-     +       
-     /* skip all messages different from GPGGA  +       /* skip all messages different from GPGGA  
-     * if(strstr(data,"$GPGGA")==()) +        * if(strstr(data,"$GPGGA")==()) 
-     * continue; +        * continue; 
-     */ +        */ 
-     sent = write(serial_fd, data, len); +       sent = write(serial_fd, data, len); 
-    nb_syslog("sending Data via serial interface"); +       nb_syslog("sending Data via serial interface"); 
-       if (write(serial_fd, data, len) != len){ +       if (write(serial_fd, data, len) != len) { 
-           nb_syslog("Unable to send message via Serial, data %s, len: %i\n", data, len); +          nb_syslog("Unable to send message via Serial, data %s, len: %i\n", data, len); 
-   }+       }
     }     }
-     +
     close(gpsd);     close(gpsd);
     close(serial_fd);     close(serial_fd);
     exit(6);     exit(6);
 +</code>