2012-03-30 58 views
0

我正在尝试创建一个允许使用BeagleBone的串行(uart)端口的NodeJS库。某些引脚被复用,因此一些配置位必须写入两个文件。这里是我的功能写入配置位,使能UART:BeagleBoard上的NodeJS fs.write错误未知-1

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."); 
    } 
}; 

下面是一个包含该funcion的file。 以下是运行这个函数的输出:

[email protected]:~/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 repo这个项目,它现在还处于初期阶段,所以没有很多文档。 python版本也在回购中。

回答

0

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

fs.writeSync (txFd, txBuf, 0, txBuf.length, -1);