--code--
this is the code i use to set up the terminal before reading from the serial port:
const std = @import("std");
pub const SerialPipe = struct {
const Self = @This();
serial_fd: std.posix.fd_t,
pub fn setupEnv(self: *Self) !void {
var terminfo = try std.posix.tcgetattr(self.serial_fd);
terminfo.ispeed = .B115200;
terminfo.ospeed = .B115200;
terminfo.cflag.CLOCAL = true;
terminfo.cflag.CREAD = true;
terminfo.cflag.CSIZE = .CS8;
terminfo.cflag.PARENB = false;
terminfo.cflag.CSTOPB = false;
terminfo.lflag.ICANON = false;
terminfo.lflag.ISIG = false;
terminfo.iflag.IGNCR = false;
terminfo.iflag.INPCK = false;
terminfo.iflag.INLCR = false;
terminfo.iflag.ICRNL = false;
terminfo.iflag.IUCLC = false;
terminfo.iflag.IMAXBEL = false;
terminfo.iflag.IXON = false;
terminfo.iflag.IXOFF = false;
terminfo.iflag.IXANY = false;
terminfo.oflag.OPOST = false;
terminfo.cc[11] = 0x00;
terminfo.cc[16] = 0x00;
terminfo.cc[4] = 0x00;
try std.posix.tcsetattr(self.serial_fd, .NOW, terminfo);
}
pub fn init(serial_path: []const u8) !Self {
const try_fd = try std.posix.open(serial_path, .{
.SYNC = true,
.NOCTTY = true,
}, 0o444);
return Self{
.serial_fd = try_fd,
};
}
pub fn deinit(self: *Self) void {
std.posix.close(self.serial_fd);
}
pub fn read(self: SerialPipe, buffer: []u8) std.posix.ReadError!usize {
return std.posix.read(self.serial_fd, buffer);
}
pub const Reader = std.io.Reader(SerialPipe, std.posix.ReadError, read);
pub fn reader(pipe: SerialPipe) Reader {
return .{ .context = pipe };
}
};