read(into:timeout:)
Reads a byte from the external device.
@discardableResult func read(into buffer: inout UInt8, timeout: Int? = nil) -> Result<Int, Errno>
Parameters
Returns
The number of data that is still needed or an error for the failure case.
The parameter timeout
sets the maximum time for data reception. If the reception is fulfilled before the time limit, the read process will end up automatically. If the time is too short, you may not get all data.
The timeout value can be:
-1: wait until all required data are received.
0: stop the data reception immediately no matter whether all data are received or not.
A positive integer: wait for a specified period (in milliseconds), then stop reading data when time is up, even if not all needed data are got.
The returned result indicates how the communication goes:
For success case: it returns an integer telling the expected data count and the actual received data count.
0 means all needed data are received, that’s to say, the reading is fulfilled.
1 means you still needs a byte, that means the time is not enough, so you haven’t got the byte from the sensor.
For failure case: it returns the error that happens during communication.