Community
cancel
Showing results for 
Search instead for 
Did you mean: 
KSels
New Contributor II
1,106 Views

Read multiple strings from serial port.

I was able to read serial data from the serial port and send to console.log.

I am wondering how to save multible strings on serial port and then split the data up.

The strings are gps data.

  • String 1 = $GPGGA
  • String 2 = $GPGLL
  • String 3 = $GPGSA
  • String 4 = $GPGSV
  • String 5 = $GPGSV
  • String 6 = $GPGSV
  • String 7 = $GPGSV
  • String 8 = $GPRMC
  • String 9 = $GPVTG

Current running code.

var m = require('mraa'); //require mraa

var fs = require('fs'); // require filesystem

u = new m.Uart(0);

console.log(u.getDevicePath());

var SerialPort = require("serialport").SerialPort

var serialPort = new SerialPort(u.getDevicePath(), {

baudrate: 9600

});

// receive GPS (data)

var PMTK_SET_NMEA_UPDATE_1HZ = "$PMTK220,1000*1F";

var PMTK_SET_NMEA_UPDATE_5HZ = "$PMTK220,200*2C";

var PMTK_SET_NMEA_UPDATE_10HZ = "$PMTK220,100*2F";

// Baud Rate 0(default = 9600) - 115200

var PMTK_SET_NMEA_BAUDRATE_0 = "$PMTK251,0*28";

var PMTK_SET_NMEA_BAUDRATE_4800 = "$PMTK251,4800*14";

var PMTK_SET_NMEA_BAUDRATE_9600 = "$PMTK251,9600*17";

var PMTK_SET_NMEA_BAUDRATE_14400 = "$PMTK251,14400*29";

var PMTK_SET_NMEA_BAUDRATE_19200 = "$PMTK251,19200*22";

var PMTK_SET_NMEA_BAUDRATE_38400 = "$PMTK251,38400*27";

var PMTK_SET_NMEA_BAUDRATE_57600 = "$PMTK251,57600*2C";

var PMTK_SET_NMEA_BAUDRATE_115200 = "$PMTK251,115200*1F";

// turn on only the second sentence (GPRMC)

var PMTK_SET_NMEA_OUTPUT_RMC = "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29";

// GGA only

var PMTK_SET_NMEA_OUTPUT_GGA = "$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29";

// turn on RMC and GGA

var PMTK_SET_NMEA_OUTPUT_RMCGGA = "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28";

// turn on RMC, GGA and GSA

var PMTK_SET_NMEA_OUTPUT_RMCGGAGSA = "$PMTK314,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29";

// turn on ALL THE DATA

var PMTK_SET_NMEA_OUTPUT_ALLDATA = "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28";

// turn off output

var PMTK_SET_NMEA_OUTPUT_OFF = "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28";

// Standby

var PMTK_STANDBY = "$PMTK161,0*28";

//

var PMTK_Q_RELEASE = "$PMTK605*31";

// Ant off

var PMTK_ANT_OFF = "$PGCMD,33,0*6D";

// Active Interference Cancellation (AIC) Disable

var PMTK_AIC_OFF = "$PMTK286,0*22";

// Active Interference Cancellation (AIC) Enable

var PMTK_AIC_ON = "$PMTK286,1*23";

// DGPS Mode

var PMTK_DT_DGPS_MODE = "$PMTK401*37";

// Starting logging data to SD

var GPS_log = false;

function delay(milliseconds)

{

var startTime = Date.now();

while (Date.now() - startTime < milliseconds);

}

serialPort.on('open',function() {

console.log('Port open');

serialPort.write(PMTK_SET_NMEA_OUTPUT_ALLDATA + "\r\n");

console.log(PMTK_SET_NMEA_OUTPUT_ALLDATA);

delay(500);

serialPort.write(PMTK_DT_DGPS_MODE + "\r\n"); // DGPS

console.log(PMTK_DT_DGPS_MODE);

delay(500);

serialPort.write(PMTK_SET_NMEA_UPDATE_1HZ + "\r\n");

console.log(PMTK_SET_NMEA_UPDATE_1HZ);

delay(500);

serialPort.write(PMTK_AIC_ON + "\r\n"); // AIC ON

console.log(PMTK_AIC_ON);

delay(500);

//GPS_log = true;

});

// receive GPS (approx every second)

//write all received data into new_file

serialPort.on("open", function () {

serialPort.on('data', function(data) {

var data_string = '' + data

console.log(data_string);

/*

if (data_string.substring(0,6) == '$GPGGA') {

gpgga_string = data_string.split('\n')[0];

gpgga_string = gpgga_string.replace('\n', '');

gps_data = gpgga_string.split(',');

console.log('data received: ' + gps_data);

var time_string = gps_data[1];

var gps_time = new Date();

var gps_time_hours = parseInt(gps_data[1].substring(0,2));

var gps_time_minutes = parseInt(gps_data[1].substring(2,4));

var gps_time_seconds = parseInt(gps_data[1].substring(4,6));

gps_time.setHours(gps_time_hours);

gps_time.setMinutes(gps_time_minutes);

gps_time.setSeconds(gps_time_seconds);

gps_lat = gps_data[2];

gps_lat_ns = gps_data[3];

gps_lon = gps_data[5];

gps_lon_ew = gps_data[6];

//console.log('gps_time', gps_time);

//console.log(gps_lat, gps_lat_ns);

}

if (data_string.substring(0,6) == '$GPRMC') {

console.log('RMC data found');

}

*/

});

});

Tags (1)
0 Kudos
3 Replies
idata
Community Manager
52 Views

Hi Kim,

 

 

You could use the String.Split method. If I understand it correctly, you could set a list of chars that will be the parameters to separate/split the original string, so with this you could just make a list of possible chars that usually appear in your strings, then you pass this list of chars to detect in the original string and the String.Split method will do the job. You could check this example which is explained in more detail https://msdn.microsoft.com/en-us/library/ms228388.aspx https://msdn.microsoft.com/en-us/library/ms228388.aspx, it separates words using commas, colons, periods and tabs.

 

 

Regards,

 

Pablo
KSels
New Contributor II
52 Views

Tnx I will look at this.

Got it working, but not 100%

Here is my current code, only the GSV data is not complete(missing 3 of 4)

var m = require('mraa'); //require mraa

var fs = require('fs'); // require filesystem

var com = require("serialport");

// Get Edison Time

var dat = new Date();

var day = dat.getDate();

var month = dat.getMonth() + 1;

var year = dat.getFullYear();

// Set Serial port

var serialPort = new com.SerialPort("/dev/ttyMFD1", {

baudrate: 9600,

parser: com.parsers.readline('\r\n')

});

// Create .txt file

var fs = require('fs');

//var ws = fs.createWriteStream('/media/sdcard/GPS.txt');

var ws = fs.createWriteStream('/media/sdcard/GPS.txt', {'flags': 'a'});

// receive GPS (data)

var PMTK_SET_NMEA_UPDATE_1HZ = "$PMTK220,1000*1F";

var PMTK_SET_NMEA_UPDATE_5HZ = "$PMTK220,200*2C";

var PMTK_SET_NMEA_UPDATE_10HZ = "$PMTK220,100*2F";

// Baud Rate 0(default = 9600) - 115200

var PMTK_SET_NMEA_BAUDRATE_0 = "$PMTK251,0*28";

var PMTK_SET_NMEA_BAUDRATE_4800 = "$PMTK251,4800*14";

var PMTK_SET_NMEA_BAUDRATE_9600 = "$PMTK251,9600*17";

var PMTK_SET_NMEA_BAUDRATE_14400 = "$PMTK251,14400*29";

var PMTK_SET_NMEA_BAUDRATE_19200 = "$PMTK251,19200*22";

var PMTK_SET_NMEA_BAUDRATE_38400 = "$PMTK251,38400*27";

var PMTK_SET_NMEA_BAUDRATE_57600 = "$PMTK251,57600*2C";

var PMTK_SET_NMEA_BAUDRATE_115200 = "$PMTK251,115200*1F";

// turn on only the second sentence (GPRMC)

var PMTK_SET_NMEA_OUTPUT_RMC = "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29";

// GGA only

var PMTK_SET_NMEA_OUTPUT_GGA = "$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29";

// turn on RMC and GGA

var PMTK_SET_NMEA_OUTPUT_RMCGGA = "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28";

// turn on RMC, GGA and GSA

var PMTK_SET_NMEA_OUTPUT_RMCGGAGSA = "$PMTK314,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29";

// turn on ALL THE DATA

var PMTK_SET_NMEA_OUTPUT_ALLDATA = "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28";

// turn off output

var PMTK_SET_NMEA_OUTPUT_OFF = "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28";

// Standby

var PMTK_STANDBY = "$PMTK161,0*28";

//

var PMTK_Q_RELEASE = "$PMTK605*31";

// Ant off

var PMTK_ANT_OFF = "$PGCMD,33,0*6D";

// Active Interference Cancellation (AIC) Disable

var PMTK_AIC_OFF = "$PMTK286,0*22";

// Active Interference Cancellation (AIC) Enable

var PMTK_AIC_ON = "$PMTK286,1*23";

// DGPS Mode

var PMTK_DT_DGPS_MODE = "$PMTK401*37";

// Starting logging data to SD

var GPS_log = false;

function delay(milliseconds)

{

var startTime = Date.now();

while (Date.now() - startTime < milliseconds);

}

serialPort.on('open',function() {

console.log('Port open');

serialPort.write(PMTK_SET_NMEA_OUTPUT_ALLDATA + "\r\n"); // RMC and GGA

console.log(PMTK_SET_NMEA_OUTPUT_RMCGGAGSA);

delay(500);

serialPort.write(PMTK_DT_DGPS_MODE + "\r\n"); // DGPS

console.log(PMTK_DT_DGPS_MODE);

delay(500);

serialPort.write(PMTK_SET_NMEA_UPDATE_1HZ + "\r\n"); // 5Hz

console.log(PMTK_SET_NMEA_UPDATE_5HZ);

delay(500);

serialPort.write(PMTK_AIC_ON + "\r\n"); // AIC ON

console.log(PMTK_AIC_ON);

delay(500);

GPS_log = true;

});

/*

setInterval(function()

{

console.log('500ms' + '\n');

}, 500); // 50ms

*/

serialPort.on('data', function(data) {

var data_string = '' + data;

if (data_string.substring(0,6) == '$GPGGA') {

gpgga_string = data_string.split('\n')[0];

console.log('GGA data received: ' + data_string);

gprmc_string = gpgga_string.replace('*', ',');

gpgga_string = gpgga_string.replace('\n', '');

gps_datagga = gpgga_string.split(',');

var time_string = gps_datagga[1]; // Fix Time HH:MM:SS

var gps_time = new Date();

var gps_time_hours = parseInt(gps_datagga[1].substring(0,2));

var gps_time_minutes = parseInt(gps_datagga[1].substring(2,4));

var gps_time_seconds = parseInt(gps_datagga[1].substring(4,6));

gps_time.setHours(gps_time_hours);

gps_time.setMinutes(gps_time_minutes);

gps_time.setSeconds(gps_time_seconds);

gps_lat = gps_datagga[2]; // Latitude

gps_lat_ns = gps_datagga[3]; // Latitude

gps_lon = gps_datagga[4]; // Longitude

gps_lon_ew = gps_datagga[5]; // Longitude

gps_pos_fix = gps_datagga[6]; // Fix Quality 1-8

gps_satt_qty = gps_datagga[7]; // Number of satellites

gps_hdop = gps_datagga[8]; // Horizontal dilution of position

gps_msl_alt = gps_datagga[9]; // Altitude above sea level

gps_unit = gps_datagga[10]; // Meters

gps_geo = gps_datagga[11]; // Height of geoid above WGS84

gps_geo_unit = gps_datagga[12]; // Meters

gps_dgps = gps_datagga[13]; // DGPS laste update time

gps_dgps_id = gps_datagga[14]; // DGPS station ID number

gps_Checksum = gps_datagga[15]; // Checksum

if (gps_pos_fix == '1') {

ws.write(data_string + '\n');

console.log('GGA GPS Fixed');

console.log('Time: ', gps_time_hours + ':' + gps_time_minutes + ':' + gps_time_seconds);

console.log('Position: ' + gps_lat + gps_lat_ns + ',' + gps_lon + gps_lon_ew);

console.log('Altitude: ' + gps_msl_alt + 'M' + ' +/- ' + gps_hdop + ...

idata
Community Manager
52 Views

Hi Kim,

 

 

Please let us know what results you get once you try it out, you might save some code lines using String.Split. It's nice to know that you got it working already, I was looking at your code and I guess that the GSV and VTG () implementation is similar to the rest of them (GGA, GGL, etc.) so at the end it should work using your configuration or using the Split.String function.

 

 

Regards,

 

Pablo
Reply