Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
sdk:gps2serial [2015/05/15 13:13] – created jurascheksdk:gps2serial [2020/03/25 12:18] fachet
Line 5: Line 5:
 ===== Script Summary  ===== ===== Script Summary  =====
  
-This Script connects to the GPS server via tcp. The NMEA Frames will be startet by the request "R" and will be then written to the serial interface.  +This Script connects to the GPS server and forwards the data to the first serial interface. 
- +GPS must be enabled and in RAW mode. 
-The default serial parameter are 115200 8N1 (8 databit, no parity, 1 stop bit, no flow controll). This parameter can be configured at the begining of the script. +The default serial parameters for GPS are 4800 8N1 (8 databit, no parity, 1 stop bit, no flow controll). This parameter can be configured at the begining of the script.  
 +Please use if possible a higher data rate e.g. 115200.
  
 ===== Pre Requierments ===== ===== Pre Requierments =====
Line 13: Line 14:
 To use this script you need to enable the GNSS Interface on the Router to your needs.  Please use the "raw-mode" with this SDK script.  To use this script you need to enable the GNSS Interface on the Router to your needs.  Please use the "raw-mode" with this SDK script. 
  
 +{{ :sdk:screenshot_-_150515_-_15_28_39.png?nolink&600 |}}
  
 Also assign the serial interface to the SDK Also assign the serial interface to the SDK
 +
 +{{ :sdk:screenshot_-_150515_-_15_28_56.png?nolink&600 |}}
  
  
Line 22: Line 25:
 Detailed step by step instructions how to install a sdk script can be found [[sdk:testing-the-sms-send-script|on this wiki page]] Detailed step by step instructions how to install a sdk script can be found [[sdk:testing-the-sms-send-script|on this wiki page]]
  
-Please combine this script with the **"system-startup" trigger** to a job +Please combine this script with the **"system-startup" trigger** to get a job.
- +
-This script does not need any configuration, but you can define the serial parameter to your needs: +
- +
-<code c> +
- +
-/* Serial Baud Rate 9600, 19200, 38400, 57600, 115200 */ +
-SER_SPEED=115200; +
-/*  number of data bits (5, 6, 7, 8) */ +
-SER_DATABIT=8; +
-/*  number of stop bits (1, 2) */ +
-SER_STOPBIT=1; +
-/* parity (0=no parity, 1=odd parity, 2=even parity) */ +
-SER_PARITY=0; +
-/*  flow control (0=none, 1=xon/xoff, 2=hardware) */ +
-SER_FLOW=0; +
-/*  name of the Serial Interface (Only changed if your device has more then one serial Interface (NB3710) */ +
-DEV = "SERIAL1"; +
- +
- +
-</code> +
- +
-===== Testing ====== +
- +
-To be sure the script is running you can take a look on the [[http://simulator.netmodule.com/logs.php.html|System Log]] and watch for outputs like:  +
-<code bash> +
-Apr 23 00:53:41 NB2700 user.info sdkhost[2283]: testrun: starting serial +
-Apr 23 00:53:42 NB2700 user.info sdkhost[2283]: testrun: Connecting to GPS daemon +
-Apr 23 00:53:42 NB2700 user.info sdkhost[2283]: testrun: Requesting raw NMEA stream +
-Apr 23 00:53:42 NB2700 user.info sdkhost[2283]: testrun: Successfully connected to GPS daemon +
-Apr 23 00:53:42 NB2700 user.info sdkhost[2283]: testrun: Processing NMEA data +
-Apr 23 00:53:42 NB2700 user.info sdkhost[2283]: testrun: Unable to send message to via Serial +
-Apr 23 00:54:08 NB2700 user.info sdkhost[2283]: testrun: starting serial +
-Apr 23 00:54:09 NB2700 user.info sdkhost[2283]: testrun: Connecting to GPS daemon +
-Apr 23 00:54:09 NB2700 user.info sdkhost[2283]: testrun: Requesting raw NMEA stream +
-Apr 23 00:54:09 NB2700 user.info sdkhost[2283]: testrun: Successfully connected to GPS daemon +
-Apr 23 00:54:09 NB2700 user.info sdkhost[2283]: testrun: Processing NMEA data +
- +
-</code>+
  
 ===== The Script ===== ===== The Script =====
Line 66: Line 31:
  
 <code c gps-to-serial.are> <code c gps-to-serial.are>
- 
- 
-/* DESC: This script sends the local GPS NMEA stream (incl. serial/checksum) to a remote UDP server. 
- * Copyright (C) 2013-2015 NetModule AG, Switzerland 
- */ 
- 
 /* Options to configure by user*/ /* Options to configure by user*/
-/* Serial Baud Rate 9600, 19200, 38400, 57600, 115200 */ +SER_SPEED = 4800; /* Serial Baud Rate 4800, 9600, 19200, 38400, 57600, 115200 */ 
-SER_SPEED=115200; +SER_DATABIT 8; /*  number of data bits (5, 6, 7, 8) */ 
-/*  number of data bits (5, 6, 7, 8) */ +SER_STOPBIT 1; /*  number of stop bits (1, 2) */  
-SER_DATABIT=8; +SER_PARITY 0; /* parity (0=no parity, 1=odd parity, 2=even parity) */ 
-/*  number of stop bits (1, 2) */ +SER_FLOW = 0; /*  flow control (0=none, 1=xon/xoff, 2=hardware) */ 
-SER_STOPBIT=1; +DEV "SERIAL1"; /*  name of the Serial Interface */ 
-/* parity (0=no parity, 1=odd parity, 2=even parity) */ +  
-SER_PARITY=0; +/* GPS Deamon Parameters */ 
-/*  flow control (0=none, 1=xon/xoff, 2=hardware) */ +HOST = "127.0.0.1"; /* local host */ 
-SER_FLOW=0; +PORT = (int) nb_config_get("gpsd.0.port"); 
-/*  name of the Serial Interface (Only changed if your device has more then one serial Interface (NB3710) */ +  
-DEV = "SERIAL1"; +void usage() { 
- +    nb_syslog("Usage: gps2serial.are\n");
- +
- +
-void usage()  +
-+
-    nb_syslog("Usage: gps-udp-client.are <server> <port>");+
     exit(1);     exit(1);
 } }
- + 
 int start_serial() { int start_serial() {
- + if (nb_config_get("serial.0.status") != "2") { 
-    /* check serial port config */ +     nb_syslog("serial not assinged to SDK\n"); 
-    status = nb_config_get("serial.0.status")+        exit(2);
-    if (status != "2") { +
-        nb_syslog("Serial port is not enabled for us"); +
-        return -1;+
     }     }
- +    // printf("set serial attributes\n"); 
-    /set attributes */ +    if ((rc = nb_serial_setattr(DEV, SER_SPEED, SER_DATABIT, SER_STOPBIT, SER_PARITY, SER_FLOW)!= 0) { 
-    rc = nb_serial_setattr(DEV, SER_SPEED, SER_DATABIT, SER_STOPBIT, SER_PARITY, SER_FLOW)+        nb_syslog("ERROR: unable to set serial attributes (rc %d) to %s\n", rc, DEV);
-    if (rc != 0) { +
-        nb_syslog("ERROR: unable to set serial attributes (rc %d)", rc);+
         return -1;         return -1;
     }     }
- +    // nb_syslog("Open serial device %s\n", DEV); 
-    /* open serial port */ +    if ((fd = nb_serial_open(DEV)) < 0) { 
-    fd = nb_serial_open(DEV); +        nb_syslog("ERROR: can't open serial port %s\n", DEV);
-    if (fd < 0) {+
         return -1;         return -1;
     }     }
- 
     return fd;     return fd;
 } }
- +  
-int connect_gpsd ()+int connect_gpsd()
 { {
-    for (attempt = 0; attempt < 60; attempt++) { +    if (nb_config_get("gpsd.0.status") !"1" || nb_config_get("gpsd.0.cmode") != "1") { 
-        sleep(1); +    nb_syslog("gpsd not enabled or not in RAW mode\n"); 
 +        exit(3); 
 +    } 
 +    while (1) {
         sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);         sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-        if (sock < 0) { +        if (sock < 0) 
-            nb_syslog("Unable to create socket"); +            nb_syslog("Unable to create socket\n"); 
-            continue; +        else if (connect(sock, HOSTPORT) < 0){ 
-        } +            nb_syslog("Could not connect to GPS daemon\n");
-        nb_syslog("Connecting to GPS daemon"); +
-        if (connect(sock, "127.0.0.1"2947) < 0){ +
-            nb_syslog("Could not connect to GPS daemon");+
             close(sock);             close(sock);
-            continue;+        } else { 
 +            nb_syslog("Successfully connected to GPS daemon\n"); 
 +            return sock;
         }         }
-        nb_syslog("Requesting raw NMEA stream")+      sleep(10);
-        if (send(sock, "R\n") < 0) { +
-            nb_syslog("Unable to request raw NMEA stream"); +
-            close(sock); +
-            continue; +
-        } +
- +
-        nb_syslog("Successfully connected to GPS daemon"); +
-        return sock;+
     }     }
- +    /* no return */
-    return -1;+
 } }
- +  
- + 
-SERVER = trim((string) argv[1]); +
-PORT = (int) argv[2]; +
- +
-SERIAL = struct_get(nb_status("system"), "SERIAL_NUMBER"); +
-if (SERIAL == "") { +
-    nb_syslog("Unable to obtain serial number"); +
-    exit(1); +
-} +
 nb_syslog("starting serial"); nb_syslog("starting serial");
-serial_fd = start_serial()+if ((serial_fd = start_serial()< 0 ) { 
-if (serial_fd < 0 ) { +    nb_syslog("Could not start serial, exiting\n"); 
-    nb_syslog("Could not start serial, exiting"); +    exit(4);
-    exit(1);+
 } }
 +// printf("connect to gpsd\n");
 gpsd = connect_gpsd(); gpsd = connect_gpsd();
-if (gpsd < 0) { 
-    nb_syslog("Could not start gpsd socket, exiting"); 
-    exit(1); 
-} 
- 
-/* process NMEA stream */ 
-nb_syslog("Processing NMEA data"); 
  
 +// printf("Processing NMEA data\n"); 
 while (1) { while (1) {
-    /wait for socket data  */ +    // printf("wait for data\n"); 
-    rv = select(gpsd, 3)+    if ((rv = select(gpsd, 10)) == 0) { 
-    if (rv > 0) { + // printf("Select timed out\n");  
-        data = recv(gpsd); +       continue; 
-        len = strlen(data)+ } 
-    } else if (rv < 0) { +    if (rv < 0 || ( data = recv(gpsd)) == "") { 
-        nb_syslog("select failed"); +        nb_syslog("Select or recv failed, re-connecting to GPS daemon\n"); 
-        len -1; +        if ((gpsd connect_gpsd()) < 0) 
-    } else { +            exit(5); /* can't connect */
-        /* nothing received */+
         continue;         continue;
-    }  
-    if (len <= 0) { 
-        nb_syslog("Receive failed, re-connecting to GPS daemon"); 
-        close(gpsd); 
-        gpsd = connect_gpsd(); 
-        if (gpsd < 0) { 
-            exit(1); 
-        } 
-    } else { 
-        str = strcat("PNMID,", SERIAL); 
-        chars = explode(str); 
- 
-        checksum = 0; 
-        for (i = 0; i < length(chars); i++) { 
-            c = ord(chars[i]); 
-            checksum ^= c; 
-        } 
-        sentence = sprintf("$%s*%02X\r\n", str, checksum); 
-        data = strcat(data, sentence); 
-        len = strlen(data); 
-        nb_syslog("len: %i", len); 
-        if (len > 0) { 
-            sent = write(serial_fd, data, len); 
-            if (sent != len) { 
-                nb_syslog("Unable to send message via Serial, sent: %i, len: %i",sent, len); 
-            } 
-        } 
     }     }
- +    len = strlen(data); 
-    + // printf("write data to serial <%s> [%i]\n", data, len); 
 +    if (write(serial_fd, data, len) != 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(0);+