Qu

serial_ports

Builtin index · Concurrency

result = serial_ports()

Input: no input · Output: result

What it is

Takes no arguments. Lists the short names of every serial device the OS currently reports (e.g. "COM3" on Windows, "/dev/ttyUSB0" on Linux/macOS).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = serial_ports()no input
result List or string
Returns a List of strings. An empty list on a machine with no serial hardware attached is a correct, honest result, not an error.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

ports = serial_ports()                              # e.g. ["COM3"], or [] with no hardware attached

conn = serial_open(ports[0], 9600, timeout_ms=500)
line = read_line(conn)                              # blocks up to 500ms; `none` if nothing arrived

if line != none
    print("got: {line}")
else
    print("no line arrived within the timeout")
end if
close(conn)
output
no line arrived within the timeout

from concurrency

ports = serial_ports()
print("found {length(ports)} port(s): {ports}")

conn = serial_open(ports[0], 115200, timeout_ms = 1000)
write(conn, "MEAS?\n")
line = read_line(conn) ?? "(timed out)"
print(line)
close(conn)

not run by the build — this one needs something the repository cannot supply · from specialized

ports = serial_ports()
if length(ports) > 0
    conn = serial_open(ports[0], 9600, timeout_ms=200)
    n = available(conn)                        # bytes waiting, non-blocking

    data = read_bytes(conn, 4)                  # up to 4 bytes, short read on timeout

    print("available={n}, got {length(data)} byte(s)")
    close(conn)
else
    print("no serial hardware attached")
end if
output
available=0, got 0 byte(s)

from concurrency

How it works

serial_open(port, baud_rate, ...) / serial_ports() (§ real module's own doc comment for the full builtin-by-builtin design. read_bytes/available are new method names (no other Value kind answers to them, unlike read_line/ close/write above, which are shared with Value::File/ Value::UrlStream and so are guarded arms further up this match instead).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.