Write data to serial port
Write and Read Data with Serial Port Device
Create a connection to a serial port device. In this example, the serial port at
COM3 is connected to a loopback device.
device = serialport("COM3",9600)
device = Serialport with properties: Port: "COM3" BaudRate: 9600 NumBytesAvailable: 0 Show all properties, functions
Write the values
[1,2,3,4,5] in uint8 format.
Since the port is connected to a loopback device, the data you write to the device is returned to MATLAB. Read all the data.
ans = 1×5 1 2 3 4 5
device — Serial port connection
Serial port connection, specified as a
write(device,1:5,"uint8") writes to the serial port
data — Numeric or ASCII data
numeric | character vector | string scalar
Numeric or ASCII data, specified as a row (1-by-
N) or column
N-by-1) vector of numeric values or as a character vector or
string scalar of text. For all numeric
data is a row vector of values.
write(device,[20:24],"int16") writes the values
datatype — Size and format of each value
Size and format of each value, specified as a character vector or string.
datatype determines the number of bytes to write for each value
and the interpretation of those bytes as a MATLAB data type. For ASCII text, you can specify
write(device,1:5,"int16") writes data as int16 data