Apollo3zehn / FluentModbus

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

feat: support start Modbus tcp server with an specific port #51

Closed ChangeTheCode closed 2 years ago

ChangeTheCode commented 2 years ago

It would be great if the ModbusTcpServer.Start() also supports an function with a port configuration. I already found the Start(IPEndPoint) where it would be possible to set the port.

For my point of view would be nice to have an method with for example an nullable string. If the string is null the default port 502 will be used.

Apollo3zehn commented 2 years ago

Do you mean something like server.Start(string? localEndpoint)? I am not happy with a nullable string, I would not consider this good API design. I would prefer the following:

server.Start("127.0.0.1:1024") // listens on 127.0.0.1:1024
server.Start("192.168.0.2") // listens on 192.168.0.2:502
server.Start("localhost") // listens on 127.0.0.1:502
server.Start() // listens on 127.0.0.1:502

Would this fit to your usecase?

ChangeTheCode commented 2 years ago

@Apollo3zehn I would also be happy with for e.g. server.Start("127.0.0.1:1024") // listens on 127.0.0.1:1024

I understand your point about API design. So would be better to go with the approach you mentioned 👍

rligocki commented 2 months ago

Is this feature implemented? I searched source code, but did not found any way how to specify port on which modbus TCP server should start. Commit that closed this issue changes only modbus client connect method. Thank you.

Apollo3zehn commented 2 months ago

You can use the void Start(IPEndPoint localEndpoint) overload and specify the port like this: new IPEndPoint(ipAddress, 502). Or do you need the exact feature of being able to pass a string to the Start() method like above? Something like 127.0.0.1:1024 or 0.0.0.0:1024? I am not sure why I did not implement it for the server as well at that time but right know I would like to figure out if you need any way to specify the port or the specific way of this issue by using a string.

rligocki commented 2 months ago

No, there is no need to pass string like this 127.0.0.1:1024. I need just some way to specify port when starting modbus TCP server. Will try method overload you mentioned. ;) Thank you. PS: IP address should be 0.0.0.0 in case I need modbus server to listen to any IP?

Apollo3zehn commented 2 months ago

Yes, in that case it is 0.0.0.0.