read Function

Top  Previous  Next

Receives data from the serial port.




nThe serial port number, 0 - 2.
countThe number of bytes to receive.

Return Value

A string of length count or less, or a negative number if failure.


If the requested number of bytes has already been received and is waiting in the port's input buffer, this function will return immediately with a string of length count. Otherwise, this function will pause execution waiting for received data up to the amount of time specified by the readTimeout property. If the read times out before receiving all of the requested data, the return string will contain the data that had been received to that point. If no data was received, the return value is the empty string. The bytesCanRead property can be used to determine the largest of value for count that can be passed without causing execution to pause.


If the received data is binary, individual bytes can be accessed from the returned string using the element reference operator [].


This function can fail if the serial port has not been enabled by setting its bitRate property to a positive value.


Cell.Serial[0].bitRate = 9600;// Enable at 9600 baud

// ...

// reconstruct 16-bit word sent low byte first

if (Cell.Serial[0].bytesCanRead >= 2)


 val = Cell.Serial[0].read(2);// get two bytes

 val = val[0] + (val[1] << 8);


See Also

readTimeout Property, bytesCanRead Property