diff --git a/src/serial.zig b/src/serial.zig index cdd0e89..17b120d 100644 --- a/src/serial.zig +++ b/src/serial.zig @@ -932,6 +932,23 @@ pub fn configureSerialPort(port: std.Io.File, config: SerialConfig) !void { } } +/// Read the number input byte available from port `port` +/// Only work on linux +pub fn InWaintingSerialPort(port: std.Io.File) !usize { + switch (builtin.os.tag) { + .linux => { + var remaining: c_int = 0; + const FIONREAD: u32 = 0x541B; // same as TIOCINQ + if (std.os.linux.ioctl(port.handle, FIONREAD, @intFromPtr(&remaining)) == -1) { + // if (std.c.ioctl(port.handle, @bitCast(FIONREAD), &remaining) == -1) { + return error.InWaitingError; + } + return @intCast(remaining); + }, + else => @compileError("unsupported OS, please implement!"), + } +} + const Flush = enum { input, output, @@ -1178,10 +1195,10 @@ test "basic configuration test" { else => unreachable, } - var port = std.Io.Dir.openFileAbsolute(std.testing.io, tty, .{ .mode = .read_write }) catch |err| switch(err) { + var port = std.Io.Dir.openFileAbsolute(std.testing.io, tty, .{ .mode = .read_write }) catch |err| switch (err) { error.FileNotFound => return error.SkipZigTest, else => |e| return e, - }; + }; defer port.close(std.testing.io); try configureSerialPort(port, cfg); @@ -1196,7 +1213,7 @@ test "basic flush test" { .macos => tty = "/dev/cu.usbmodem101", else => unreachable, } - var port = std.Io.Dir.openFileAbsolute(std.testing.io, tty, .{ .mode = .read_write }) catch |err| switch(err) { + var port = std.Io.Dir.openFileAbsolute(std.testing.io, tty, .{ .mode = .read_write }) catch |err| switch (err) { error.FileNotFound => return error.SkipZigTest, else => |e| return e, };