connect
R2026bConnect MAVLink client to UDP or serial port
Description
connects the MAVLink client connectionName = connect(mavlink,type)mavlink using the connection type specified
by type.
specifies one or more options using name-value arguments in addition to the input arguments from the previous syntax.
For example, connectionName = connect(___,Name=Value)LocalPort=50791 specifies to connect the MAVLink client to UDP port 50791.
Examples
Connect a local MAVLink client to a serial port and establish communication with a Pixhawk® 4 board running PX4® firmware by exchanging heartbeat messages.
Create a mavlinkdialect
object using the common.xml file.
dialect = mavlinkdialect("common.xml");Create a local MAVLink client by using the mavlinkio
object.
gcs = mavlinkio(dialect);
Store the remote MAVLink client information by using the mavlinkclient
object. This code stores information for a Pixhawk board with a system ID of 1 and a component ID of 1.
uav = mavlinkclient(gcs,1,1)
uav =
mavlinkclient with properties:
SystemID: 1
ComponentID: 1
ComponentType: "Unknown"
AutopilotType: "Unknown"Connect the local MAVLink client to the same serial port as the Pixhawk board by using the connect
function. In this code, the Pixhawk board uses the "COM5" port.
connect(gcs,"Serial",SerialPort="COM5");
Create a heartbeat microservice object.
heartbeat = mavlinkmicroservice(gcs,"heartbeat");Start sending heartbeat messages to the Pixhawk board by using the start
function.
start(heartbeat,uav,"Serial",SerialPort="COM5")
If the Pixhawk board replies with heartbeat messages, the local MAVLink client establishes a connection to it.
Verify that a connection has been established by using the listClients
function. Note that the heartbeat messages include the ComponentType and
AutopilotType information of the Pixhawk board.
listClients(gcs)
ans = 2×4 table
SystemID ComponentID ComponentType AutopilotType
________ ___________ ____________________ _______________________
255 1 "MAV_TYPE_GCS" "MAV_AUTOPILOT_INVALID"
1 1 "MAV_TYPE_QUADROTOR" "MAV_AUTOPILOT_PX4"Verify that the ComponentType and
AutopilotType properties of the mavlinkclient
object have been automatically updated to match the heartbeat message values.
uav
uav =
mavlinkclient with properties:
SystemID: 1
ComponentID: 1
ComponentType: "MAV_TYPE_QUADROTOR"
AutopilotType: "MAV_AUTOPILOT_PX4"To stop the connection, first stop the heartbeat message stream.
stop(heartbeat,uav)
Then, disconnect the local MAVLink client from the serial port.
disconnect(gcs)
Connect a local MAVLink client to QGroundControl by exchanging heartbeat messages over UDP,
Open QGroundControl.

Create a mavlinkdialect object using the common.xml file.
dialect = mavlinkdialect("common.xml");Create a local MAVLink client that represents a simulated UAV by creating a mavlinkio object with these settings:
MAVLink message definition —
dialectMAVLink system ID —
1MAVLink component ID —
1MAVLink component type —
MAV_TYPE_QUADROTORMAVLink autopilot type —
MAV_AUTOPILOT_GENERIC
uav = mavlinkio(dialect,SystemID=1,ComponentID=1,AutopilotType="MAV_AUTOPILOT_GENERIC", ... ComponentType="MAV_TYPE_QUADROTOR");
Store the remote MAVLink client information that represents QGroundControl. By default, QGroundControl has a system ID of 255 and a component ID of 190.
qgc = mavlinkclient(uav,255,190);
Connect the local MAVLink client to a UDP port by using the connect function.
connect(uav,"UDP");Verify that the local MAVLink client has connected to a UDP port by using the listConnections function.
connectionTable = listConnections(uav)
connectionTable =
1x2 table
ConnectionName ConnectionInfo
______________ ___________________
"Connection1" "UDP@0.0.0.0:55371"Create a heartbeat microservice object.
heartbeat = mavlinkmicroservice(uav,"heartbeat");Specify the host address and port of QGroundControl.
qgcHost = "127.0.0.1";
qgcPort = 14550;Start sending heartbeat messages to QGroundControl by using the start function.
start(heartbeat,qgc,"UDP",RemoteHost=qgcHost,RemotePort=qgcPort);If QGroundControl replies with heartbeat messages, the local MAVLink client establishes a connection to it.

Verify that the connection has been established by using the listClients
function.
listClients(uav)
ans = 2×4 table
SystemID ComponentID ComponentType AutopilotType
________ ___________ ____________________ _______________________
255 190 "MAV_TYPE_GCS" "MAV_AUTOPILOT_INVALID"
1 1 "MAV_TYPE_QUADROTOR" "MAV_AUTOPILOT_GENERIC"To stop the connection, first stop the heartbeat message stream.
stop(heartbeat,gcs)
Then, disconnect the local MAVLink client from the UDP port.
disconnect(uav)
Input Arguments
Local MAVLink client, specified as a mavlinkio object.
Connection type, specified as one of these options:
"UDP"— Connects the local MAVLink client to a UDP port. Specify the UDP port number using theLocalPortargument. If you do not specify the port number, theconnectfunction connects the local MAVLink client to a random open UDP port."Serial"— Connects the local MAVLink client to a serial port. You must specify the serial port name using theSerialPortargument.
Data Types: char | string
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: connect(mavlink,"UDP",LocalPort=50791) specifies to connect the MAVLink client to UDP port 50791.
Connection identifier, specified as a character vector or string scalar. By default, the function assigns a connection identifier of
"Connection, where N"N is an integer that starts at 1 and
increases with each connection created, including named connections and connections that have been disconnected.
Data Types: char | string
UDP port number, specified as an integer in the range [0,
65535]. To specify this argument, you must specify
type as "UDP". If you specify LocalPort as
0, connect function connects the
MAVLink client to a random open local port.
Serial port name, specified as a character vector or string scalar. To specify
this argument, you must specify type as
"Serial".
Tip
To get a list of serial ports on your system, use the serialportlist function, or press tab when you specify the SerialPort argument.
Example: "COM2"
Data Types: char | string
Serial port baud rate, specified as a positive scalar. To specify this
argument, you must specify type as
"Serial".
Data flow control method for a serial connection, specified as one of these
options. To specify this argument, you must specify type as
"Serial".
"none"— Do not use data flow control."hardware"— Use hardware handshaking to control data flow."software"— Use software handshaking to control data flow.
Data Types: char | string
Parity bit mode for a serial connection, specified as "none",
"even", or "odd". To specify this argument,
you must specify type as
"Serial".
Data Types: char | string
Number of bits used to represent one character of data for a serial connection, specified
as 5, 6, 7, or
8. To specify this argument, you must specify
type as "Serial".
Number of stop bits used for a serial connection, specified as 1 or
2. To specify this argument, you must specify
type as "Serial".
Output Arguments
Connection identifier, returned as a string scalar.
Data Types: string
Version History
Introduced in R2019aThe connect function now enables you to connect a MAVLink client
to a serial port by specifying the type argument as
"Serial". You can also configure the serial connection by using the
SerialPort, BaudRate,
EnableFlowControl, Parity,
DataBits, and StopBits name-value arguments.
See Also
disconnect | mavlinkclient | heartbeatMicroservice | parameterMicroservice | commandMicroservice | missionMicroservice | ftpMicroservice | mavlinkdialect
External Websites
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)