2012-03-30 60 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);