Qu

read_bytes

Builtin index · Concurrency

result = read_bytes(conn, n)

Input: conn, n · Output: result

What it is

conn (a Serial handle); n (integer, maximum bytes to collect). Blocks up to the handle's timeout_ms, returning a Vec of 0-255 integer values — possibly fewer than n if the timeout elapses first. Use read_line instead when the data is text with a line terminator.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = read_bytes(conn, n)
conn Serialconn (a Serial handle).
n —n (integer, maximum bytes to collect).
result Vec
Returns a Vec of 0-255 integer values, at most n long and possibly shorter.

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()
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.