I have built a Function that check if Port exists via array given from SerialPort
package.
The port is connected. And when I run the code outside the function it's work(true
while port's plugged).
When i tried to run it inside a function I recive undefined
function exists(portName) {
SerialPort.list(function (err, ports) {
ports.forEach(function (port) {
console.log(port.comName);
if (port.comName == portName) {
return true
}
});
});
}
console.log(exists('COM7'));
results:
NodeJS service has started.
undefined
COM1
COM7
Port is connected.
Full code at: https://github.com/eshk12/SerialPort-HTTP-Server/blob/master/routes/index.js
Thanks!
As port checking is asynchronous you probably need a promising function:
Or as @skirtle noted it can be even shorter:
So you can do:
Or: