Fully commented and working NMEA parsing code
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 //Michael Antonucci 00002 //Last modified Oct. 27, 2022 00003 //Assignment 4, Exercise 1: Create a working code to parse NMEA inputs into human-readable data 00004 #include "mbed.h" 00005 00006 Serial pc(USBTX, USBRX); 00007 00008 char dataque[100]; //calls for an array of 100 characters long with the name "dataque" 00009 char header[5]; //calls for an array of 5 characters long with name "header" 00010 float lat; //creates float "lat" for latitude 00011 float longi; //creates float "longi" for longitude 00012 char ns[1]; //creates array of 1 character with name "ns" 00013 char ew[1]; //creates array of 1 character with name "ew" 00014 float elevation; //creates float "elevation" 00015 char checksum[2]; //calls for an array of 2 characters long with name "checksum" 00016 int seconds; //creates integer "seconds" for time 00017 float xaccel; //creates float "xaccel" for x acceleration 00018 float yaccel; //creates float "yaccel" for y acceleration 00019 float zaccel; //creates float "zaccel" for z acceleration 00020 float temp; //creates float "temp" for temperature 00021 int main() { //starts main code 00022 while(1) { 00023 if (pc.readable()) { 00024 pc.scanf("%s",dataque); 00025 sscanf(dataque,"$%5s",header); 00026 if (!strcmp(header,"GPRMC")) { //if the header is "GPRMC" 00027 sscanf(dataque,"$%5s,%d,%f,%1s,%f,%1s,%f,*%2s", //reassigns variables to data 00028 header,&seconds,&lat,ns,&longi,ew,&elevation,checksum); 00029 pc.printf("GPS NMEA string recieved:\r\n"); //prints type of message 00030 pc.printf("Time: %d seconds\r\n",seconds); //prints time 00031 pc.printf("Latitude: %f degrees %s",lat,ns); //prints latitude 00032 pc.printf("Longitude: %f degrees %s\r\n",longi,ew); //prints longitude 00033 pc.printf("Elevation: %f m\r\n",elevation); //prints elevation 00034 pc.printf("Checksum Value: %s",checksum); //prints checksum value 00035 } 00036 else if (!strcmp(header,"IMUPO")) { //if the header is "IMUPO", print a message 00037 sscanf(dataque,"$%5s,%d,%f,%f,%f,*%2s", //reassigns variables to data 00038 header, 00039 &seconds, 00040 &xaccel, 00041 &yaccel, 00042 &zaccel, 00043 checksum); 00044 pc.printf("Motion Message NMEA string recieved:\r\n"); //prints type of data 00045 pc.printf("Time: %d seconds\r\n",seconds); //prints time 00046 pc.printf("X-Acceleration: %f m/s^2 \r\n",xaccel); //prints x acceleration 00047 pc.printf("Y-Acceleration: %f m/s^2 \r\n",yaccel); //prints y acceleration 00048 pc.printf("Z-Acceleration: %f m/s^2 \r\n",zaccel); //prints z acceleration 00049 pc.printf("Checksum Value: %2s \r\n",checksum); //prints checksum value 00050 } 00051 else if (!strcmp(header,"TEMPE")) { //if the header is "TEMPE", print a message 00052 sscanf(dataque,"$%5s,%d,%f,*%2s", //reassigns variables to data 00053 header, 00054 &seconds, 00055 &temp, 00056 checksum); 00057 pc.printf("Temperature NMEA string recieved:\r\n"); //prints type of data 00058 pc.printf("Time: %d seconds\r\n",seconds); //prints time 00059 pc.printf("Temperature: %f degrees Celcius\r\n",temp); //prints temperature 00060 pc.printf("Checksum Value: %2s \r\n",checksum); //prints checksum value 00061 } 00062 else { //if the header is not one of those three, respond with an error message 00063 pc.printf("NOT A VALID NMEA INDICATOR \r\n"); //shows that error is associated with header format 00064 } 00065 } 00066 } 00067 }
Generated on Fri Oct 28 2022 22:17:00 by
1.7.2