Main Content


Write and read binary data from SPI instrument


A = writeAndRead(OBJ, dataToWrite)


A = writeAndRead(OBJ, dataToWrite) writes the data, dataToWrite, to the instrument connected to interface object OBJ and reads the data available from the instrument as a result of writing dataToWrite. OBJ must be a 1-by-1 SPI interface object. Values are written and read as uint8 data.

The interface object must be connected to the device using the connect function before any data can be read from the device, otherwise an error is returned. A connected interface object has a ConnectionStatus property value of connected.

SPI protocol operates in full duplex mode, so input and output data transfers happen simultaneously. For every byte written to the device, a byte is read back from the device.

For more information on using the SPI interface and this function, see Configuring SPI Communication and Transmitting Data over the SPI Interface.


To get a list of options you can use on a function, press the Tab key after entering a function on the MATLAB® command line. The list expands, and you can scroll to choose a property or value. For information about using this advanced tab completion feature, see Using Tab Completion for Functions.


This example shows how to create a SPI object s, and read and write data.

Construct a spi object called s using Vendor 'aardvark', with BoardIndex of 0, and Port of 0.

s = spi('aardvark', 0, 0);

Connect to the chip.


Read and write to the chip.

dataToWrite = [2 0 0 255]
data = writeAndRead(s, dataToWrite);

Disconnect the SPI device and clean up by clearing the object.


Version History

Introduced in R2013b