Skip to main content

uart.read()

Reads a byte from the serial port

Availability

Device

Returns

Integer — the value read from the UART FIFO, or -1 if the FIFO is empty

Description

This method reads a byte from the UART via a receive (input) FIFO stack.

With impOS™ release 32 and under, the method returns a byte from the FIFO, or -1 if no data is available.

From impOS release 34, the data returned will depend on whether the TIMING_ENABLED control flag has been applied to the UART’s configuration. If TIMING_ENABLED is not set, the method returns a byte from the FIFO, or -1 if no data is available.

If TIMING_ENABLED is set, however, the returned integer is ‘stamped’ with the interval (in microseconds) from the receipt of the ‘previous’ byte and the reported one. This interval is stored in bits 30 through 8 of the returned value; bits 7 through 0 is the data itself. Since only 22 bits are available to store the interval, the interval is limited to a maximum of 0x7FFFFF microseconds (8.388607 seconds). Any interval greater than this will be reported as 0x7FFFFF.

Again, if no data is available, the method will return -1 even with TIMING_ENABLED set.

By default, the receive FIFO is set at 80 bytes is size. However, under impOS Release 32 and up, the size of the FIFO may be altered using uart.setrxfifosize().

Example Code

The following code demonstrates the use of the TIMING_ENABLED control flag (available in impOS release 34 and up) to manage Modbus frames. Note The code checks for 4.5 character times, as the value includes the time taken for the byte itself, as well as for the 3.5 characters of gap.

The following imp device code uses uart.read() — implemented as uart57 and aliased as arduino — to sample the bus for data values sent by the Arduino (code below) as its LED changes state. The UART reading code is triggered by data arriving at the imp, an event which triggers the arduinoData() callback.