gpsdev
连接到GPS接收器连接到主机第一版er
Description
Thegpsdev
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, seeWhat Are System Objects?
Creation
Description
connects to a GPS Receiver at the specified serial port of host computer.gps
= gpsdev(port
)
connects to a GPS Receiver specified by a serial object.gps
= gpsdev(serialobj
)
connects to a GPS Receiver on the specified port or specified through a serial object, using one or more name-value pairs.gps
= gpsdev(___,Name,Value)
Properties
Unless otherwise indicated, properties arenontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and therelease
function 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.
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 usegpsdev
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 theSamplesPerRead
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.
可调:No
Data Types:character vector
|string
SamplesRead
—Samples read
double
This property is read-only.
Number of samples read from the GPS receiver using theread
function, after the 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.
Data Types:double
SamplesAvailable
—Samples in the host buffer
double
This property is read-only.
Samples available in the host buffer. When yourelease
the object,SamplesAvailable
is set to 0.
Data Types:double
SamplesPerRead
—Samples per read
1
(default)
Samples read from the firstread
, specified as a positive integer in the range [1 10].
可调:No
Data Types:double
OutputFormat
—Set output format
'timetable'
(default) |'matrix'
Set the output format of the data returned by executing theread
function.
When theOutputFormat
is set totimetable
,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 theOutputFormat
is 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 thetimetable
format.
可调: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.
可调: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
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 agpsdev
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 andgpsTime
.
If the GPS module does not have fix, the above commands giveNaN
s for latitude and longitude andNaT
forgpsTime
. 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 GPS Receiver
Write configuration commands to the GPS receiver connected to the host computer usingserialport
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 usingserialport
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.
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 and00040007
is the hardware version.
Clean Up
When the connection is no longer needed, clear the associated object.
delete(gps); cleargps; clears;
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 agpsdev
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 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 theSamplesRead
andSamplesAvailable
properties.
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
GPS Modules
To verify the functionality, the following GPS modules were used:
Adafruit Ultimate GPS
Ublox NEO 6M
Ublox NEO 7M
版本历史
See Also
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)