Exemplo n.º 1
0
 def __init__(self, port: str, baudrate: int = 9600):
     self.__port = port
     self.__baudrate = baudrate
     self.loop, self.client = ModbusClient(schedulers.ASYNC_IO,
                                           port=port,
                                           baudrate=baudrate,
                                           method='rtu')
Exemplo n.º 2
0
    def __init__(self, spec):
        self.spec = spec
        self.address = spec['address']
        # self.operations = spec['operations']
        # ModbusClient.__init__(self,method='rtu', port=spec['port'],stopbits=spec['stopbits'], baudrate=spec['baudrate'])
        print("1")
        try:
            print("2")

            self.loop, self.client = ModbusClient("async_io",
                                                  port=spec['port'],
                                                  stopbits=spec['stopbits'],
                                                  baudrate=spec['baudrate'],
                                                  method="rtu")

        except Exception as e:
            print(e)
        print("1")
        adress_register = 0x0  # 起始寄存器
        length_data = 0x40  # 数据长度 HEX=
        adress_gateway = 0x1  # 云盒地址
        rr = await client.read_holding_registers(adress_register,
                                                 length_data,
                                                 unit=adress_gateway)
        print('rr: %s' % repr(rr.registers))
        print(len(rr.registers))
        # assert(rq.function_code < 0x80)     # test that we are not an error
        # assert(rq.registers == [20]*8)      # test the expected value
        # assert(rr.registers == [20]*8)      # test the expected value
    except Exception as e:
        log.exception(e)
        client.transport.close()
    await asyncio.sleep(1)


if __name__ == '__main__':
    # ----------------------------------------------------------------------- #
    # For testing on linux based systems you can use socat to create serial
    # ports
    # ----------------------------------------------------------------------- #
    # socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
    # link=/tmp/ttyp0,raw,echo=0,ospeed=9600
    loop, client = ModbusClient(schedulers.ASYNC_IO,
                                port='COM2',
                                baudrate=115200,
                                method="rtu")
    loop.run_until_complete(start_async_test(client.protocol))
    loop.close()
Exemplo n.º 4
0
            "\nINFO : 同时读写保持寄存器测试----读写输入寄存器起始地址均为1,读写8个值,写入8个值均为20,读写的从机地址为0x01"
        )
        rq = await client.readwrite_registers(unit=UNIT, **arguments)
        rr = await client.read_holding_registers(1, 8, unit=UNIT)
        assert rq.function_code < 0x80
        assert rq.registers == [77] * 8
        assert rr.registers == [77] * 8  # 将期望值与保持寄存器读回值对比
        # --------------------------------------------------------------------------- #
        # --------------------------------------------------------------------------- #
        log.debug("\nINFO : 读取输入寄存器测试----读取输入寄存器起始地址为1,读取8个值,读取的从机地址为0x01")
        # read_input_registers 读取输入寄存器功能,参数:输入寄存器的起始地址、读取输入寄存器的数量、从机地址
        rr = await client.read_input_registers(1, 8, unit=UNIT)
        assert rq.function_code < 0x80
    except Exception as e:
        log.exception(e)
        client.transport.close()
    await asyncio.sleep(0.5)
    import sys

    sys.exit()


if __name__ == "__main__":
    # 启动主机客户端,监听串行端口'/dev/ttymxc2',串口波特率115200,modbus传输方式rtu
    loop, serial_client = ModbusClient(schedulers.ASYNC_IO,
                                       port="/dev/ttymxc2",
                                       baudrate=115200,
                                       method="rtu")
    loop.run_until_complete(start_async_test(serial_client.protocol))
    loop.close()
        arguments = {
            'read_address':    1,
            'read_count':      8,
            'write_address':   1,
            'write_registers': [20]*8,
        }
        log.debug("Read write registers simulataneously")
        rq = await client.readwrite_registers(unit=UNIT, **arguments)
        rr = await client.read_holding_registers(1, 8, unit=UNIT)
        assert(rq.function_code < 0x80)     # test that we are not an error
        assert(rq.registers == [20]*8)      # test the expected value
        assert(rr.registers == [20]*8)      # test the expected value
    except Exception as e:
        log.exception(e)
        client.transport.close()
    await asyncio.sleep(1)


if __name__ == '__main__':
    # ----------------------------------------------------------------------- #
    # For testing on linux based systems you can use socat to create serial
    # ports
    # ----------------------------------------------------------------------- #
    # socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
    # link=/tmp/ttyp0,raw,echo=0,ospeed=9600
    loop, client = ModbusClient(schedulers.ASYNC_IO, port='/tmp/ptyp0',
                                baudrate=9600, method="rtu")
    loop.run_until_complete(start_async_test(client.protocol))
    loop.close()

Exemplo n.º 6
0
        # arguments = {
        #     'read_address':    1,
        #     'read_count':      8,
        #     'write_address':   1,
        #     'write_registers': [20]*8,
        # }
        # log.debug("Read write registers simulataneously")
        # rq = await client.readwrite_registers(unit=UNIT, **arguments)
        # rr = await client.read_holding_registers(1, 8, unit=UNIT)
        # assert(rq.function_code < 0x80)     # test that we are not an error
        # assert(rq.registers == [20]*8)      # test the expected value
        # assert(rr.registers == [20]*8)      # test the expected value
    except Exception as e:
        log.exception(e)
        client.transport.close()
    await asyncio.sleep(1)


if __name__ == '__main__':
    # ----------------------------------------------------------------------- #
    # For testing on linux based systems you can use socat to create serial
    # ports
    # ----------------------------------------------------------------------- #
    # socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
    # link=/tmp/ttyp0,raw,echo=0,ospeed=9600
    loop, client = ModbusClient(schedulers.ASYNC_IO,
                                port='/dev/tty.usbserial-AK066TL5',
                                baudrate=15200,
                                method="rtu")
    loop.run_until_complete(start_async_test(client.protocol))
    loop.close()