Skip to content

Instantly share code, notes, and snippets.

@lachlanm-git
Created January 24, 2024 04:42
Show Gist options
  • Save lachlanm-git/8a852c42b133ba24523751e9c708c242 to your computer and use it in GitHub Desktop.
Save lachlanm-git/8a852c42b133ba24523751e9c708c242 to your computer and use it in GitHub Desktop.
Serial PR verify: Windows ANSI API
/// Quick example to validate PR: https://github.com/ZigEmbeddedGroup/serial/pull/13
///
/// Output of program:
/// -----------------------------------------------------------------------
/// Port name (5): COM5
/// - System location (9): \\.\COM5
/// - Friendly name (23): USB Serial Port (COM5)
/// - Description (16): USB Serial Port
/// - Manufacturer (5): FTDI
/// - Serial # (9): DP03W90FA
/// - HW ID: FTDIBUS\VID_0403+PID_6015+DP03W90FA\0000
/// - VID: 0x0403 PID: 0x6015
/// echo: H
/// echo: e
/// echo: l
/// echo: l
/// echo: o
/// echo: ,
/// echo:
/// echo: W
/// echo: o
/// echo: r
/// -----------------------------------------------------------------------
const std = @import("std");
const zig_serial = @import("serial");
pub fn main() !u8 {
var iterator = try zig_serial.list_info();
defer iterator.deinit();
while (try iterator.next()) |info| {
// .len-1 -> account for null termination in .port_name
if (std.mem.eql(u8, "COM5", info.port_name[0 .. info.port_name.len - 1])) {
std.debug.print("\nPort name ({d}): {s}\n", .{ info.port_name.len, info.port_name });
std.debug.print(" - System location ({d}): {s}\n", .{ info.system_location.len, info.system_location });
std.debug.print(" - Friendly name ({d}): {s}\n", .{ info.friendly_name.len, info.friendly_name });
std.debug.print(" - Description ({d}): {s}\n", .{ info.description.len, info.description });
std.debug.print(" - Manufacturer ({d}): {s}\n", .{ info.manufacturer.len, info.manufacturer });
std.debug.print(" - Serial # ({d}): {s}\n", .{ info.serial_number.len, info.serial_number });
std.debug.print(" - HW ID: {s}\n", .{info.hw_id});
std.debug.print(" - VID: 0x{X:0>4} PID: 0x{X:0>4}\n", .{ info.vid, info.pid });
_ = try echo(info.system_location);
}
}
return 0;
}
fn echo(port_name: []const u8) !u8 {
var serial = std.fs.cwd().openFile(port_name, .{ .mode = .read_write }) catch |err| switch (err) {
error.FileNotFound => {
try std.io.getStdOut().writer().print("The serial port {s} does not exist.\n", .{port_name});
return 1;
},
else => return err,
};
defer serial.close();
try zig_serial.configureSerialPort(serial, zig_serial.SerialConfig{
.baud_rate = 115200,
.word_size = 8,
.parity = .none,
.stop_bits = .one,
.handshake = .none,
});
try serial.writer().writeAll("Hello, World!\r\n");
var i: usize = 0;
while (i < 10) {
var b = try serial.reader().readByte();
std.debug.print("echo: {c}\n", .{b});
try serial.writer().writeByte(b);
i += 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment