Qu

serial_open

Builtin index · Concurrency

result = serial_open(port, baud_rate, [data_bits=8, stop_bits=1, parity="none", timeout_ms=1000])

Input: port, baud_rate, data_bits · Output: result

What it is

Opens a real OS serial port. port (string) is the OS device name, e.g. "COM3" on Windows or "/dev/ttyUSB0" on Linux/macOS. baud_rate (integer, must be positive) is the line speed. data_bits (optional integer, default 8) accepts 5/6/7/8. stop_bits (optional integer, default 1) accepts 1 or 2 (the underlying crate has no 1.5-stop-bit option). parity (optional string, default "none") accepts "none"/"even"/"odd". timeout_ms (optional number, default 1000, milliseconds) bounds every blocking read below — see the note on timeouts vs. EOF further down.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = serial_open(port, baud_rate, [data_bits=8, stop_bits=1, parity="none", timeout_ms=1000])
port stringport (string) is the OS device name, e.g.
baud_rate —baud_rate (integer, must be positive) is the line speed.
data_bits optional (default 8, stop_bits=1, parity="none", timeout_ms=1000)data_bits (optional integer, default 8) accepts 5/6/7/8.
result Serial
Returns a Serial handle. Errors clearly (naming the port) if the port doesn't exist, is already open elsewhere, or the OS refuses it.

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.