Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Issue opening/closing character devices in Node.js #7101

Closed
@bminer

Description

@bminer

I am having issues opening/closing character devices in Node. How does one properly open and close a character device (i.e. a serial port or TTY device) for reading and writing?

Here's my little program that doesn't quite work as expected:

var fs = require("fs");
fs.open("/dev/ttyUSB0", "r+", function(err, fd) {
    if(err) throw err;
    var input = fs.createReadStream("/dev/ttyUSB0", {"fd": fd});
    input.on("data", function(chunk) {
        console.log("in:", chunk);
    });
    var out = fs.createWriteStream("/dev/ttyUSB0", {"fd": fd});
    console.log("out:", "ATZ\r\n");
    out.write("ATZ\r\n");
    setTimeout(function() {
        console.log("Closing file...");
        fs.close(fd, function(err) {
            console.log("File has been closed", err);
            // At this point, Node will just hang
        });
    }, 5000).unref();
});

The output of the program is something like this:

out: ATZ

in: <Buffer 41 54 5a 0a>
in: <Buffer 0a>
in: <Buffer 0a>
in: <Buffer 4f 4b 0a>
in: <Buffer 0a>
Closing file...
File has been closed null

Everything looks okay, but Node does not exit, as expected; rather, Node simply hangs at this point. Why? Is this a bug?

This problem exists even when opening a TTY terminal (i.e. /dev/tty2 for reading/writing)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions