G’day
I found a small bug in the “pac-int-mb-master” library that had me going around in circles chasing intermittent checksum errors whilst troubleshooting some modbus hardware.
In the subroutine for 15 ForceMultipleCoils there is an error in the “Build RTU” script block that leads to an incorrect evaluation of the “number of data payload” bytes.
// Add Byte Count
nTemp2 = 1 + (nNumValues / 8);
Any time an integer multiple of 8 coils is being written to this evaluates to 1 more than the required number of bytes to carry the coil states/bits.
A more correct method would be to use:
nTemp2 = ceil(nNumValues / 8); but unfortunately, there is no ceil() function equivalent in optoscript (or at least not that I can find)…
I used a crude method to implement the ceil() function to round up the resulting fraction:
ceil(x/y) can be achieved with (x + y - 1)/y when assigning the result to an integer. Replacing the offending line with the following fixed the subroutine and all my intermittent modbus woes… and the slaves are now responding to their master.
// Add Byte Count
nTemp2 = (nNumValues + 7)/8;
Fairly trivial but I hope this helps somebody out there