Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/serial.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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,
};
Expand Down
Loading