Reputation: 346476
I'm trying to access a virtual serial port created with http://www.hhdsoftware.com/virtual-serial-ports on Windows 8.
When I use a terminal emulator (Realterm) to access the port, it seems to work; well, I see characters I type in the output file, I do not see anything from the input file in the emulator window, but to be honest the UI of the emulator is overwhelming and I don't fully understand what I'm doing.
But I actually want to access the port using the serialport module for node.js. This is my JS code:
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var serialPort = new SerialPort("COM2", {
// these are the default values in Realterm that seem to work there
baudRate: 57600,
dataBits: 8,
stopBits: 1,
parity: 'none',
parser: serialport.parsers.readline("\r\n")
}, true, function(error){
if ( error ) {
console.log('failed to open: '+error);
} else {
console.log('open');
serialPort.on('data', function(data) {
console.log('data received: ' + data);
});
serialPort.write("ls\r\n", function(err, results) {
console.log('err ' + err);
console.log('results ' + results);
});
}
});
My problem is that when I run the above code, I only get an error:
failed to open: Error: SetCommState: Unknown error code 87
What's really strange is that I only get this error when I run the code before accessing the port with Realterm. After closing Realterm, I do not get this error, but instead there is simply no output, i.e. the code doesn't receive the "open" event.
Any Ideas what I'm doing wrong?
Upvotes: 1
Views: 2516
Reputation: 10386
The library serialport you're using is calling SetCommState:
if (!SetCommState(file, &dcb)) {
ErrorCodeToString("SetCommState", GetLastError(), data->errorString);
return;
}
To configure the communication of your virtual serial port. Then it checks the errors the function GetLastError returns:
void ErrorCodeToString(const char* prefix, int errorCode, char *errorStr) {
switch(errorCode) {
case ERROR_FILE_NOT_FOUND:
_snprintf(errorStr, ERROR_STRING_SIZE, "%s: File not found", prefix);
break;
case ERROR_INVALID_HANDLE:
_snprintf(errorStr, ERROR_STRING_SIZE, "%s: Invalid handle", prefix);
break;
case ERROR_ACCESS_DENIED:
_snprintf(errorStr, ERROR_STRING_SIZE, "%s: Access denied", prefix);
break;
case ERROR_OPERATION_ABORTED:
_snprintf(errorStr, ERROR_STRING_SIZE, "%s: operation aborted", prefix);
break;
default:
_snprintf(errorStr, ERROR_STRING_SIZE, "%s: Unknown error code %d", prefix, errorCode);
break;
}
}
The unknown error 87 is not checked but it means that an INVALID PARAMETER has been passed when opening the port. The problem is that GetLastError function is not going to give more information to clarify which param is causing the error.
Since you can't do anything on Realterm, I think it doesn't work either on it.
After reading the home page of serialport, I noticed it provides two tools for playing with serial ports: serialportlist & serialportterm, try with the last one and see if you can open the port from there.
The most usual error with serial ports is setting wrong the baud speed rate. I suppose you've already tried several speeds, but in case you don't, try with the valid ones:
115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300, 200, 150, 134, 110, 75, or 50
Upvotes: 4