write
Write data to serial port
Description
Examples
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.
write(device,1:5,"uint8")
Since the port is connected to a loopback device, the data you write to the device is returned to MATLAB. Read all the data.
read(device,5,"uint8")
ans = 1×5 1 2 3 4 5
Input Arguments
device
— Serial port connection
serialport
object
Serial port connection, specified as a serialport
object.
Example: write(device,1:5,"uint8")
writes to the serial port
connection device
.
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 datatype
types,
data
is a row vector of values.
Example: write(device,[20:24],"int16")
writes the values
[20,21,22,23,24]
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| char
| string
datatype
— Size and format of each value
"uint8"
| "int8"
| "uint16"
| "int16"
| "uint32"
| "int32"
| "uint64"
| "int64"
| "single"
| "double"
| "char"
| "string"
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 datatype
as either "char"
or "string"
.
Example: write(device,1:5,"int16")
writes data as int16 data
type.
Data Types: char
| string
Version History
Introduced in R2019b
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)