相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 =========================
    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

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
client = ModbusClient(
  method = 'rtu'
  ,port='/dev/tty.usbserial-AQ00BYCR'
  ,baudrate=38400
  ,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
                Thank you so much ! I will try that on monday! Do you have an example of write register and multiple registers with two values?
– Théo LE BORGNE
                Feb 28, 2020 at 15:31
                I have updated the answer above to include a write example. I'm not sure what you mean by "multiple registers with two values". If you elaborate on that I can possibly help. If this solved your problem please tag it as "correct". Thanks!
– Hayden Eastwood
                Feb 28, 2020 at 19:44
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章