Main Content

gpsdev

Connect to a GPS receiver connected to host computer

Since R2020b

Description

The gpsdev System object™ connects to a GPS receiver connected to the host computer.

To connect to a GPS receiver:

  1. Create the gpsdev object and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects?

Creation

Description

example

gps = gpsdev(port) connects to a GPS Receiver at the specified serial port of host computer.

example

gps = gpsdev(serialobj) connects to a GPS Receiver specified by a serial object.

example

gps = gpsdev(___,Name,Value) connects to a GPS Receiver on the specified port or specified through a serial object, using one or more name-value pairs.

Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, see System Design in MATLAB Using System Objects.

This property is read-only.

The baud rate for serial communication. The baud rate is set at 9600 bits/sec. The GPS receiver must be configured to work at 9600 bits/sec. If your GPS receiver is configured to some other baud rate, reconfigure it to 9600 bits/sec to use gpsdev function.

Specify whether to return the latest or the oldest data samples. The number of samples depends on the SamplesPerRead value. The data read from the GPS receiver is stored in the MATLAB® buffer.

  • latest — Provides the latest data samples available in the buffer. All previous data samples in the buffer are discarded. For example, if SamplesPerRead = 3, the latest three data samples read by the GPS receiver are returned.

  • oldest — Provides the oldest data samples available in the buffer. In this case, no data samples are discarded. For example, if SamplesPerRead = 3, the first three data samples read are returned for the first read, the next three data samples are returned for the second read, and so on.

Tunable: No

Data Types: character vector | string

This property is read-only.

Number of samples read from the GPS receiver using the read function, after the object is locked. The gpsdev object gets locked either at the first call of the read function after the object creation or at the first call of the read function after the execution of the release function.

Data Types: double

This property is read-only.

Samples available in the host buffer. When you release the object, SamplesAvailable is set to 0.

Data Types: double

Samples read from the first read, specified as a positive integer in the range [1 10].

Tunable: No

Data Types: double

Set the output format of the data returned by executing the read function.

When the OutputFormat is set to timetable, the timetable returned has the following fields:

  • LLA (Latitude, Longitude, Altitude)

  • Ground Speed

  • Course over ground

  • Dilution of Precisions(DOPs), VDOP,HDOP,PDOP

  • GPS Receiver Time

  • Time — System time when the data is read, in datetime or duration format

When the OutputFormat is set to matrix, the data is returned as matrices of Time, LLA, Ground Speed, Course over ground, DOPs, and GPS receiver time. The units for the GPS receiver readings are the same as the timetable format.

Tunable: Yes

Data Types: character vector | string

Set the format of the time displayed when the GPS data is read.

  • datetime — Displays the date and time at which the data is read.

  • duration — Displays the time elapsed in seconds after the GPS object is locked. The gpsdev object gets locked either at the first call of the read function after the object creation or at the first call of the read function after the execution of the release function.

Tunable: Yes

Data Types: character vector | string

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:

release(obj)
flushFlush all GPS data accumulated in the buffers and reset properties
infoRead update rate, GPS lock information and number of satellites in view for the GPS receiver
readRead data from GPS receiver
releaseRelease the GPS object
writeBytesWrite raw commands to the GPS receiver

Examples

collapse all

Get the geographic location using the GPS receiver connected to the host computer on a specific serial port and plot the location in a map.

Required Hardware

To run this example, you need:

  • UBlox Neo-6M GPS module

  • GPS antenna

  • USB to UART module

  • USB cable

  • Connecting wires

Hardware Connection

GPS_UART_USB_PC.png

Connect the pins on the UBlox Neo-6M GPS module to the pins on your USB to UART module. The connections are:

  • VCC - +5V

  • RX - TXO

  • TX - RXI

  • GND - GND

Connect the GPS antenna to the GPS module. Connect the USB to UART module to the host computer with a USB cable. GPS Fix can be easily acquired in locations that have a clear view of the sky. Wait for the GPS module to acquire satelite signals (Fix).This can be verified by checking the Fix LED (D1) of your GPS module.

Create GPS Object

Create a gpsdev object for the GPS module connected to a specific port.

gps = gpsdev('COM4')
gps = 
  gpsdev with properties:

                         SerialPort: COM4	
                           BaudRate: 9600 (bits/s)

                     SamplesPerRead: 1	 
                           ReadMode: "latest"	 
                        SamplesRead: 0	 
Show all properties all functions

Read the GPS data

Read the GPS data and extract latitude, longitude, and time from it. GPS returns UTC datetime. Convert it to system time zone.

[gpsData,~] = read(gps);
latitude = gpsData.LLA(1);
longitude = gpsData.LLA(2);
gpsTime = gpsData.GPSReceiverTime;
gpsTime.TimeZone = 'local';

Plot the position in a map along with the timestamp

Plot the position in geographic axes with the data obtained from the GPS module. GPS should have fix to get valid values for latitude, longitude and gpsTime.

If the GPS module does not have fix, the above commands give NaNs for latitude and longitude and NaT for gpsTime. In this case, make sure the antenna is exposed to clear sky and wait for some time and try the above steps again.

if(~isnan(latitude) && ~isnan(longitude))
 % plot the position in geographic coordinates
 fig = geoplot(latitude,longitude,'Marker',"o",'MarkerSize',6,'Color','red','MarkerFaceColor','red');

 % Sets the latitude and longitude limits of the base Map   
 geolimits([latitude-0.05 latitude+0.05],[longitude-0.05 longitude+0.05]) ;

 % Selects the basemap   
 geobasemap streets;
 timeString = strcat("Timestamp: ",string(gpsTime));

 % Create annotation and display time received from GPS
 annotation('textbox',[0.005 0.98 0.6 0.01],'FitBoxToText','on','string',timeString,'Color','blue','FontSize',10);
end

Clean Up

When the connection is no longer needed, clear the associated object.

delete(gps);
clear gps;

Write configuration commands to the GPS receiver connected to the host computer using serialport object.

Required Hardware

To run this example, you need:

  • UBlox Neo-6M GPS module

  • GPS antenna

  • USB to UART module

  • USB cable

  • Connecting wires

Hardware Connection

GPS_UART_USB_PC.png

Connect the pins on the UBlox Neo-6M GPS module to the pins on your USB to UART module. The connections are:

  • VCC - +5V

  • RX - TXO

  • TX - RXI

  • GND - GND

Connect the GPS antenna to the GPS module. Connect the USB to UART module to the host computer with a USB cable. GPS Fix can be easily acquired in locations that have a clear view of the sky. Wait for the GPS module to acquire satelite signals (Fix).This can be verified by checking the Fix LED (D1) of your GPS module.

Create GPS Object

Connect to the GPS receiver using serialport object. Specify the port name and the baud rate.

s = serialport('COM4',9600)
s = 
  Serialport with properties:

                 Port: "COM4"
             BaudRate: 9600
    NumBytesAvailable: 0

  Show all properties, functions

gps = gpsdev(s)
gps = 
  gpsdev with properties:

                         SerialPort: COM4	
                           BaudRate: 9600 (bits/s)

                     SamplesPerRead: 1	 
                           ReadMode: "latest"	 
                        SamplesRead: 0	 
Show all properties all functions

Write Configuration Commands

In the default configuration the GPS receiver returns the following NMEA messages: GPRMC, GPVTG, GPGGA, GPGSA, GPGSV, and GPGLL. The receiver can be configured to have a user defined set of output messages.

Read few lines of default messages from the serial port the GPS receiver is connected.

for i = 1:10    
data = readline(s);
disp(data);
end
$GPRMC,,V,,,,,,,,,,N*53
$GPVTG,,,,,,,,,N*30
$GPGGA,,,,,,0,00,99.99,,,,,,*48
$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
$GPGSV,2,1,08,01,,,18,08,,,12,09,,,12,15,,,19*77
$GPGSV,2,2,08,23,,,13,24,,,09,25,,,10,27,,,25*79
$GPGLL,,,,,,V,N*64
$GPRMC,,V,,,,,,,,,,N*53
$GPVTG,,,,,,,,,N*30
$GPGGA,,,,,,0,00,99.99,,,,,,*48

Write the version monitor command to the GPS receiver to return the software and hardware version of the GPS receiver.

configCMD = [0xB5 0x62 0x0A 0x04 0x00 0x00 0x0E 0x34];
% writeBytes(gps,cfg)
write(s,configCMD,'uint8')

Read few lines of messages again to verify the version message.

for i = 1:10    
data = readline(s);
disp(data);
end
$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
$GPGSV,2,1,05,01,,,13,09,,,11,15,,,16,23,,,12*74
$GPGSV,2,2,05,25,,,10*7A
$GPGLL,,,,,,V,N*64
µb
( 7.03 (45969)                  00040007  °$GPRMC,,V,,,,,,,,,,N*53
$GPVTG,,,,,,,,,N*30
$GPGGA,,,,,,0,00,99.99,,,,,,*48
$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
$GPGSV,2,1,06,01,,,11,09,,,11,23,,,14,24,,,21*75

It can be observed from the output, 7.03 (45969) is the software version and 00040007 is the hardware version.

Clean Up

When the connection is no longer needed, clear the associated object.

delete(gps);
clear gps;
clear s;

Read data from the GPS receiver connected to the host computer on a specific serial port.

Required Hardware

To run this example, you need:

  • UBlox Neo-6M GPS module

  • GPS antenna

  • USB to UART module

  • USB cable

  • Connecting wires

Hardware Connection

GPS_UART_USB_PC.png

Connect the pins on the UBlox Neo-6M GPS module to the pins on your USB to UART module. The connections are:

  • VCC - +5V

  • RX - TXO

  • TX - RXI

  • GND - GND

Connect the GPS antenna to the GPS module. Connect the USB to UART module to the host computer with a USB cable. GPS Fix can be easily acquired in locations that have a clear view of the sky. Wait for the GPS module to acquire satelite signals (Fix).This can be verified by checking the Fix LED (D1) of your GPS module.

Create GPS Object

Create a gpsdev object for the GPS receiver connected to a specific port. Specify the output format of the data as a timetable.

gps = gpsdev('COM4','OutputFormat',"timetable")
gps = 
  gpsdev with properties:

                         SerialPort: COM4	
                           BaudRate: 9600 (bits/s)

                     SamplesPerRead: 1	 
                           ReadMode: "latest"	 
                        SamplesRead: 0	 
Show all properties all functions

Read the GPS data

Read the GPS data and return them as a timetable.

[tt,overruns] = read(gps)
tt=1×5 timetable
              Time                         LLA               GroundSpeed    Course            DOPs                GPSReceiverTime     
    ________________________    _________________________    ___________    ______    ____________________    ________________________

    22-Mar-2021 15:31:15.190    17.47    78.343     449.6      0.25619       NaN      9.31    1.48    9.19    22-Mar-2021 10:01:14.000

overruns = 0

Display number of samples read and the samples available in the host buffer.

gps.SamplesRead
ans = 1
gps.SamplesAvailable
ans = 0

Release the GPS object to configure the non tunable properties. The release function also clears the buffer and resets the SamplesRead and SamplesAvailable properties.

release(gps)

Specify the number of samples per read to 2. Read the GPS data.

gps.SamplesPerRead = 2;
read(gps)
ans=2×5 timetable
              Time                         LLA               GroundSpeed    Course            DOPs                GPSReceiverTime     
    ________________________    _________________________    ___________    ______    ____________________    ________________________

    22-Mar-2021 15:31:17.178    17.47    78.343       450     0.063791       NaN      9.32    1.48     9.2    22-Mar-2021 10:01:16.000
    22-Mar-2021 15:31:17.178    17.47    78.343       450     0.063791       NaN      9.32    1.48     9.2    22-Mar-2021 10:01:16.000

Display number of samples read and the samples available in the host buffer.

gps.SamplesRead
ans = 1
gps.SamplesAvailable
ans = 0

Clean Up

When the connection is no longer needed, clear the associated object.

delete(gps);
clear gps;

More About

expand all

Version History

Introduced in R2020b

See Also