This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
sdk:scripts:gps2serial-gnsstogps [2021/03/19 14:00] – created dodenhoeft | sdk: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 = " | ||
- | /* 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; | ||
- | DEV = " | ||
- | |||
/* default values */ | /* default values */ | ||
local_ip = " | local_ip = " | ||
local_port = (int) nb_config_get(" | local_port = (int) nb_config_get(" | ||
- | + | ||
gpsd = -1; | gpsd = -1; | ||
- | | + | |
// start serial connection | // start serial connection | ||
- | int start_serial() | + | int start_serial() { |
- | | + | |
- | if (nb_config_get(" | + | nb_syslog(" |
- | nb_syslog(" | + | exit(2); |
- | exit(2); | + | |
- | } | + | |
- | // printf(" | + | |
- | if ((rc = nb_serial_setattr(DEV, | + | nb_syslog(" |
- | nb_syslog(" | + | return -1; |
- | return -1; | + | |
- | } | + | |
- | // nb_syslog(" | + | |
- | if ((fd = nb_serial_open(DEV)) < 0) { | + | nb_syslog(" |
- | nb_syslog(" | + | return -1; |
- | return -1; | + | |
- | } | + | |
- | return fd; | + | |
} | } | ||
// restart GPS deamon | // restart GPS deamon | ||
- | int gpsd_restart () | + | int gpsd_restart() { |
- | | + | |
- | nb_syslog(" | + | |
- | | + | |
- | nb_config_set(" | + | |
- | do { | + | sleep(3); |
- | sleep(3); | + | |
- | } while ( nb_config_done != 0) | + | |
- | | + | |
- | nb_config_set(" | + | |
- | do { | + | sleep(5); |
- | | + | |
- | } while ( nb_config_done != 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(" | + | |
- | | + | |
- | gpsd = socket(AF_INET, | + | |
- | if (gpsd < 0) { | + | nb_syslog(" |
- | nb_syslog(" | + | exit(-1); |
- | exit(-1); | + | |
- | } | + | |
- | for (attempt = 0; attempt <= 15; attempt++) { | + | sleep(15); |
- | sleep(15); | + | |
- | | + | if (connect(gpsd, |
- | if (connect(gpsd, | + | |
- | nb_syslog(" | + | |
- | if (attempt == 12) { | + | nb_syslog(" |
- | nb_syslog(" | + | gpsd_restart(); |
- | gpsd_restart(); | + | |
- | } | + | } else { |
- | } else { | + | |
- | break; | + | } |
- | } | + | |
- | } | + | |
- | nb_syslog(" | + | |
- | send(gpsd, " | + | |
- | | + | |
- | nb_syslog(" | + | |
- | | + | |
- | return gpsd; | + | |
} | } | ||
- | + | ||
- | string checksum(string s) | + | string checksum(string s) { |
- | | + | |
- | pos = strchr(s, " | + | |
- | | + | |
- | if (is_void(pos)) | + | return s; /* no checksum to correct */ |
- | return s; /* no checksum to correct */ | + | |
- | | + | |
- | cs = 0; | + | |
- | chars = explode(s); | + | |
- | for (i = 1; i < pos; i++) { /* skip $ and *XX */ | + | |
- | | + | c = ord(chars[i]); |
- | cs ^= c; | + | cs ^= c; |
- | } | + | |
- | r = sprintf(" | + | |
- | return r; | + | |
} | } | ||
- | + | ||
/* main() */ | /* main() */ | ||
- | + | ||
if (argc < 2) { | if (argc < 2) { | ||
- | | + | usage(); |
} | } | ||
- | + | ||
- | + | ||
/* starting serial */ | /* starting serial */ | ||
nb_syslog(" | nb_syslog(" | ||
- | if ((serial_fd = start_serial()) < 0 ) { | + | if ((serial_fd = start_serial()) < 0) { |
- | nb_syslog(" | + | |
- | exit(4); | + | |
} | } | ||
- | |||
/* process NMEA stream */ | /* process NMEA stream */ | ||
nb_syslog(" | nb_syslog(" | ||
- | while(1) { | + | while (1) { |
- | /* connect to gpsd */ | + | |
- | if(gpsd < 0) { | + | |
- | gpsd = gpsd_connect(gpsd); | + | gpsd = gpsd_connect(gpsd); |
- | if(gpsd < 0) { | + | if (gpsd < 0) { |
- | nb_syslog(" | + | |
- | sleep(5); | + | |
- | continue; | + | |
- | } | + | } |
- | } | + | |
- | | + | |
- | /* wait for client socket data */ | + | |
- | rv = select(gpsd, | + | |
- | | + | |
- | if(rv == -1) { | + | |
- | nb_syslog(" | + | nb_syslog(" |
- | close(gpsd); | + | close(gpsd); |
- | gpsd = -1; | + | gpsd = -1; |
- | continue; | + | continue; |
- | } else if(rv == 0) { | + | |
- | /* nothing received */ | + | /* nothing received */ |
- | continue; | + | continue; |
- | } | + | |
- | | + | |
- | + | | |
- | data = recv(gpsd); | + | |
- | len = strlen(data); | + | |
- | /* Check for correct framing: CR+LF */ | + | |
- | if(len > 2) { | + | if (strrchr(data, |
- | if(strrchr(data, | + | |
- | nb_syslog(" | + | |
- | close(gpsd); | + | |
- | gpsd = -1; | + | |
- | gpsd_restart(); | + | |
- | continue; | + | } |
- | } | + | |
- | } | + | |
- | | + | |
- | if(len == 0) { | + | nb_syslog(" |
- | nb_syslog(" | + | close(gpsd); |
- | close(gpsd); | + | gpsd = -1; |
- | gpsd = -1; | + | continue; |
- | continue; | + | |
- | } | + | |
- | /* Convert message $GN to $GP for backward compatibility */ | + | |
- | if(strstr(data, | + | |
- | a_arr = explode(data); | + | a_arr = explode(data); |
- | a_arr[2] = " | + | a_arr[2] = " |
- | data = checksum(implode(a_arr)); | + | data = checksum(implode(a_arr)); |
- | } | + | |
- | /* skip all messages different from GPGGA | + | |
- | * if(strstr(data," | + | * if(strstr(data," |
- | * continue; | + | * continue; |
- | */ | + | */ |
- | sent = write(serial_fd, | + | |
- | nb_syslog(" | + | |
- | | + | |
- | | + | nb_syslog(" |
- | | + | |
} | } | ||
- | + | ||
close(gpsd); | close(gpsd); | ||
close(serial_fd); | close(serial_fd); | ||
exit(6); | exit(6); | ||
+ | </ |