Intel® Makers
Intel® Edison, Intel® Joule™, Intel® Curie™, Intel® Galileo
Announcements
Welcome - This is a Peer-to-Peer Forum only. Intel has discontinued these products but you may find support from other customers on this Forum
9868 Discussions

Need help reading SerialPort for javaScript

KSels
New Contributor II
1,783 Views

I got my starter code for Serial write to work, but now I want to read SerialPort.

How do I read several strings at the same time? like the GPS sends.

Demo code from the GPS.

$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47

$GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39

$GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75

$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A

$GPGLL,4916.45,N,12311.12,W,225444,A,*1D

$GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48

I want to get the GGA and RMC. How do I read these data only from the list at top?

Here is my GGA code (under), but I cant get the GGA and RMC data to work at the same time.

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

var data_string = '' + data;

console.log(data_string);

ws.write(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[4];

gps_lon_ew = gps_data[5];

gps_pos_fix = gps_data[6];

gps_satt_qty = gps_data[7];

gps_hdop = gps_data[8];

gps_msl_alt = gps_data[9];

gps_unit = gps_data[10];

gps_geo = gps_data[11];

gps_geo_unit = gps_data[12];

if (gps_pos_fix == '1') {

console.log('GPS Fixed');

console.log('Time: ', gps_time);

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

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

console.log('Satellites Qty: ' + gps_satt_qty + '\n');

}

if (gps_pos_fix == '2') {

console.log('DGPS Fixed');

console.log('Time: ', gps_time);

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

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

console.log('Satellites Qty: ' + gps_satt_qty + '\n');

}

if (gps_pos_fix == '0') {

console.log('No GPS Fix');

}

}

});

3 Replies
Carlos_M_Intel
Employee
136 Views

Hi Kim,

If you are going to use Serial Communication you can't read more than one value at a time, this is because there is only one RX line available so the system will be receiving the information continuously and not in parallel.

You will need to create a routine that will read first GCA and then RMC.

Regards,

Charlie

KSels
New Contributor II
136 Views

Looks like I got it working. It gives me the data I need right now. But time will tell when the javaScript gets bigger.

var com = require("serialport");

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

baudrate: 9600,

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

});

// 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";

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_RMCGGA + "\r\n"); // RMC and GGA

console.log(PMTK_SET_NMEA_OUTPUT_RMCGGA);

delay(200);

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

console.log(PMTK_DT_DGPS_MODE);

delay(200);

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

console.log(PMTK_SET_NMEA_UPDATE_5HZ);

delay(200);

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

console.log(PMTK_AIC_ON);

delay(200);

});

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

//console.log(data);

var data_string = '' + data;

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

//console.log('GGA found');

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

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

gps_data = gpgga_string.split(',');

console.log('GGA 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[4];

gps_lon_ew = gps_data[5];

gps_pos_fix = gps_data[6];

gps_satt_qty = gps_data[7];

gps_hdop = gps_data[8];

gps_msl_alt = gps_data[9];

gps_unit = gps_data[10];

gps_geo = gps_data[11];

gps_geo_unit = gps_data[12];

if (gps_pos_fix == '1') {

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 + 'M');

console.log('Satellites Qty: ' + gps_satt_qty + '\n');

}

if (gps_pos_fix == '2') {

console.log('DGPS Fixed');

console.log('Time: ', gps_time);

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

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

console.log('Satellites Qty: ' + gps_satt_qty + '\n');

}

if (gps_pos_fix == '0') {

console.log('No GPS Fix');

}

}

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

//console.log('RMC found');

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

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

gps_data = gprmc_string.split(',');

console.log('RMC 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_pos_fix = gps_data[2];

gps_lat = gps_data[3];

gps_lat_ns = gps_data[4];

gps_lon = gps_data[5];

gps_lon_ew = gps_data[6];

gps_speed = gps_data[7];

gps_track_...

KSels
New Contributor II
136 Views

I added GSA and SD card logging also to my GPS script. Still looking good.

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

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

var com = require("serialport");

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');

// 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_RMCGGAGSA + "\r\n"); // RMC, GGA and GSA

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"); // 1Hz

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;

});

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

//console.log(data);

var data_string = '' + data;

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

//console.log('GGA found');

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

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

gps_data = gpgga_string.split(',');

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

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

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]; // Latitude

gps_lat_ns = gps_data[3]; // Latitude

gps_lon = gps_data[4]; // Longitude

gps_lon_ew = gps_data[5]; // Longitude

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

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

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

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

gps_unit = gps_data[10]; // Meters

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

gps_geo_unit = gps_data[12]; // Meters

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

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

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 + 'M');

console.log('Satellites Qty: ' + gps_satt_qty + '\n');

}

if (gps_pos_fix == '2') {

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

console.log('DGPS Fixed');

console.log('Time: ', gps_time);

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

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

Reply