Standard Library: std.devices
● Intermediate5 min
Every type, method and function of std.devices: what the spec lists, then each declaration in its source, with its examples and the line that declares it.
Spec 14.18 · learn it in Devices.
A declaration below shows what a program writes against: a type's fields, a function's parameters, what it gives, its contracts (require, ensure) and its examples. kavach test runs every example, so each one is true of the code.
What the spec lists
| Function or method | Gives |
|---|---|
devices.serial_ports() -> [String] | The serial ports there are: COM3 on Windows, /dev/ttyUSB0 on Linux |
devices.open_serial(path, baud) -> SerialPort ! DeviceError | Opens the port at path at baud bits a second (8 data bits, no parity, 1 stop bit) |
port.write(data) ! DeviceError | Sends the Bytes data |
port.read(max) -> Bytes ! DeviceError | Up to max bytes: what has arrived, waiting at most a second for the first; empty Bytes when none came |
port.close() | Closes the port; a port is closed anyway when its last holder ends |
std.devices · devices.kav
use std.devices: 2 types and 1 function. stdlib/devices/devices.kav
DeviceError type
Why a device couldn't be opened, read or written.
pub type DeviceError = NotFound(path: String) | Denied(path: String) | Failed(path: String, reason: String)SerialPort type
An open serial port.
pub type SerialPort {
pub path: String
id: Int
}write method
Sends data.
pub fn write(self, data: Bytes) ! DeviceError uses Serialread method
Up to max bytes: what has arrived, waiting at most a second for the first. Empty when none came.
pub fn read(self, max: Int) -> Bytes ! DeviceError uses Serialopen_serial function
Opens the serial port at path (COM3, /dev/ttyUSB0) at baud bits a second, 8 data bits, no parity, 1 stop bit.
pub fn open_serial(path: String, baud: Int) -> SerialPort ! DeviceError uses Serial