Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
sdk:gps2serial [2020/03/25 10:02] fachetsdk:gps2serial [2020/03/25 12:21] (current) fachet
Line 5: Line 5:
 ===== Script Summary  ===== ===== Script Summary  =====
  
-This Script connects to the GPS server via tcpT +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 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.  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. Please use if possible a higher data rate e.g. 115200.
Line 25: 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 4800, 9600, 19200, 38400, 57600, 115200 */ +
-SER_SPEED=4800; +
-/*  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 69: 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*/
 SER_SPEED = 4800; /* Serial Baud Rate 4800, 9600, 19200, 38400, 57600, 115200 */ SER_SPEED = 4800; /* Serial Baud Rate 4800, 9600, 19200, 38400, 57600, 115200 */
Line 82: Line 38:
 SER_FLOW = 0; / flow control (0=none, 1=xon/xoff, 2=hardware) */ SER_FLOW = 0; / flow control (0=none, 1=xon/xoff, 2=hardware) */
 DEV = "SERIAL1"; / name of the Serial Interface */ DEV = "SERIAL1"; / name of the Serial Interface */
-  + 
 /* GPS Deamon Parameters */ /* GPS Deamon Parameters */
 HOST = "127.0.0.1"; /* local host */ HOST = "127.0.0.1"; /* local host */
-PORT = nb_config_get("gpsd.0.port"); +PORT = (int) nb_config_get("gpsd.0.port"); 
- +  
-void usage()  +void usage() { 
-+    nb_syslog("Usage: gps2serial.are\n");
-    nb_syslog("Usage: gps2serial.are");+
     exit(1);     exit(1);
 } }
 + 
 int start_serial() { int start_serial() {
-    /set attributes */+ if (nb_config_get("serial.0.status") != "2") { 
 +    nb_syslog("serial not assinged to SDK\n"); 
 +        exit(2); 
 +    } 
 +    // 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", rc, DEV);+        nb_syslog("ERROR: unable to set serial attributes (rc %d) to %s\n", rc, DEV);
         return -1;         return -1;
     }     }
-    /* open serial port */+    // 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", DEV);+        nb_syslog("ERROR: can't open serial port %s\n", DEV);
         return -1;         return -1;
 +    }
     return fd;     return fd;
 } }
- +  
-int connect_gpsd() /* tries infinetively to connect to gpsd - socket returned */+int connect_gpsd()
 { {
 +    if (nb_config_get("gpsd.0.status") != "1" || nb_config_get("gpsd.0.cmode") != "1") {
 +    nb_syslog("gpsd not enabled or not in RAW mode\n");
 +        exit(3);
 +    }
     while (1) {     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");
         else if (connect(sock, HOST, PORT) < 0){         else if (connect(sock, HOST, PORT) < 0){
-            nb_syslog("Could not connect to GPS daemon");+            nb_syslog("Could not connect to GPS daemon\n");
             close(sock);             close(sock);
         } else {         } else {
-            nb_syslog("Successfully connected to GPS daemon");+            nb_syslog("Successfully connected to GPS daemon\n");
             return sock;             return sock;
         }         }
Line 123: Line 87:
     /* no return */     /* no return */
 } }
- +  
 + 
 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();
-nb_syslog("Processing NMEA data"); 
  
 +// printf("Processing NMEA data\n"); 
 while (1) { while (1) {
-    /wait for socket data  */ +    // printf("wait for data\n"); 
-    if ((rv = select(gpsd, 3)) == 0)+    if ((rv = select(gpsd, 10)) == 0) 
 + // printf("Select timed out\n"); 
       continue;       continue;
-    if (rv < 0) { +
-        nb_syslog("Receive failed, re-connecting to GPS daemon");+    if (rv < 0 || ( data = recv(gpsd)) == "") { 
 +        nb_syslog("Select or recv failed, re-connecting to GPS daemon\n"); 
 + close(gpsd);
         if ((gpsd = connect_gpsd()) < 0)         if ((gpsd = connect_gpsd()) < 0)
-            exit(3); /* can't connect */+            exit(5); /* can't connect */
         continue;         continue;
     }     }
-    data = recv(gpsd); 
     len = strlen(data);     len = strlen(data);
 + // printf("write data to serial <%s> [%i]\n", data, len);
     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", 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(4);+exit(6);
 </code> </code>