ZigEmbeddedGroup / serial

Serial port configuration library for Zig
MIT License
53 stars 18 forks source link

`configureSerialPort` doesn't seem to work properly #22

Closed romner-set closed 3 months ago

romner-set commented 3 months ago

I'm on Linux (NixOS specifically) trying to use this library to communicate with an Arduino Nano 33 BLE.

Everything works fine if the port was pre-configured by running screen /dev/ttyACM0 and zig_serial.configureSerialPort() is never called, but calling it messes up the port somehow and makes it so no input is ever received. This is my program:

const std = @import("std");
const zig_serial = @import("serial");

pub fn main() !void {
    var serial = try std.fs.openFileAbsolute("/dev/ttyACM0", .{ .mode = .read_write });
    defer serial.close();

    // --- commenting this out and running `screen /dev/ttyACM0` before starting the program works
    try zig_serial.configureSerialPort(serial, zig_serial.SerialConfig{
        .baud_rate = 9600,
        .word_size = .eight,
        .parity = .none,
        .stop_bits = .one,
        .handshake = .none,
    });
    // ---

    while (try serial.reader().readByte() != 0) { // note: .read(&buf) doesn't work either
        std.debug.print("received\n", .{});
    }
}

I'm using zig v0.13.0. To install the lib I used the following command:

zig fetch --save https://github.com/ZigEmbeddedGroup/serial/archive/9765882e390f1dcda41ad2a81283e5dbad122d4b.zip

Then added this to build.zig:

const pkg = b.dependency("serial", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("serial", pkg.module("serial"));
ikskuh commented 3 months ago

Arduino uses DTR,RTS for resetting the device. You have to the those lanes accordingly so the Arduino isn't held in RESET

You can use https://github.com/ZigEmbeddedGroup/serial/blob/9765882e390f1dcda41ad2a81283e5dbad122d4b/src/serial.zig#L811 to do that :)

romner-set commented 3 months ago

That works, thanks! Here's the code incase anyone comes across this in the future:

try zig_serial.changeControlPins(serial, zig_serial.ControlPins{ .rts = true, .dtr = true });