Write line of ASCII data to serial port
Write and Read Line of ASCII Data from 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
Check the default ASCII terminator.
ans = "LF"
Set the terminator to
"CR" and write a string of ASCII data. The
writeline function automatically appends the terminator to the
Write another string of ASCII data with the terminator automatically appended.
Since the port is connected to a loopback device, the data you write to the device
is returned to MATLAB. Read a string of ASCII data. The
returns data until it reaches a terminator.
ans = "hello"
Read a string of ASCII data again to return the second string that you wrote.
ans = "world"
Clear the serial port connection.
device — Serial port connection
Serial port connection, specified as a
writeline(device,1:5) writes to the serial port
data — ASCII data
character vector | string scalar
ASCII data to write, specified as a character vector or string scalar of text.
writeline(device,"*IDN?") writes the ASCII string