Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

UARTs

UARTs provide asynchronous serial I/O over TX/RX. Final’s type is UartPeriph. Instances: uart0, uart2, uart3, uart4 (common default). Optional RS-232 hardware flow control uses CTS/RTS when .flowHW is true. See the Pin Map.

Set transmission properties before enabling. Changing them while active is not allowed.

Properties (set before enable)

PropertyValues / defaultDescription
baud> 0, def 115200bit rate
dataBits5..8, def 8data bits
stopBits1 or 2, def 1stop bits
parityUartPeriph.parityNone (def), parityOdd, parityEvenparity
flowHWbool, def falseif true, claims CTS/RTS pins
enabledboolopen the port and start RX background read
pathreadonlydevice path (e.g. /dev/ttyAMA0)
  115200 uart4.baud!
  8 uart4.dataBits!
  1 uart4.stopBits!
  UartPeriph.parityNone uart4.parity!
  false uart4.flowHW!
  true uart4.enabled!

Write

.write accepts an Int (one byte) or a ByteList:

  0x41 uart4.write^              // 'A'
  myBytes uart4.write^           // ByteList

Read with UartReader

Reading needs a byte budget, idle timeout, and optional delimiter. Use UartReader from .newReader^, set its properties, then foreach. The loop ends on timeout, max bytes, delimiter, or error. Check the readonly status fields afterward.

PropertyR/WDescription
maxNR/Wmax bytes this session; 0 = unlimited
timeoutR/Widle ms between bytes (def 100)
untilByteR/Wstop after this byte (0..255), or -1 for none
nRbytes delivered
timedOutRtrue if idle timeout ended the loop
hitDelimRtrue if untilByte was received
okRfalse on hard error
errRerror string when not ok

Example

Loopback-style read until newline (same pattern as the hardware UART tests):

  115200 uart0.baud!
  true uart0.enabled!
  115200 uart2.baud!
  true uart2.enabled!

  // TX on uart0
  ByteList.new^ >xs
  "hello\n" foreach i x:.append^ /foreach
  xs> uart0.write^

  // RX on uart2 until '\n'
  uart2.newReader^ >ts
  1000 t:.timeout!
  0x0A t:.untilByte!          // '\n'
  ByteList.new^ >xs
  t:peek foreach
    i x:.append^
  /foreach
  // xs> holds the line including '\n' if hitDelim
  t:.hitDelim@
  t:.timedOut@
  tdrop
  x:drop

There is no UART demo under examples/pi/ yet; enable the matching overlays (e.g. uart0-pi5 / uart2-pi5) before testing on hardware.