pyhys / minimalmodbus

Easy-to-use Modbus RTU and Modbus ASCII implementation for Python.
Apache License 2.0
306 stars 146 forks source link

how to separate the debug mode information and the result ? #83

Closed patfrench closed 2 years ago

patfrench commented 2 years ago

Hi

I have a tkinter application with 2 Text widgets, I would like to put in the first one the result and in the second one the debug mode details

Thank you

j123b567 commented 2 years ago

What do you mean by "result"?

patfrench commented 2 years ago

Hi

widget 1

MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): '\x01\x03\x00d\x00\x01ÅÕ' (01 03 00 64 00 01 C5 D5)
MinimalModbus debug mode. Clearing serial buffers for port COM25
MinimalModbus debug mode. No sleep required before write. Time since previous read: 33633125.00 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: '\x01\x03\x02\x07\x0fúp' (01 03 02 07 0F FA 70) (7 bytes), roundtrip time: 31.0 ms. Timeout for reading: 2500.0 ms.

widget2

1807

thx

j123b567 commented 2 years ago

minimalmodbus does not use standard logging and does not have hooks to register event listeners. One solution I can imagine is that you can implement your own class based on the original and hook all functions you need like this

class MyLoggingInstrument(minimalmodbus.Instrument):
    def _print_debug(self, text: str) -> None:
        do_something_with_debug_text(text)

    def read_long(self, *args, **kwargs):
        result = super().read_long(*args, **kwargs)
        do_something_with_read_long_result(result)
        return result

instrument = MyLoggingInstrument('/dev/ttyUSB1', 1)

There can be probably easier solution, but this should just work.

patfrench commented 2 years ago

Hi

Thanks for the code, it will help me

Thank you