write
Write data to serial device
Description
write(
writes data to the serial device.myserialdevice
,data
)
The write
method stops when it finishes writing the specified
data to the device, or when the timeout period elapses.
write(
writes data to the serial device and specifies precision.myserialdevice
,data
,precision
)
Examples
Connect to 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 Raspberry Pi® hardware.
mypi = raspi
Note
If you encounter errors after running the above command, try using additional
arguments (as listed in raspi
) or refer to Troubleshoot Connecting Issues to Raspberry Pi Hardware.
Show the location of the Tx and Rx pins, GPIO
14 (UART0_TXD)
and GPIO 15 (UART0_RXD)
,
on the GPIO header.
showPins(mypi)
Raspberry Pi hardware uses +3.3 V. Do not connect Raspberry Pi hardware directly to devices that use higher voltages.
Connect the Raspberry Pi serial port to a +3.3 V serial device.
To receive data, connect the RXD pin (
GPIO 15 UART0_RXD
) on the Raspberry Pi board to the TXD pin on the serial device.To transmit data, connect the TXD pin (
GPIO 14 UART0_TXD
) on the Raspberry Pi board to the RXD pin on the serial device.Connect a ground pin (
GND
) on the Raspberry Pi board to theGND
pin on the serial device.Connect one of the
+3.3 V
pins on the Raspberry Pi board to theVCC
pin on the serial device.
Research the values the serial device requires for baud, data bits, parity, and stop bit.
Create a connection, myserialdevice
,
from the MATLAB software to the serial device.
myserialdevice = serialdev(mypi,'/dev/serial0',9600)
myserialdevice = Serialdev with Properties: Port: '/dev/serial0' BaudRate: 9600 DataBits: 8 Parity: 'none' StopBits: 1 Timeout: 10
Write a pair of values to the serial device that requires a specific data type.
write(myserialdevice,[10 12],'uint16')
Read a 100-element array of numbers from the serial port.
output = read(myserialdevice,100,'uint16')
Increase the timeout period of the serial port.
myserialdevice.Timeout = 20
myserialdevice = Serialdev with Properties: Port: '/dev/serial0' BaudRate: 115200 DataBits: 8 Parity: 'none' StopBits: 1 Timeout: 20
Input Arguments
myserialdevice
— Connection to serial device
connection
Connection to a serial device, specified as a serialdev
object.
Example: myserialdevice
data
— Data to write to serial device
data
Data to write to the serial device, specified as a vector. The value is
cast to the precision selected in the precision
argument. If you do not specify the precision
argument, the value is cast to the default
precision of uint8
.
Example: [10 12]
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
| char
| string
precision
— Data precision
'uint8'
(default) | 'uint16'
| 'uint32'
| 'uint64'
| 'int8'
| 'int16'
| 'int32'
| 'char'
| 'single'
| 'double'
Data precision, specified as a string. Optional.
Example: 'uint8'
Data Types: char
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The
write
function does not support code generation for character data.You can also deploy
write
on the Raspberry Pi hardware in MATLAB Online™.
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
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)