readwrite(string, <read_len>)

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

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

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

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 address ADDR set at IIC bus Python object creation is not 0:

if string is not empty it will be sent to IIC bus device (Write);

if read_len is greater than 0 a string will be received from IIC bus device (Read);

if string is not empty and read_len is greater than 0 first string will be sent to IIC bus device (Write) and then a string will be received from IIC bus device (Read).

 

If the address ADDR set at IIC bus Python object creation is 0:

if string is not empty and it is at least 2 bytes long and read_len is 0 it will be sent to a generic IIC bus device (Write) whose address and W bit must be set properly by Python script developer in first byte of string;

if string is not empty and it is at least one byte long and read_len is greater than 0 it will be sent to a generic IIC bus device whose address and R bit must be set properly by Python script developer in first byte of string and then a string will be received from IIC bus device (Read);

it is not possible a Write and Read operation in the same readwrite call.

 

Examples:

 

IICbus = IIC.new(3, 4, 0x50)

IICbus.init()

rec = IICbus.readwrite('\x08'+'test', 10)

 

sends 08 hex byte followed by 'test' and then receives a string of 10 bytes assigning it to rec.

 

rec = IICbus.readwrite('\x08'+'test', 0)

 

sends only 08 hex byte followed by 'test'.

 

rec = IICbus.readwrite('\x08', 10)

 

sends 08 hex byte and then receives a string of 10 bytes assigning it to rec.

 

rec = IICbus.readwrite('', 10)

 

receives only a string of 10 bytes assigning it to rec.