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
9867 Discussions

JavaScript serialPort.write help needed

KSels
New Contributor II
1,022 Views

I am working on an GPS project and need to serial write some data to the GPS module with 100-200ms delay.

How do I add delay before every code sendt to the GPS module?

Here is my code.

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

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

u = new m.Uart(0); // /dev/ttyMFD1

console.log(u.getDevicePath());

var SerialPort = require("serialport").SerialPort; // npm install serialport

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

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"

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

console.log('GET GPS data');

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

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

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

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

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[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 == '0') {

console.log('No GPS Fix');

}

}

});

});

I want to add a delay between every serialPort.write:

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

//-- add 100-200ms delay here --

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

//-- add 100-200ms delay here --

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

//-- add 100-200ms delay here --

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

//-- add 100-200ms delay here --

1 Solution
KSels
New Contributor II
142 Views

I got the delay function to work.

I added this code to the js.

function delay(milliseconds)

{

var startTime = Date.now();

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

}

At start of SerialPort.on open I added.

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

delay(200); // 200ms

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

delay(200); // 200ms

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

delay(200); // 200ms

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

delay(200); // 200ms

The new code is:

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

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

u = new m.Uart(0); // /dev/ttyMFD1

console.log(u.getDevicePath());

var SerialPort = require("serialport").SerialPort; // npm install serialport

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

baudrate: 9600

});

function delay(milliseconds)

{

var startTime = Date.now();

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

}

// 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...

View solution in original post

2 Replies
Pedro_M_Intel
Employee
142 Views

Hello KimLorentz,

I suggest you to take a look at the https://github.com/intel-iot-devkit/mraa/blob/master/examples/javascript/Blink-IO.js Blink-IO.js mraa example and check how they make a delay. I believe it may be of help for you.

Peter.

KSels
New Contributor II
143 Views

I got the delay function to work.

I added this code to the js.

function delay(milliseconds)

{

var startTime = Date.now();

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

}

At start of SerialPort.on open I added.

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

delay(200); // 200ms

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

delay(200); // 200ms

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

delay(200); // 200ms

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

delay(200); // 200ms

The new code is:

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

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

u = new m.Uart(0); // /dev/ttyMFD1

console.log(u.getDevicePath());

var SerialPort = require("serialport").SerialPort; // npm install serialport

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

baudrate: 9600

});

function delay(milliseconds)

{

var startTime = Date.now();

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

}

// 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...

View solution in original post

Reply