Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 3 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ pub fn build(b: *std.Build) void {

const serial_mod = b.addModule("serial", .{
.root_source_file = b.path("src/serial.zig"),
.target = target,
.optimize = optimize,
});

const unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/serial.zig"),
.target = target,
.optimize = optimize,
}),
.root_module = serial_mod,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
Expand Down
19 changes: 11 additions & 8 deletions src/serial.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");
const c = @cImport(@cInclude("termios.h"));
const Io = std.Io;

pub fn list(io: Io) !PortIterator {
Expand Down Expand Up @@ -963,12 +962,16 @@ pub fn flushSerialPort(port: std.Io.File, flush: Flush) !void {
return error.FlushError;
},
.macos => {
const TCIFLUSH = 1;
const TCOFLUSH = 2;
const TCIOFLUSH = 3;
const tcflush = @extern(fn (c_int, c_int) c_int, .{ .name = "tcflush" });
const mode: c_int = switch (flush) {
.input => c.TCIFLUSH,
.output => c.TCOFLUSH,
.both => c.TCIOFLUSH,
.input => TCIFLUSH,
.output => TCOFLUSH,
.both => TCIOFLUSH,
};
if (0 != c.tcflush(port.handle, mode))
if (0 != tcflush(port.handle, mode))
return error.FlushError;
},
else => @compileError("unsupported OS, please implement!"),
Expand Down Expand Up @@ -1178,10 +1181,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 +1199,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