I try to read from a filedescriptor with fs.read. The callback-function is called after it got the data. It's working fine.
But now I want to implement a timeout mechanism: If the fs.read does not get data within the timeout it should stop reading.
How can I tell fs.read to stop reading? It is still hanging in the background trying to read.
I do roughly the following:
...
var fd = fs.openSync("/dev/ttyUSB0", 'rs+');
...
...
var bout = new Buffer(string,'binary');
fs.write(fd, bout, 0, bout.length,undefined,function(err, written, buffer) {
console.log("error writing: "+err);
var bin = new Buffer(1);
fs.read(fd,bin,0,1,undefined,function(err,read,buffer) {
console.log("got it: "+bin);
});
});
...
...
I want to write something to /dev/ttyUSB0 and read an answer, but sometimes there is no answer. If this happens, the read should timeout so that I can start another write/read.
Thanks
I tried to do it with Timeout and close, but it is not working, here is an example: You have to make "mkfifo file" for the test.
var fs=require('fs');
console.log("open file");
fs.open('file', 'r+', function(err,fd) {
console.log("start reading on "+fd);
var length=5;
var data = new Buffer(length);
data.fill("-");
setTimeout(function(){
console.log("timeout");
console.log("close fd "+fd);
fs.close(fd,function(err) {
console.log("close done: "+err);
});
}, 5000);
fs.read(fd, data, 0, length,undefined, function(error, got) {
console.log("error: "+error);
console.log("got callback: "+data);
});
console.log("done");
});
The fs.close does not work. After it has closed you can make a "echo test > file" and then the read get the data. Reading on a closed Filehandle?
Any Idea?