添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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 checked all the links regarding this problem on Stackoverflow

  • AttributeError: 'ModbusIOException' object has no attribute 'registers'
  • AttributeError: 'ModbusIOException' object has no attribute 'registers' with Kinova Robot ModBus Code
  • Reading registers using Pymodbus (Modbus RTU)
  • I have the same problem but from what I was able to debug, I can read the data using QModMaster with no problem. The send data from the QModMaster and from my script is identical, still, I am unable to receive a proper response.

    Below you can see my code:

    from pymodbus.client.sync import ModbusSerialClient as ModbusClient
    from pymodbus.constants import Endian
    from pymodbus.exceptions import ConnectionException
    from pymodbus.payload import BinaryPayloadDecoder
    import time
    import logging
    import logging.handlers as Handlers
    logging.basicConfig()
    log = logging.getLogger()
    log.setLevel(logging.DEBUG)
    METHOD = 'RTU'
    COM_PORT = 'COM7'
    STOP_BITS = 1
    BYTE_SIZE = 8
    PARITY ='N'
    BAUD_RATE = 9600
    TIMEOUT = 1
    client = ModbusClient(method=METHOD,
                           port=COM_PORT,
                           stopbits=STOP_BITS,
                           bytesize=BYTE_SIZE,
                           parity=PARITY,
                           baudrate=BAUD_RATE,
                           strict=False,
                           timeout=TIMEOUT,
    WORD_ORDER = Endian.Little
    BYTE_ORDER = Endian.Big
    while True:
        connection = client.connect()
            print('Reading...')
            print(f"Connection is {connection}, socket is {client.is_socket_open()}")
            values = client.read_holding_registers(address=0x0,count=0xA,unit=0x1)
            print(values.registers[0])
            print(values, values.registers)
        except ConnectionException as ce:
            print("CE ERROR")
            # print("===ERROR=== \nPort cannot be accesed \n",ce)
        except AttributeError as ae:
            print("AE ERROR" , ae)
            connection = client.close()
        logging.basicConfig()
        log = logging.getLogger('pymodbus.server')
        log.setLevel(logging.ERROR)
        log = logging.getLogger('pymodbus')
        log.setLevel(logging.ERROR)
        handlers = [
            Handlers.RotatingFileHandler("logfile", maxBytes=1024 * 1024),
            Handlers.SMTPHandler("mx.host.com",
                                 "pymodbus@host.com",
                                 ["support@host.com"],
                                 "Pymodbus"),
            Handlers.SysLogHandler(facility="daemon"),
            Handlers.DatagramHandler('localhost', 12345),
        [log.addHandler(h) for h in handlers]
        time.sleep(1)
    

    And my debug log:

    DEBUG INFO

    And here is the send request from QModMaster and also the response in the background QModMaster Working

    Any idea why this is happening? I tried with different USB-Serial cables, and also with different devices. The problem is the same.

    So I managed to find a solution using this post: LINK

    The post suggested that a delay between connection and reading would be recommended "You can try running the serialclient with a start up delay after it open the port then doing a "stty -F /dev/ttyUSB0 crtscts", and see if that works."

    This is what I did, and it worked!

    while True:
    connection = client.connect()
    time.sleep(2)
        print('Reading...')
        print(f"Connection is {connection}, socket is {client.is_socket_open()}")
        values = client.read_holding_registers(address=0x0,count=0xA,unit=0x1)
        value = ((f'Modbus error: {values}') if values.isError() else values.registers)
        print(values.registers[0])
        print(values, values.registers)
            

    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.