write
Write data to serial device
Description
Examples
Connect and Exchange Data with Serial Device
You can connect to a serial device from the MATLAB® software, write data to the device, and read data from the device.
Create a connection from the MATLAB software to the BeagleBone Black hardware.
bbb = beaglebone
Enable serial port 1
.
enableSerialPort(bbb,1) bbb.AvailableSerialPorts
ans = '/dev/ttyO1'
In '/dev/ttyO1'
, the 'O'
is
the capital letter O
, not the number zero.
Show the location of the port 1
TX
and RX pins, P9_24 (UART1_TXD)
and P9_26
(UART1_RXD)
, on the GPIO header.
showPins(bbb)
The BeagleBone Black board uses +3.3
V.
Do not connect BeagleBone Black hardware directly to devices that
use higher voltages.
Connect the BeagleBone Black serial port to a +3.3
V
serial device.
To receive data, connect the
P9_26 (UART1_RXD)
pin on the BeagleBone Black hardware to the TxD pin on the serial device.To transmit data, connect the
P9_24 (UART1_TXD)
pin on the BeagleBone Black hardware to the RxD pin on the serial device.To provide power, connect one of the
+3.3 V
pins on the BeagleBone Black hardware to theVCC
pin on the serial device.To ground the serial device, connect a ground pin (
GND
) on the BeagleBone Black hardware to theGND
pin on the serial device.
Research the values the serial device requires for baud, data bits, parity, and stop bit.
Create a connection, serial
,
from the MATLAB software to the serial device.
serial = serialdev(bbb,'/dev/ttyO1',9600)
serial = Serialdev with Properties: BaudRate: 9600 DataBits: 8 Parity: 'none' StopBits: 1 Timeout: 10
Write a pair of values to a serial device that requires a specific data type.
write(serial,[10 12],'uint16')
Read a 100-element array of numbers from the serial port.
output = read(serial,100,'uint16')
Increase the timeout period of the serial port.
serial.Timeout = 20
serial = Serialdev with Properties: BaudRate: 115200 DataBits: 8 Parity: 'none' StopBits: 1 Timeout: 20
Input Arguments
serial
— Connection to serial device
connection
Connection to a serial device, specified as a serialdev
object.
Example: serial
data
— Data to write to serial device
data
Data to write to the serial device, specified as a vector.
Example: [10 12]
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| char
precision
— Data precision
'uint8'
(default) | 'uint16'
| 'uint32'
| 'int8'
| 'int16'
| 'int32'
| 'char'
| 'single'
| 'double'
Data precision, specified as a character vector. Optional.
Example: 'uint8'
Data Types: char
Version History
Introduced in R2015a
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)