Main Content

gpsdev

连接到GPS接收器连接到主机第一版er

Description

ThegpsdevSystem object™ connects to a GPS receiver connected to the host computer.

To connect to a GPS receiver:

  1. Create thegpsdevobject and set its properties.

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

To learn more about how System objects work, seeWhat 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 arenontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and thereleasefunction unlocks them.

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

For more information on changing property values, seeSystem 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 usegpsdevfunction.

Specify whether to return the latest or the oldest data samples. The number of samples depends on theSamplesPerReadvalue. 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.

可调:No

Data Types:character vector|string

This property is read-only.

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

Data Types:double

This property is read-only.

Samples available in the host buffer. When youreleasethe object,SamplesAvailableis set to 0.

Data Types:double

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

可调:No

Data Types:double

Set the output format of the data returned by executing thereadfunction.

When theOutputFormatis set totimetable,timetablereturned 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, indatetimeordurationformat

When theOutputFormatis set tomatrix,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 thetimetableformat.

可调: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. Thegpsdevobject gets locked either at the first call of thereadfunction after the object creation or at the first call of the read function after the execution of thereleasefunction.

可调: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 namedobj, 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

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

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 agpsdevobject 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 andgpsTime.

If the GPS module does not have fix, the above commands giveNaNs for latitude and longitude andNaTforgpsTime. 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 coordinatesfig = geoplot(latitude,longitude,'Marker',"o",'MarkerSize',6,'Color',“红色”,'MarkerFaceColor',“红色”);% Sets the latitude and longitude limits of the base Mapgeolimits([latitude-0.05 latitude+0.05],[longitude-0.05 longitude+0.05]) ;% Selects the basemapgeobasemapstreets; timeString = strcat("Timestamp: ",string(gpsTime));% Create annotation and display time received from GPSannotation('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); cleargps;

Write configuration commands to the GPS receiver connected to the host computer usingserialportobject.

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 usingserialportobject. 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.

fori = 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

编写监控命令GPS recei版本ver 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.

fori = 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 and00040007is the hardware version.

Clean Up

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

delete(gps); cleargps; clears;

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 agpsdevobject 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 timetableTime 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 theSamplesReadandSamplesAvailableproperties.

release(gps)

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

gps.SamplesPerRead = 2; read(gps)
ans=2×5 timetableTime 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); cleargps;

More About

expand all

版本历史

Introduced in R2020b

See Also