Standard library · Hardware

hw.uart

The serial port: open at a baud rate, send bytes and text, read bytes and lines.

import hw.uart

hw.uart is the serial port. Open sets the baud rate and returns a Port; bytes go out with WriteByte and WriteStr and come in with ReadByte, Available and ReadLine. Reading never blocks: ReadByte returns -1 when nothing has arrived, so poll Available or check for -1.

Sending to a device

Open the port at the device baud rate, then send text with WriteStr and single bytes with WriteByte. Commands to modems and GPS modules usually end with a carriage return, 13, and a line feed, 10; add them yourself.

hw-uart-guide-send.salam
Try it
import hw.board
import hw.uart

func main:
    port := uart.Open(1, 115200)
    uart.WriteStr(port, "AT")
    uart.WriteByte(port, 13)
    mut n := board.SimTaken(1)
    until n >= 0:
        print n, " "
        n = board.SimTaken(1)
    end
    println ""
end
Output
65  84  13

Receiving a reply

Available tells whether a byte waits, ReadByte takes one and ReadLine collects a whole line. On a computer, board.SimInject plays the device.

hw-uart-guide-receive.salam
Try it
import hw.board
import hw.uart

func main:
    port := uart.Open(1, 9600)
    println uart.Available(port)
    board.SimInject(1, 79)
    board.SimInject(1, 75)
    board.SimInject(1, 10)
    println uart.Available(port)
    println uart.ReadLine(port)
    println uart.ReadByte(port)
end
Output
false
true
OK
-1

Types

Port

#
struct Port

A serial port and the line settings it was opened with.

NameTypeDescription
numberint = 0The port number.
baudint = 115200The baud rate.
data_bitsint = 8The number of data bits, 8 unless changed.
parityint = 0The parity setting: PARITY_NONE, PARITY_EVEN or PARITY_ODD. The board layer keeps only the baud rate, so this field is a record for your own use.
stop_bitsint = 1The number of stop bits, 1 unless changed. Like parity, a record for your own use.
hw-uart-Port.salam
Try it
import hw.board
import hw.uart

func main:
    port := uart.Open(1, 9600)
    println uart.PARITY_NONE, uart.PARITY_EVEN, uart.PARITY_ODD
    uart.WriteStr(port, "hi")
    uart.WriteByte(port, 33)
    println board.SimTaken(1), board.SimTaken(1), board.SimTaken(1)
    println uart.Available(port), uart.ReadByte(port)
    board.SimInject(1, 111)
    board.SimInject(1, 107)
    board.SimInject(1, 10)
    println uart.Available(port)
    println uart.ReadLine(port)
    println uart.Available(port)
end
Output
0 1 2
104 105 33
false -1
true
ok
false

Constants and variables

PARITY_NONE

#
const PARITY_NONE: i32 = 0

Parity setting: no parity bit.

The example for this item is shown with the ones above: hw-uart-Port.salam

PARITY_EVEN

#
const PARITY_EVEN: i32 = 1

Parity setting: even parity.

The example for this item is shown with the ones above: hw-uart-Port.salam

PARITY_ODD

#
const PARITY_ODD: i32 = 2

Parity setting: odd parity.

The example for this item is shown with the ones above: hw-uart-Port.salam

Functions

Open

#
func Open(number: int, baud: int): Port

Sets the baud rate of a port and returns a Port for it.

The example for this item is shown with the ones above: hw-uart-Port.salam

WriteByte

#
func WriteByte(p: Port, value: int)

Sends one byte; only the low 8 bits are used.

The example for this item is shown with the ones above: hw-uart-Port.salam

WriteStr

#
func WriteStr(p: Port, text: str)

Sends every byte of a string, without adding a line ending.

The example for this item is shown with the ones above: hw-uart-Port.salam

ReadByte

#
func ReadByte(p: Port): int

Takes the next received byte, or -1 when nothing has arrived.

The example for this item is shown with the ones above: hw-uart-Port.salam

Available

#
func Available(p: Port): bool

True when a received byte is waiting.

The example for this item is shown with the ones above: hw-uart-Port.salam

ReadLine

#
func ReadLine(p: Port): str

Reads bytes up to a line feed and returns them as text, dropping carriage returns. Stops early when no more bytes are waiting.

The example for this item is shown with the ones above: hw-uart-Port.salam