FluentModbus icon indicating copy to clipboard operation
FluentModbus copied to clipboard

fix two modbus rtu problems

Open LukasKarel opened this issue 2 years ago • 4 comments

When a ModbusRtuRequestHandler is Disposed it closes the SerialPort before the CancellationTokenSource of the base class is cancelled. Therefor the InBuffer of SerialPort should be cleared even if the serialport is already closed. Secondly on a timeout exception the UnitIdentifier should be reset otherwise an exception is thrown.

LukasKarel avatar Aug 25 '22 15:08 LukasKarel

Added a new commit, which adds a virtual stop method to the ModbusServer base class. This helps to add both servers in the same container and perform cleanup on them.

LukasKarel avatar Aug 29 '22 13:08 LukasKarel

Thank you! I will go through it next week as I need to finish another project right now.

Apollo3zehn avatar Aug 30 '22 10:08 Apollo3zehn

You wrote "Therefor the InBuffer of SerialPort should be cleared even if the serialport is already closed." but your PR does the opposite: Is clears the buffer only when the serial port is already closed. I am a little bit confused.

What happens if the serial port is already closed and we try to clear the buffer? Will it throw an exception?

Edit: I have tested it and it throws an exception. So I agree to this change (only discarding buffer when port is open).

Apollo3zehn avatar Sep 06 '22 12:09 Apollo3zehn

Where does the exception occur, if the UnitIdentifier is not reset? To be honest I think there is another bug in the ModbusRtuRequestHandler method InternalReceiveRequestAsync because the return value is never used. I think I need to change error handling in general in InternalReceiveRequestAsync (both for Modbus TCP and RTU) because when a TimeoutException occurs no frame should be processed at all. It should simply continue looping.

Apollo3zehn avatar Sep 06 '22 12:09 Apollo3zehn

I have removed the Line UnitIdentifier = 255 because I do not understand why it is required. However I will now work on a better error handling in that method so hopefully your issue are solved anyway.

Apollo3zehn avatar Sep 07 '22 12:09 Apollo3zehn