0
votes

I'm using minimalmodbus for reading and writing. I have USB to Rs485 connection.

I'm trying to write this "02 05 0000 ff11" to RTU register in python but i'm getting error. Here is my code.

Device address: 02

Function code: 05

Register Address: 0000

Value: ff00

 import minimalmodbus
 instrument = minimalmodbus.Instrument('COM4', 1)
 instrument.write_register(02, 05, 0000, ff00)

I need the same functionality in the above code using python!

I need this functionality in Python script

1

1 Answers

1
votes

You are mixing up some settings.

The Modbus slave address (2 in your case) should be included in the instantiation of the instrument:

instrument = minimalmodbus.Instrument('COM4', 2)

And the arguments you are using for the write_register function are wrong as well, they should be:

instrument.write_register(0, 0xff00)

As you can see write_register only takes two arguments: the register number and the value you want to read in it.

It might be a good idea to take a look at the code and/or some examples.

EDIT: I realized you actually want to use function code 05 (write coils). To do that you should use write_bit function instead:

instrument.write_bit(0, 1)

That means write True on coil 0.