Open shantanoo opened 1 year ago
Pull Request #33
Hi shantanoo,
first off, thanks for the pull request!
I'm trying to keep the code/API simple and concise, so I'm trying to understand the rationale behind this. Why would you need to differentiate between holding reg write opcodes?
Need to take action based on opcode. E.g. only write multiple registers should be supported and single register should return error. Might be better to different handler functions for single and multiple registers to have one-to-one mapping as per modbus specification.
only write multiple registers should be supported and single register should return error
If what you're trying to achieve is to make sure that multiple registers are being written atomically (instead of a single register), would testing on Quantity work in your handler? Something along those lines:
if isWrite && req.Quantity < 2 {
err = ErrIllegalDataValue
return
}
Note that your handler still wouldn't be able to tell if that request came through opcode 0x6 or through 0x10 with a single register, but that shouldn't be a concern for your use case.
Note that this technique can also be used on reads, if you ever want to ensure that reads are atomic as well.
Actually I am trying to read the modbus request on TCP server/slave and get the data from RTU client/master. In that case, server don't know what type of request comes from the client. Hence needed to differentiate between function code 6 and 16 (0x10).
In TCP server implementation, how to find whether the function code was 6 or 16 (0x10)? And how to respond to it specifically?