This example shows you how to read the data from a GPS receiver connected to a computer, and parse the National Marine Electronics Association(NMEA) data.
National Marine Electronics Association (NMEA) format is a specification that defines how data is transmitted between various marine electronic devices. Most of the GPS receiver output data in the NMEA format.
The data is transmitted in a sequence called a sentence. Each sentence is independent of the other sentences from the receiver. Sentences contain information such as latitude, longitude, speed, and time as ASCII characters. The data in a sentence are comma separated. The data is output on the serial port of the receiver at an interval called the Update Rate
.
The parser implemented in this example can be used to parse RMC, CGA, GSA, and VTG sentences. The message format for each of these sentences are explained below. The message formats are based on the protocol specification mentioned in the u-blox 6 Receiver Description.
Required Hardware
Ublox NEO-6M GPS Module
Serial to USB signal converters like FTDI (If the GPS module cannot be directly connected to computer using USB)
If a USB connector is available on the GPS receiver, connect the GPS module directly to the computer via USB Cable and install the required drivers. Otherwise, use serial to USB converters, to convert the serial transmissions to USB signals.
Create a gpsReceiver
object and associate it with the serial port on which GPS module is connected. The serial data from the GPS module follows 9600 8N1 format.
ublox_gps = gpsReceiver('COM17');
GPS outputs data in multiple sentences which follows NMEA format. The read
method of the gpsReceiver
class returns frames of raw GPS data. The start of each frame is assumed as an RMC sentence and the end of a frame is assumed to be the start of the next RMC sentence. The number of frames returned can be specified as an argument to the read
function. By default, the function returns one frame of NMEA data. Make sure the Ublox-6M module is configured to its default state, where the Baud Rate
is 9600, Update Rate
is 1 hz and the module outputs the messages corresponding to RMC, GSA, GGA, and VTG IDs.
rawGPSData = read(ublox_gps);
For multiple number of frames, specify the number of NMEA frames as an argument in the read
function.
NumOfFrames = 3; rawGPSDataWithFrames = read(ublox_gps, NumOfFrames);
The HelperParseGPSData
function in the example folder can be used to parse the GPS Data. The implemented parser is capable of decoding RMC, GGA, GSA, and VTG Sentences. The parser function returns a structure whose fields correspond to the data decoded from the corresponding sentence. If the receiver does not output a value, the corresponding checksum field in the structure will be empty. The ChecksumValidity
field determines the validity of the received data.
The below code can be used to parse for RMC, GGA, GSA, and VTG sentences.
[GPRMC, GPGGA, GPVTG, GPGSA] = HelperParseGPSData(rawGPSData);
If there are multiple NMEA sentences available in the input raw GPS Data, an array of structures is returned as output. The size of the array will be the number of available NMEA sentences corresponding to the message IDs.
If an NMEA sentence corresponding to a message ID is not available in the input data, the output structure corresponding to that message ID will return null fields except for MessageID field in the structure. These outputs can be suppressed using tilde operator.
GPSData = '$GPGGA,135015.00,1257.62410,N,07742.00316,E,1,06,1.56,903.3,M,-86.4,M,,*77';
[~,GPGGA,~,~] = HelperParseGPSData(GPSData);
The flush
method of the object can be used to flush the input buffers associated with the gpsReceiver
object.
flush(ublox_gps);
When the connection is no longer required, disconnect and delete the object.
delete(ublox_gps);
To get help on parser function, use
help HelperParseGPSData;
To get help on gpsReceiver object, use
help gpsReceiver;