3 Replies Latest reply on May 24, 2016 11:03 AM by Intel Corporation

    Read multiple strings from serial port.

    KimLorentz

      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');
            }
      */
        });
      });
      
        • 1. Re: Read multiple strings from serial port.
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          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, it separates words using commas, colons, periods and tabs.

          Regards,
          Pablo

          • 2. Re: Read multiple strings from serial port.
            KimLorentz

            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 + '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' + ' +/- ' + gps_hdop + 'M');
                          console.log('Satellites Qty: ' + gps_satt_qty + '\n');
                      }
                      
                      if (gps_pos_fix == '0') {
                          console.log('No GPS Fix' + '\n');
                      }
                     
                 }
                
            
                  if (data_string.substring(0,6) == '$GPGLL') {
                      gpgll_string = data_string.split('\n')[0];
                      console.log('GLL data received: ' + data_string);
                      gpgll_string = gpgll_string.replace('*', ',');
                      gpgll_string = gpgll_string.replace('\n', '');
                      gps_datagll = gpgll_string.split(',');
                      gps_lat = gps_datagll[1];
                      gps_lat_ns = gps_datagll[2];
                      gps_lon = gps_datagll[3];
                      gps_lon_ew = gps_datagll[4];
                      var time_string = gps_datagll[5];                                    // Fix Time HH:MM:SS:MS
                      var gps_fix_time = new Date();  
                      var gps_fix_time_hours = parseInt(gps_datagll[5].substring(0,2));  
                      var gps_fix_time_minutes = parseInt(gps_datagll[5].substring(2,4));  
                      var gps_fix_time_seconds = parseInt(gps_datagll[5].substring(4,6));  
                      gps_fix_time.setHours(gps_fix_time_hours);  
                      gps_fix_time.setMinutes(gps_fix_time_minutes);  
                      gps_fix_time.setSeconds(gps_fix_time_seconds); 
                      gps_AV = gps_datagll[6];
                      
                      console.log('GPS Fix Time: ' + gps_fix_time_hours + ':' + gps_fix_time_minutes + ':' + gps_fix_time_seconds);
                      console.log('Position: ' + gps_lat + gps_lat_ns + ',' + gps_lon + gps_lon_ew + '\n');
                      
                  }
                    
                  if (data_string.substring(0,6) == '$GPGSA') {
                      gpgsa_string = data_string.split('\n')[0];
                      console.log('GSA data received: ' + data_string);
                      gpgsa_string = gpgsa_string.replace('*', ','); 
                      gpgsa_string = gpgsa_string.replace('\n', '');
                      gps_datagsa = gpgsa_string.split(',');   
                      gps_automan = gps_datagsa[1];                                     // Auto selection of 2D or 3D, A=auto, M=manual
                      gps_2D3D = gps_datagsa[2];                                        // 3D fix - Values, 1-3
                      gps_PRN1 = gps_datagsa[3];                                        // PRNs of satellites used for fix 1of12
                      gps_PRN2 = gps_datagsa[4];                                        // PRNs of satellites used for fix 2of12
                      gps_PRN3 = gps_datagsa[5];                                        // PRNs of satellites used for fix 3of12
                      gps_PRN4 = gps_datagsa[6];                                        // PRNs of satellites used for fix 4of12
                      gps_PRN5 = gps_datagsa[7];                                        // PRNs of satellites used for fix 5of12
                      gps_PRN6 = gps_datagsa[8];                                        // PRNs of satellites used for fix 6of12
                      gps_PRN7 = gps_datagsa[9];                                        // PRNs of satellites used for fix 7of12
                      gps_PRN8 = gps_datagsa[10];                                       // PRNs of satellites used for fix 8of12
                      gps_PRN9 = gps_datagsa[11];                                       // PRNs of satellites used for fix 9of12
                      gps_PRN10 = gps_datagsa[12];                                      // PRNs of satellites used for fix 10of12
                      gps_PRN11 = gps_datagsa[13];                                      // PRNs of satellites used for fix 11of12
                      gps_PRN12 = gps_datagsa[14];                                      // PRNs of satellites used for fix 12of12
                      gps_PDOP = gps_datagsa[15];                                       // PDOP (dilution of precision)
                      gps_HDOP = gps_datagsa[16];                                       // HDOP (horizontal of precision)
                      gps_VDOP = gps_datagsa[17];                                       // VDOP (vertival og precision)
                      gps_Checksum = gps_datagsa[18];                                   // Checksum
                        
                      if (gps_2D3D == '1') {  
                          console.log('No GPS Fix' + '\n');   
                      }  
                        
                      if (gps_2D3D == '2') {    
                          ws.write(data_string + '\n');
                          console.log('GSA 2D Fix');
                          if(gps_automan == 'A')
                          {
                              console.log('Fix Mode : Auto' + '\n')
                          }
                          if(gps_automan == 'M')
                          {
                              console.log('Fix Mode : Manual' + '\n')
                          }
                      }  
                      
                      if (gps_2D3D == '3') {    
                          ws.write(data_string + '\n');
                          console.log('GSA 3D Fix');
                          console.log('Dilution of precision: ' + gps_PDOP + 'M');
                          console.log('Horizontal of precision: ' + gps_HDOP + 'M');
                          console.log('Vertival og precision: ' + gps_VDOP + 'M');
                          if(gps_automan == 'A')
                          {
                              console.log('Fix Mode : Auto' + '\n')
                          }
                          if(gps_automan == 'M')
                          {
                              console.log('Fix Mode : Manual' + '\n')
                          }
                      }
                      
                 }
                
                  if (data_string.substring(0,6) == '$GPRMC') {
                      gprmc_string = data_string.split('\n')[0]; 
                      console.log('RMC data received: ' + data_string);
                      gprmc_string = gprmc_string.replace('*', ',');
                      gprmc_string = gprmc_string.replace('\n', '');  
                      gps_datarmc = gprmc_string.split(',');    
                      var time_string = gps_datarmc[1];                                    // Fix Time HH:MM:SS:MS
                      var gps_time = new Date();  
                      var gps_time_hours = parseInt(gps_datarmc[1].substring(0,2));  
                      var gps_time_minutes = parseInt(gps_datarmc[1].substring(2,4));  
                      var gps_time_seconds = parseInt(gps_datarmc[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_datarmc[2];                                        // Status, A=active, V=Void
                      gps_lat = gps_datarmc[3];                                            // Latitude
                      gps_lat_ns = gps_datarmc[4];                                         // Latitude
                      gps_lon = gps_datarmc[5];                                            // Longitude
                      gps_lon_ew = gps_datarmc[6];                                         // Longitude
                      gps_speed = gps_datarmc[7];                                          // Speed in knots
                      gps_track_angle = gps_datarmc[8];                                    // Track angle  in degrees True
                      gps_date = gps_datarmc[9];                                           // Date, DD:MM:YY
                       var gps_rmc_day = parseInt(gps_datarmc[9].substring(0,2));
                       var gps_rmc_month = parseInt(gps_datarmc[9].substring(2,4));
                       var gps_rmc_year = parseInt(gps_datarmc[9].substring(4,6));
                      gps_mag = gps_datarmc[10];                                           // Magnetic Variation
                      gps_checksum = gps_datarmc[11];                                      // Checksum data
                        
                      if (gps_pos_fix == 'V') {  
                          console.log('No GPS Fix' + '\n');   
                      }  
                        
                      if (gps_pos_fix == 'A') {
                          ws.write(data_string + '\n');
                          console.log('RMC GPS Fix');  
                          console.log('GPS Time: ' + gps_time_hours + ':' + gps_time_minutes + ':' + gps_time_seconds);
                          console.log('Date: ' + gps_rmc_day + '/' + gps_rmc_month + '/' + gps_rmc_year);
                          console.log('Position: ' + gps_lat + gps_lat_ns + ',' + gps_lon + gps_lon_ew + '\n');
                      }  
                      
                 } 
            });
            
            • 3. Re: Read multiple strings from serial port.
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              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