Skip to content

Update to zig master #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ pub fn build(b: *std.Build) void {
});

const unit_tests = b.addTest(.{
.root_source_file = b.path("src/serial.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/serial.zig"),
.target = target,
.optimize = optimize,
}),
});
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
Expand All @@ -29,11 +31,13 @@ pub fn build(b: *std.Build) void {
for (example_files) |example_name| {
const example = b.addExecutable(.{
.name = example_name,
.root_source_file = b.path(
b.fmt("examples/{s}.zig", .{example_name}),
),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path(
b.fmt("examples/{s}.zig", .{example_name}),
),
.target = target,
.optimize = optimize,
}),
});

// port info only works on Windows!
Expand Down
15 changes: 11 additions & 4 deletions examples/echo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ pub fn main() !u8 {
defer serial.close();

try zig_serial.configureSerialPort(serial, zig_serial.SerialConfig{
.baud_rate = 115200,
.baud_rate = 115_200,
.word_size = .eight,
.parity = .none,
.stop_bits = .one,
.handshake = .none,
});

try serial.writer().writeAll("Hello, World!\r\n");
// NOTE: everything is written directly to the serial port so there is no
// need to flush (because there is no buffering).
var writer = serial.writer(&.{});

var r_buf: [128]u8 = undefined;
var reader = serial.reader(&r_buf);

try writer.interface.writeAll("Hello, World!\r\n");

while (true) {
const b = try serial.reader().readByte();
try serial.writer().writeByte(b);
const b = try reader.interface.takeByte();
try writer.interface.writeByte(b);
}

return 0;
Expand Down
37 changes: 21 additions & 16 deletions src/serial.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const c = @cImport(@cInclude("termios.h"));
const Io = std.Io;

pub fn list() !PortIterator {
return try PortIterator.init();
Expand Down Expand Up @@ -748,14 +749,18 @@ pub const SerialConfig = struct {
/// Defines the handshake protocol used.
handshake: Handshake = .none,

pub fn format(self: Self, fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
_ = options;
_ = fmt;
return writer.print("{d}@{d}{c}{d}{s}", .{ self.baud_rate, @intFromEnum(self.word_size), @intFromEnum(self.parity), @intFromEnum(self.stop_bits), switch (self.handshake) {
.none => "",
.hardware => " RTS/CTS",
.software => " XON/XOFF",
} });
pub fn format(self: Self, writer: *Io.Writer) !void {
return writer.print("{d}@{d}{c}{d}{s}", .{
self.baud_rate,
@intFromEnum(self.word_size),
@intFromEnum(self.parity),
@intFromEnum(self.stop_bits),
switch (self.handshake) {
.none => "",
.hardware => " RTS/CTS",
.software => " XON/XOFF",
},
});
}
};

Expand Down Expand Up @@ -1188,63 +1193,63 @@ test "change control pins" {
test "bufPrint tests" {
var buf: [32]u8 = undefined;

try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{}", .{SerialConfig{
try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{f}", .{SerialConfig{
.handshake = .software,
.baud_rate = 115200,
.parity = .none,
.word_size = .eight,
.stop_bits = .one,
}}), "115200@8N1 XON/XOFF"));

try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{}", .{SerialConfig{
try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{f}", .{SerialConfig{
.handshake = .hardware,
.baud_rate = 115200,
.parity = .none,
.word_size = .eight,
.stop_bits = .one,
}}), "115200@8N1 RTS/CTS"));

try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{}", .{SerialConfig{
try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{f}", .{SerialConfig{
.handshake = .none,
.baud_rate = 115200,
.parity = .none,
.word_size = .eight,
.stop_bits = .one,
}}), "115200@8N1"));

try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{}", .{SerialConfig{
try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{f}", .{SerialConfig{
.handshake = .none,
.baud_rate = 115200,
.parity = .even,
.word_size = .eight,
.stop_bits = .one,
}}), "115200@8E1"));

try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{}", .{SerialConfig{
try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{f}", .{SerialConfig{
.handshake = .none,
.baud_rate = 115200,
.parity = .odd,
.word_size = .eight,
.stop_bits = .one,
}}), "115200@8O1"));

try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{}", .{SerialConfig{
try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{f}", .{SerialConfig{
.handshake = .none,
.baud_rate = 115200,
.parity = .space,
.word_size = .eight,
.stop_bits = .one,
}}), "115200@8S1"));

try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{}", .{SerialConfig{
try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{f}", .{SerialConfig{
.handshake = .none,
.baud_rate = 115200,
.parity = .mark,
.word_size = .eight,
.stop_bits = .one,
}}), "115200@8M1"));

try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{}", .{SerialConfig{
try std.testing.expect(std.mem.eql(u8, try std.fmt.bufPrint(&buf, "{f}", .{SerialConfig{
.handshake = .none,
.baud_rate = 9600,
.parity = .mark,
Expand Down