readwrite(string, <read_len>, <SS>)

This SPI bus Python object method sends a string to and/or receives a string from the SPI bus device linked to the SPI bus Python object at its creation.

The first input parameter string is a Python string to send to the SPI bus device.

The second optional parameter <read_len> is a Python integer which represents the length of the data to be received from the SPI bus device. Default value if omitted is 0. read_len value range is (0 ÷254).

The third optional input parameter <SS>, is a Python byte, represents the Slave Select line number to be used among those linked to the SPI bus Python object at its creation and can have values from 0 to 7

Default behavior if omitted is that SS default line specified in init will be used, if any.

Behavior if present is that this SS line will be used regardless of what set in init.

The return value is a Python integer with value -1 if an error occurred or is a Python string which contains the data received, in the last case the value might be empty if no data is received.

 

If the length of data to be received read_len is less than the string to send string length:

only the first read_len bytes will be saved during the transmission of string bytes.

 

If the length of data to be received read_len is greater than the string to send string length:

all the read_len bytes will be saved during transmission of string bytes plus the number of 0x00 bytes necessary to complete the receive.

 

Example:

 

rec = SPIbus.readwrite('test', 4)

 

sends 'test' and receives a string of 4 bytes assigning it to rec.