serialdev - Connection to serial device on Raspberry Pi hardware - MATLAB (original) (raw)
Connection to serial device on Raspberry Pi hardware
Description
This object represents a connection from the MATLAB® software to a serial device on the Raspberry Pi® hardware. To exchange data with the serial device, use this object with the functions listed in Object Functions.
Creation
Syntax
Description
`myserialdevice` = serialdev([mypi](#buno%5F4k%5Fsep%5Fshared-mypi),[port](#mw%5F840f2468-2860-488b-9a48-0314dbccc8de))
creates a connection, myserialdevice
, from the MATLAB software to the serial device on the Raspberry Pi hardware.
On the Raspberry Pi hardware, the /boot/cmdline.txt
file configures/dev/serial0
as the serial device and sets the default baud to115200
. The serial device connects to the Rx
andTx
pins on the GPIO header.
By default, the serial console in the customized version of Raspbian Linux® on your Raspberry Pi hardware is disabled. To use serialdev
, disable the serial console.
`myserialdevice` = serialdev([mypi](#buno%5F4k%5Fsep%5Fshared-mypi),[port](#mw%5F840f2468-2860-488b-9a48-0314dbccc8de),`baudRate`,`dataBits`,`parity`,`stopBits`)
creates a connection from the MATLAB software to the serial device on the Raspberry Pi hardware using optional arguments to override the default values for baud, data bits, parity, and stop bits.
Input Arguments
Connection to the Raspberry Pi hardware board, specified as a raspi object.
The name of the serial port that is connected to the Raspberry Pi hardware, specified as a string or character vector.
Example: '/dev/serial0'
Data Types: char
| string
Properties
The name of the serial port that is connected to the Raspberry Pi hardware, specified as a string or character vector.
Example: '/dev/serial0'
Data Types: char
| string
This property is read-only.
The rate at which the data is transferred over the serial line, specified as a scalar. The baud is measured in seconds. This property is set by thebaudRate
input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.
Example: 9600
Data Types: double
This property is read-only.
The number of bits per character, specified as a scalar. This property is set by thedataBits
input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.
Example: 8
Data Types: double
This property is read-only.
The type of parity bit to be added to the data, specified as a character vector.
A parity bit is used to detect error in data transmission.
'none'
– No parity is used for error detection in the data transmission.'odd'
– The data bits plus the parity bit produce an odd number of 1s.'even'
– The data bits plus the parity bit produce an even number of 1s.
This property is set by the parity
input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.
Example: 'none'
Data Types: char
This property is read-only.
The number of bits used as the stop bit in the data transmission. A stop bit marks the end of a unit of transmission. This property is set by thestopBits
input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.
Example: 1
Data Types: double
The waiting time in seconds to perform the read or write operation on the serial device. It is specified as a positive value of type double
.
Example: 10
Data Types: double
Object Functions
read | Read data from serial device |
---|---|
write | Write data to serial device |
Examples
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.
Show the location of the Tx and Rx pins, GPIO 14 (UART0_TXD)
and GPIO 15 (UART0_RXD)
, on the GPIO header.
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
Extended Capabilities
- You can also deploy
serialdev
on the Raspberry Pi hardware in MATLAB Online™.