Main Content


Write raw commands to the GPS receiver

Since R2020b



writeBytes(gps,cmdArray) writes raw commands specified by cmdArray to configure the GPS module.


collapse all

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);

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)

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

for i = 1:10    
data = readline(s);
( 7.03 (45969)                  00040007  °$GPRMC,,V,,,,,,,,,,N*53

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.

clear gps;
clear s;

Input Arguments

collapse all

The GPS sensor, specified as a gpsdev object.

Raw command to configure the GPS module, specified as an hexadecimal array.

Example: [0xB5 0x62 0x06 0x01 0x08 0x00 0xF0 0x08 0x00 0x01 0x00 0x00 0x00 0x00 0x08 0x60]

Data Types: uint8

Version History

Introduced in R2020b

See Also