Apollo3zehn / FluentModbus

Lightweight and fast client and server implementation of the Modbus protocol (TCP/RTU).
MIT License
188 stars 69 forks source link

Modify functionality of any single unit special case #98

Open ChrisProto opened 10 months ago

ChrisProto commented 10 months ago

This is related to Issue #97 (which was also related to Issue #93)

This is the code that I implemented to solve the problem we had, which I described first in issue 93.

This allows special functionality when we have a single unit. It doesn't matter what the Unit Identifier of the unit is.

For TCP messages, the incoming Unit Identifier is ignored, and the spec recommends using the "insignificant" identifier, 0xFF. This means that there is a difference between the incoming Unit Identifier and the actual Unit Identifier of the defined single unit. For example, we might have defined Unit 1, but it must respond to an incoming message with Unit Identifier 1 or 0xFF. In addition, we might want to accept messages with Unit Identifier 0, which is used for broadcast. Also, we might want to accept messages from any Unit Identifier, since for TCP they can be ignored. In these cases, the incoming Unit Identifier can be several different values, but the internal unit we want to work with has an actual Unit Identifier of 1.

You will see code changes that keep track of the incoming and actual Unit Identifiers. In all cases, the Unit Identifier in the response is whatever the incoming Unit Identifier was in the request.

Miscellaneous Change: I needed to build this in a .net6 environment, so I added NET5_0_OR_GREATER to the conditional compilation lines in the ModbusTcpClient and ModbusUtils files.

Apollo3zehn commented 4 months ago

Hi, I was able to work yesterday and today on merging some merge request and closing some issues. However, this merge request is not yet being worked on because I thinks it needs some time for me to understand all code changes. Also I need to understand how this is connected to #103 where I stumbled over the problem of how to distinguish broadcast messages and normal messages, both addressed to unit 0.

I see in your commits that you have added a boolean option to explicitly enable broadcast responses, which is great and might solve the problem I encountered.

However I need to understand everything better first, before I can merge it. What I am wondering about is, if the default unit identifier of the server should be "0x00" or better "0xff" to avoid trouble with the broadcast address. And if so, it will be a new major release (v6.0.0) as this is a breaking change.

I hope I will find the time soon to also work on this merge request. Thanks for your patience.