opensourcegeek
opensourcegeek

Reputation: 5962

node-serialport on windows with multiple devices hangs

I've been experimenting with node-serialport library to access devices connected to a USB hub and send/receive data to these devices. The code works fine on linux but on windows(windows 8.1 and windows 7) I get some odd behaviour. It doesn't seem to work for more than 2 devices, it just hangs when writing to the port. The callback for write method never gets called. I'm not sure how to go about debugging this issue. I'm not a windows person, if someone can give me some directions it would be great.

Below is the code I'm currently using to test.

/*
  Sample code to debug node-serialport library on windows

*/ 

//var SerialPort = require("./build/Debug/serialport");
var s = require("./serialport-logger");
var parsers = require('./parsers');
var ee = require('events');


s.list(function(err, ports) {
  console.log("Number of ports available: " + ports.length);
  ports.forEach(function(port) {
    var cName = port.comName,
        sp;
    //console.log(cName);

    sp = new s.SerialPort(cName, {
      parser: s.parsers.readline("\r\n")
    }, false);

    // sp.once('data', function(data) {
    //   if (data) {
    //     console.log("Retrieved data " + data);
    //     //console.log(data);  
    //   }

    // });

    //console.log("Is port open " + sp.isOpen());

    if(!sp.isOpen()) {
      sp.open(function(err) {
        if(err) {
          console.log("Port cannot be opened manually");

        } else {
          console.log("Port is open " + cName);

          sp.write("LED=2\r\n", function(err) {
            if (err) {
              console.log("Cannot write to port");
              console.error(err);

            } else {
              console.log("Written to port " + cName);


            }

          });


        }

      });

    } 

    //sp.close();   
  });  
});

I'm sure you'd have noticed I'm not require'ing serialport library instead I'm using serialport-logger library it's just a way to use the serialport addons which are compiled with debug switch on windows box.

Upvotes: 0

Views: 1519

Answers (2)

madlyR
madlyR

Reputation: 1

As opensourcegeek pointed all u need to do is to set UV_THREADPOOL_SIZE variable above default 4 threads.

I had problems at my project with node.js and modbus-rtu or modbus-serial library when I tried to query more tan 3 RS-485 devices on USB ports. 3 devices, no problem, 4th or more and permanent timeouts. Those devices responded in 600 ms interval each, but when pool was busy they never get response back.

So on Windows simply put in your node.js environment command line: set UV_THREADPOOL_SIZE=8 or whatever u like till 128. I had 6 USB ports queried so I used 8.

Upvotes: 0

opensourcegeek
opensourcegeek

Reputation: 5962

TLDR; For me it works by increasing the threadpool size for libuv. $ UV_THREADPOOL_SIZE=20 && node server.js

I was fine with opening/closing port for each command for a while but a feature request I'm working on now needs to keep the port open and reuse the connection to run the commands. So I had to find an answer for this issue.

The number of devices I could support by opening a connection and holding on to it is 3. The issue happens to be the default threadpool size of 4. I already have another background worker occupying 1 thread so I have only 3 threads left. The EIO_WatchPort function in node-serialport runs as a background worker which results in blocking a thread. So when I use more than 3 devices the "open" method call is waiting in the queue to be pushed to the background worker but since they are all busy it blocks node. Then any subsequent requests cannot be handled by node. Finally increasing the thread pool size did the trick, it's working fine now. It might help someone. Also this thread definitely helped me.

Upvotes: 1

Related Questions