Error while getting Modbus response

I can understand not posting complete code sometimes, but what @Pylon is saying is true. Its very easy to focus on something that isn't even causing the problem when looking at partial code. I used to do quite a bit of MODBUS work, though its been a while. One thing I can tell you is that besides MODBUS often being extended in a long list of ways, none of which is part of the original MODBUS standard, companies are often very lax at implementing all features properly. So sometimes an unexpected response has more to do with a bad implementation at the other end!

Now the fact that you are successfully getting a response to a request with Function code 3 ( which as you know, actually equates to 4 in the data stream), is a good sign. It means a major source of coding errors, the MODBUS CRC, is being done right. But I know I've fallen victim in my own code to THINKING I was getting a crc error to a request, but actually was getting a 5 byte exception response and was not processing the short response it correctly. Or I was neglecting the fact that exception (error) responses have the function code's Hi bit set. Or I was doing something as silly as mistaking no response for a CRC error, or even forgetting to clear my receive buffer before a transaction (which is VERY helpful when you're at the de-bugging stage). Another "gotcha" is that despite standards being set, all MODBUS implementations seldom follow the same guidelines for parity or even the number of data bits. It can be just your luck that one particular command/response comes through correctly, even with the wrong parity setting.

Finally, if you're doing this kind of work, I'd strongly suggest you take the time to make yourself a little "hexprint" utility, which lets you easily display exactly what bytes are sent and received with nice formatting. I realize this means multiple serial ports, and extra hassle. But in any binary protocol I've worked, I've spotted more problems looking at the "playback" of what exactly came and went, than I have just by looking at my own code.