gpsdev
Description
The gpsdev
System object™ connects to a GPS receiver connected to the host computer.
To connect to a GPS receiver:
Create the
gpsdev
object and set its properties.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
specifies properties using one or more name-value arguments. For example, gps
= gpsdev(___,Name=Value)gps =
gpsdev("COM4",SamplesPerRead=2)
sets samples per read to 2.
Input Arguments
port
— Serial port name
character vector | string scalar
Serial port name, specified as a character vector or string scalar. Use
serialportlist
to get a list of connected ports.
Example: "COM2"
serialobj
— purpose name
serialport
object
Serial port connection, specified as a serialport
object
Properties
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.
BaudRate
— Baud rate
9600
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.
ReadMode
— Specify which data samples to be returned
'latest'
(default) | 'oldest'
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, ifSamplesPerRead
= 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, ifSamplesPerRead
= 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
SamplesRead
— Samples read
double
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
SamplesAvailable
— Samples in the host buffer
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
SamplesPerRead
— Samples per read
1
(default)
Samples read from the first read
, specified as a positive integer
in the range [1 10].
Tunable: No
Data Types: double
OutputFormat
— Set output format
'timetable'
(default) | 'matrix'
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
orduration
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
TimeFormat
— Set the format of the time displayed when the GPS data is read
'datetime'
(default) | 'duration'
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. Thegpsdev
object gets locked either at the first call of theread
function after the object creation or at the first call of the read function after the execution of therelease
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)
flush | Flush all GPS data accumulated in the buffers and reset properties |
info | Read update rate, GPS lock information and number of satellites in view for the GPS receiver |
read | Read data from GPS receiver |
release | Release the GPS object |
writeBytes | Write raw commands to the GPS receiver |
Examples
Plot Geographic Position Using GPS Connected to Host Computer
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
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 NaN
s 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 GPS Receiver
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
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 GPS Receiver as Timetable
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
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
GPS Modules
To verify the functionality, the following GPS modules were used:
Adafruit Ultimate GPS
Ublox NEO 6M
Ublox NEO 7M
Version History
Introduced in R2020b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)