0

我正在尝试创建一个允许使用 BeagleBone 的串行(uart)端口的 NodeJS 库。一些引脚是多路复用的,因此必须将一些配置位写入两个文件。这是我编写配置位以启用 uart 的函数:

var setMuxForUart = function (uart, next) {
    var txFd, rxFd;
    var txBuf = new Buffer(uart.muxTx.config, 'ascii');
    var rxBuf = new Buffer(uart.muxRx.config, 'ascii');
    var txBytesWritten, rxBytesWritten;

    console.log ("Configuring UART MUX for " + uart.path);

    txFd = fs.openSync (MUX_PATH + uart.muxTx.path, 'w');
    rxFd = fs.openSync (MUX_PATH + uart.muxRx.path, 'w');

    if (txFd && rxFd) {
        try {
            txBytesWritten = fs.writeSync (txFd, txBuf, 0, txBuf.length, 0);
        }
        catch (e) {
            fs.closeSync (txFd);
            fs.closeSync (rxFd);
            console.log ('Error Writing to file: '+ MUX_PATH + uart.muxTx.path + ' | ' + util.inspect (e));            
            return;
        }

        try {
            rxBytesWritten = fs.writeSync (rxFd, rxBuf, 0, rxBuf.length, 0);
        }
        catch (e) {
            fs.closeSync (txFd);
            fs.closeSync (rxFd);
            console.log ('Error Writing to file: ' + MUX_PATH + uart.muxRx.path + ' | ' + util.inspect(e));            
            return;
        }

        fs.closeSync (txFd);
        fs.closeSync (rxFd);

        if (txBytesWritten && rxBytesWritten) {
            console.log ("Uart MUX finished configuration");
            next ();
        }
        else {
            console.log ("An error occured writing to the UART MUX.");
        }
    }
    else {
        console.log ("An error occured while opening the UART MUX files.");
    }
};

这是包含此功能的文件。这是运行此函数的输出:

root@beaglebone:~/workspace/BonescriptSerial# node BonescriptSerial.js
The "sys" module is now called "util". It should have a similar interface.
Opening Serial Port for: /dev/ttyO1
Configuring UART MUX for /dev/ttyO1
Error Writing to file: /sys/kernel/debug/omap_mux/uart1_txd | { [Error: UNKNOWN, unknown error] errno: -1, code: 'UNKNOWN', syscall: 'write' }

我已经验证了正确的输出写入了测试文件,我尝试了许多模式参数('0777' 无关紧要),我用同步和异步功能尝试过,但我也尝试过,成功,在 python 中写入这些文件。如果您有任何想法可以帮助解决这个问题,我将不胜感激。

这是该项目的github 存储库,它现在处于起步阶段,因此没有很多文档。python 版本也在 repo 中。

4

1 回答 1

0

感谢 NodeJS 谷歌组的 Ben Noordhuis,我被指出了导致问题的以下问题。我试图写入的设备驱动程序显然不接受任意搜索写入,所以为了解决这个问题,我需要欺骗 NodeJS 使用 write 而不是 pwrite。诀窍是告诉写命令开始写在-1而不是0

fs.writeSync (txFd, txBuf, 0, txBuf.length, -1);
于 2012-04-01T21:13:37.700 回答