fs.open fs.read and fs.write works only one way (and one time reading) against COM-port

86 views
Skip to first unread message

KentK

unread,
Apr 6, 2012, 3:56:05 PM4/6/12
to nodejs
Hi I am running this on Windows 7.
I am trying to set up a dual communication towards an external serial
device on COM1.
What happens is that I will only receive (fs.read) the first incoming
character.
Before I receive that character I am not able to send any character,
but after receiving, I can send over and over again. But not receive
any more.

Here is the code:

var fs = require('fs');
var fd;

var Buffer = require('buffer').Buffer;
var buffer = new Buffer(100);

fs.open("\\\\.\\COM1", "w+", function(status, _fd){
fd = _fd;
if(status){
console.log(status.message);
return;
}

fs.read(fd, buffer, 0, 100, null, function(e,l,b){
console.log("Read " + l + " bytes.");
return;
});

});

var buf = new Buffer('BBBB\n');
var stdin = process.openStdin();
stdin.on('data', function(chunk) {
// For now I just send the four Bs when [Enter] is pressed.
fs.write(fd, buf, 0, buf.length, null, function(err,written){
if(err)throw err;
console.log(written + " characters written.");
});
});

Bert Belder

unread,
Apr 6, 2012, 6:36:28 PM4/6/12
to nodejs
Well, you're only doing one reading attempt, so it doesn't surprise me
that you don't receive more than one chunk of data.

However I should warn you - nobody ever tested COM ports with node for
windows (at least not that I am aware of) so it might not work at all.
If you hit a snag, you can ask here, but I guess you'd better just
debug node and figure out what's going wrong yourself.

- Bert

Nathan Rajlich

unread,
Apr 6, 2012, 6:46:53 PM4/6/12
to nod...@googlegroups.com
There's also this fork of node-serialport that appears to attempt to add some COM-specific functionality to node-serialport: https://github.com/jaredhanson/node-serialport/compare/windows-support
Reply all
Reply to author
Forward
0 new messages