From 0f59ec126f4463465af824114094a5657a813bc8 Mon Sep 17 00:00:00 2001 From: mochalins <117967760+mochalins@users.noreply.github.com> Date: Sat, 21 Sep 2024 17:47:41 +0900 Subject: [PATCH] doc: Added some examples --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index 1002fbc..985c921 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,47 @@ # serialport ![Linux Port Iteration Result](assets/Linux_Iteration_Demo.png) + +## Examples + +### Port Iteration + +```zig +// ... +var it = try serialport.iterate(); +defer it.deinit(); + +while (try it.next()) |stub| { + // Stub name used only to identify port, not to open it. + std.log.info("Found COM port {s}", .{stub.name}); + + std.log.info("Port file path: {s}", .{stub.path}); +} +// ... +``` + +### Polling Reads + +```zig +// ... +var port = try serialport.open(my_port_path); +defer port.close(); + +try port.configure(.{ + .baud_rate = .B115200, +}); + +const reader = port.reader(); + +var read_buffer: [128]u8 = undefined; +const timeout = 1_000 * std.time.ns_per_ms; + +var timer = try std.time.Timer.start(); +while (timer.read() < timeout) { + if (try port.poll()) { + const read_size = try reader.read(&read_buffer); + std.log.info("Port bytes arrived: {any}", .{read_buffer[0..read_size]}); + } +} +// ... +```