Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

NodeJS on BeagleBoard fs.write Error Unknown -1

NodeJS on BeagleBoard fs.write Error Unknown -1

Problem

I am trying to create a NodeJS library that allows usage of the BeagleBone's serial (uart) ports. Some of the pins are muxed so some configuration bits have to be written to two files. Here is my function that writes the configuration bits to enable the 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.");
    }
};

Here is the file containing this funcion. Here is the output from running this function:

[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' }

I have verified that the correct output is written to test files, I have tried numerous mode parameters ('0777' doesn't matter), I have tried this with the sync and async functions to no avail, and I have also tried, successfully, writing to these files in python. If you have any ideas that would help solve this problem I would be very grateful.

Here is a github repo of the project, its in its infancy right now so there isn't alot of documentation. The python version is in the repo as well.

Problem courtesy of: Michael Dillon

Solution

Thanks to Ben Noordhuis on the NodeJS google group I was pointed to the following issue that was causing the problem. The device driver that I was trying to write to apparently doesn't accept arbitrary seek writes, so in order to get around that I needed to trick NodeJS into using write instead of pwrite. The trick is to tell the write command to start writing at -1 instead of 0.

fs.writeSync (txFd, txBuf, 0, txBuf.length, -1);
Solution courtesy of: Michael Dillon

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

NodeJS on BeagleBoard fs.write Error Unknown -1

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×