serialport

Connection to serial port

Description

A serialport object represents a serial client for communication with the serial port. After creating the object, use dot notation to set its properties.

Creation

Description

example

s = serialport(port,baudrate) connects to the serial port specified by port with a baud rate of baudrate.

s = serialport(port,Name,Value) connects to the serial port and sets additional properties using optional name-value pair arguments.

s = serialport, without arguments, connects to the serial port using the property settings of your last cleared serialport object instance. The retained properties are Port, BaudRate, ByteOrder, FlowControl, StopBits, DataBits, Parity, Timeout, and Terminator. See Properties.

Input Arguments

expand all

Serial port name, specified as a string or character vector. Use serialportlist to get a list of connected ports.

Example: "COM2"

Baud rate for serial communication, specified as a double.

Example: 9600

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

You can use Name-Value pairs to set the DataBits, Parity, StopBits, FlowControl, ByteOrder, and Timeout object properties. See Properties for their data types and allowed values.

Example: "Timeout",30

Properties

expand all

This property is read-only.

Serial port for connection, returned as a string or character vector.

Example: "COM1"

Data Types: string

Communication speed in bits per second, returned as a positive integer double.

Example: 14400

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Parity to check whether data has been lost or written, returned as "none", "even", or "odd".

Example: "odd"

Data Types: char | string

Number of bits to represent one character of data, returned as 8, 7, 6, or 5.

Example: 8

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Pattern of bits that indicates the end of a character or of the whole transmission, returned as 1, 1.5, or 2.

Example: 1

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Mode for managing data transmission rate, returned as "none", "hardware", or "software".

Example: "software"

Data Types: char | string

Sequential order in which bytes are arranged into larger numerical values, returned as "little-endian" or "big-endian".

Example: "little-endian"

Data Types: char | string

Allowed time in seconds to complete read and write operations, returned as a numeric value.

Example: 60

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

This property is read-only.

Number of bytes available to read, returned as a numeric value.

Example: 1024

Data Types: double

This property is read-only.

Number of bytes written to the serial port, returned as a numeric value.

Example: 512

Data Types: double

Terminator character for reading and writing lines, returned as "LF", "CR", or "CR/LF". Set this property with the configureTerminator function.

Example: "CR"

Data Types: char | string

Bytes available callback function, returned as a function handle. The callback is triggered by receiving a certain number of bytes or a terminator. Set this property with the configureCallback function.

Example: @myFun

Data Types: function_handle

Number of bytes of data to trigger callback, returned as a double. This value is used only when the BytesAvailableFcnMode property is "byte". Set these properties with the configureCallback function.

Example: 128

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Bytes available callback trigger mode, returned as "off", "byte", or "terminator". This setting determines if the callback is off, triggered by a number of bytes, or by a terminator. Set this property with the configureCallback function.

Example: "off"

Data Types: char | string

Callback function triggered by error, returned as a function handle.

Example: @myErrFun

Data Types: function_handle

General purpose property for user data, returned as any type.

Example: datetime()

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | function_handle | categorical | datetime | duration | calendarDuration | fi
Complex Number Support: Yes

Object Functions

readRead data from serial port
readlineRead line of ASCII string data from serial port
readbinblockRead one binblock of data from serial port
writeWrite data to serial port
writelineWrite line of ASCII data to serial port
writebinblockWrite one binblock of data to serial port
writereadWrite command to serial port and read response
flushFlush serial port buffers
configureTerminatorSet terminator for ASCII string communication on serial port
configureCallbackSet serial port callback function and trigger
getpinstatusGet serial pin status
setRTSSet serial RTS pin
setDTRSet serial DTR pin

Examples

collapse all

Read 16 values of uint32 data from the COM3 serial port.

s = serialport("COM3",9600,"Timeout",5);
data = read(s,16,"uint32");

Introduced in R2019b