1

I need to write and read registers from a serial device. Everything works fine, but I need to store the messages in a variable. For example:

instrument = minimalmodbus.Instrument("/dev/ttyUSB0", 1, minimalmodbus.MODE_ASCII, debug=True)
instrument.serial.baudrate = 9600
instrument.serial.bytesize = serial.EIGHTBITS
instrument.serial.parity = serial.PARITY_NONE
instrument.serial.stopbits = serial.STOPBITS_ONE
instrument.serial.timeout = 2

r = instrument.read_register(1, functioncode=3)
print(r)

What I get is

MinimalModbus debug mode. Create serial port /dev/ttyUSB0
MinimalModbus debug mode. Will write to instrument (expecting 15 bytes back): ':010300010001FA\r\n' (3A 30 31 30 33 30 30 30 31 30 30 30 31 46 41 0D 0A)
MinimalModbus debug mode. Clearing serial buffers for port /dev/ttyUSB0
MinimalModbus debug mode. Sleeping 3.71 ms before sending. Minimum silent period: 4.01 ms, time since read: 0.30 ms.
MinimalModbus debug mode. Response from instrument: ':0103020000FA\r\n' (3A 30 31 30 33 30 32 30 30 30 30 46 41 0D 0A) (15 bytes), roundtrip time: 31.7 ms. Timeout for reading: 2000.0 ms.

0

which is correct; what I need is to store ':010300010001FA\r\n' and ':0103020000FA\r\n' in a variable, and possibly also the number of bytes expected back. How can I do it? I didn't find anything on the documentation about this

Deffo
  • 191
  • 1
  • 8
  • 21
  • "need to store the messages in a variable" so you want to store the raw Modbus request and response?. minimalmodbus does not make this info available (most users just want the values they requested) but with some relatively simple customisation, [starting here](https://github.com/pyhys/minimalmodbus/blob/master/minimalmodbus.py#L385), you could gain access. – Brits May 03 '21 at 20:48

1 Answers1

0

Option 1) Modify the library to return the "answer" variable from private function _communicate to your program.

Option 2) Save the debug log output to a variable and filter this variable for the string.

user3376253
  • 80
  • 1
  • 6