1
votes

I would like to control an actuator with a python script in MODBUS RTU master. I tried to use the library minimalmodbus to communicate (write bit, write & read registers) with my slave.

When I start my code, I have some errors. So, someone can I help me to find a solution?

My code:


    import minimalmodbus
    import os
    import struct
    import sys
    import serial
    import time

    instrument = minimalmodbus.Instrument('/dev/ttyRS485', 1)
    instrument.serial.port
    instrument.serial.baudrate = 9600
    instrument.serial.parity = serial.PARITY_NONE
    instrument.serial.bytesize = 8
    instrument.serial.stopbits = 1
    instrument.mode = minimalmodbus.MODE_RTU
    instrument.serial.timeout = 0.05


    modbus = instrument.write_bit(0x0427, 1)
    print (modbus)

    alarme = instrument.write_bit(0x0404, 1)
    print (alarme)

    alarme = instrument.write_bit(0x0404, 0)
    print (alarme)

    on = instrument.write_bit(0x0403, 1)
    print (on)

    home = instrument.write_bit(0x040B, 1)
    print (home)

    position = instrument.write_register(0x9900, 0, number_of_decimals=2,functioncode=16, signed=False)
    print (position)

    posi = instrument.write_register(0x9901, 6000, number_of_decimals=2,functioncode=16, signed=False)
    print (posi)

Errors:

    ========================= RESTART: /home/pi/test.py =========================
    None
    None
    None
    None
    None
    None
    Traceback (most recent call last):
    File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py", line 2448, in _pack
    result = struct.pack(formatstring, value)
    struct.error: 'H' format requires 0 <= number <= 65535

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/home/pi/test.py", line 36, in <module>
    posi = instrument.write_register(0x9901, 6000, number_of_decimals=2, functioncode=16, signed=False)
    File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py",line 518, in write_register 
    payloadformat=_PAYLOADFORMAT_REGISTER,
    File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py",line 1166, in _generic_command
    payloadformat,
    File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py",line 1514, in _create_payload
    value, number_of_decimals, signed=signed
    File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py", line 1991, in 
    _num_to_twobyte_string outstring = _pack(formatcode, integer)
    File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py", line 2454, in _pack
    raise ValueError(errortext.format(value, formatstring))
    ValueError: The value to send is probably out of range, as the num-to-bytestring conversion failed. 
    Value: 600000 Struct format code is: >H
1
can you mention the errors you have? - learner
The errors are located at the end of the code - Théo LE BORGNE
sorry, did not see that - learner
No worries and thanks for the change - Théo LE BORGNE
Did you have an idea? - Théo LE BORGNE

1 Answers

1
votes

In response to your request in the comments for an alternative library, here is what I use to read modbus with the pymodbus library:

import pymodbus
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer

port = '/dev/tty.usbserial-AQ00BYCR'
baudrate = 38400
client = ModbusClient(
  method = 'rtu'
  ,port='/dev/tty.usbserial-AQ00BYCR'
  ,baudrate=baudrate
  ,parity = 'O'
  ,timeout=1
  )
connection = client.connect()
registers  = client.read_holding_registers(0,100,unit=1)# start_address, count, slave_id
print (registers.registers)

Note that in the above, the reading begins from address 0 and continues to address 100, for slave_id 1.

To write registers, do the following:

write  = client.write_register(1,425,unit=1)# address = 1, value to set = 425, slave ID = 1