Apollo3zehn / FluentModbus

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

Allow the serial port to be passed in so its open/close lifetime can be controlled #77

Closed schotime closed 2 years ago

schotime commented 2 years ago

There are situations where having the serial port be managed externally is really beneficial, such as sharing of said serial port on a bus.

This PR allows you to pass in an external ModbusRtuSerialPort such that the underlying SerialPort can be managed.

Let me know if you need changes, or I've missed something. Great library!

Apollo3zehn commented 2 years ago

Thanks for your PR, this is a good improvement! I had to change your PR a little bit because in the Connect method you were closing the old port depending on if the current port is internal or not. There will be an old port if you call connect multiple times. That is why I moved the field to the ModbusRtuSerialPort class. I have also added some more doc strings.

I will merge the PR right now and make more changes in other parts of the library and then release the final v4 version so that is gets out of the preview status.

schotime commented 2 years ago

Yeh, awesome. Thanks. It might be good to have this for TCP (pass in the TcpClient) as well. Maybe less important if a modbus server accepts multiple tcp clients but still maybe worth having that option?

Apollo3zehn commented 2 years ago

I thought it was already possible but I confused it with the Modbus TCP Server. I will add that option tomorrow :-)