Not sure how your post is relevant to rsyslog.
The modbus kit has a few issues:
- It doesn’t clear the comm buffer before sending your new request, so if a previous request came in after the timeout was reached, you will have data in the buffer that the subroutine will happily digest.
- The kit doesn’t validate the response protocol id, trans id or unit id/slave id. So in scenario 1, you can end up recording data from a previous request. This could be a different device in case of RTU or TCP to serial gateway, or different register range if requesting multiple groups of registers from the same device.
The first issue is easy to mostly fix and it will greatly reduce the risk of the 2nd issue. This is by changing the Open block on the kit subroutines from
if (GetNumCharsWaiting(chCommHandle) < 0) then
nStatus = OpenOutgoingCommunication(chCommHandle);
else
nStatus = 0;
endif
to
if (GetNumCharsWaiting(chCommHandle) < 0 or not IsCommunicationOpen(chCommHandle)) then
nStatus = OpenOutgoingCommunication(chCommHandle);
else
ClearCommunicationReceiveBuffer(chCommHandle);
nStatus = 0;
endif
I say mostly, because it is still possible for stale data to come in after the buffer is cleared. The validation issue will need some additional checks added to the Receive block to fix. I don’t have code for that, but did give some recommendations here on validating the Slave Id on RTU:
I guess I should get around to filing a bug report.