node-modbus-serial
node-modbus-serial copied to clipboard
Read coil issues
Hi. I think there's a problem with the size of the returning array on readCoils function.
I want to read just one coil. To do that, I'm doing this code:
result = await client.readCoils(0,1);
console.log(result);
But the result is always an array with 8 elements and the application can deal with wrong data.
This works different from the readHoldingRegisters function, because the size of the returning array matches the number of the registers on the request. For example:
result = await client.readHoldingRegisters(0,5);
console.log(result);
In this case, the result is an array with 5 elements.
What do you think?
Hi, thank you for the issue.
The protocol return the coils in groups of 8 ( one byte ) so you will always get the coiled asked for and a padding of zeroes to complete the number to a multiplication of 8.
On the user side we can sugar cote it, and remove the padding, this will require adding a per-request parameter that will store the requested number of coils, and when data is received, use this data to cut off the padding.
You are welcome to send a pull request implementing it.