|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Pymodbus Synchronous Client Examples |
| 4 | +-------------------------------------------------------------------------- |
| 5 | +
|
| 6 | +The following is an example of how to use the synchronous modbus client |
| 7 | +implementation from pymodbus. |
| 8 | +
|
| 9 | +It should be noted that the client can also be used with |
| 10 | +the guard construct that is available in python 2.5 and up:: |
| 11 | +
|
| 12 | + with ModbusClient('127.0.0.1') as client: |
| 13 | + result = client.read_coils(1,10) |
| 14 | + print result |
| 15 | +""" |
| 16 | +import struct |
| 17 | +# --------------------------------------------------------------------------- # |
| 18 | +# import the various server implementations |
| 19 | +# --------------------------------------------------------------------------- # |
| 20 | +from pymodbus.pdu import ModbusRequest, ModbusResponse, ModbusExceptions |
| 21 | +from pymodbus.client.sync import ModbusTcpClient as ModbusClient |
| 22 | +from pymodbus.bit_read_message import ReadCoilsRequest |
| 23 | +from pymodbus.compat import int2byte, byte2int |
| 24 | +# --------------------------------------------------------------------------- # |
| 25 | +# configure the client logging |
| 26 | +# --------------------------------------------------------------------------- # |
| 27 | +import logging |
| 28 | +logging.basicConfig() |
| 29 | +log = logging.getLogger() |
| 30 | +log.setLevel(logging.DEBUG) |
| 31 | + |
| 32 | +# --------------------------------------------------------------------------- # |
| 33 | +# create your custom message |
| 34 | +# --------------------------------------------------------------------------- # |
| 35 | +# The following is simply a read coil request that always reads 16 coils. |
| 36 | +# Since the function code is already registered with the decoder factory, |
| 37 | +# this will be decoded as a read coil response. If you implement a new |
| 38 | +# method that is not currently implemented, you must register the request |
| 39 | +# and response with a ClientDecoder factory. |
| 40 | +# --------------------------------------------------------------------------- # |
| 41 | + |
| 42 | + |
| 43 | +class CustomModbusResponse(ModbusResponse): |
| 44 | + function_code = 55 |
| 45 | + _rtu_byte_count_pos = 2 |
| 46 | + |
| 47 | + def __init__(self, values=None, **kwargs): |
| 48 | + ModbusResponse.__init__(self, **kwargs) |
| 49 | + self.values = values or [] |
| 50 | + |
| 51 | + def encode(self): |
| 52 | + """ Encodes response pdu |
| 53 | +
|
| 54 | + :returns: The encoded packet message |
| 55 | + """ |
| 56 | + result = int2byte(len(self.values) * 2) |
| 57 | + for register in self.values: |
| 58 | + result += struct.pack('>H', register) |
| 59 | + return result |
| 60 | + |
| 61 | + def decode(self, data): |
| 62 | + """ Decodes response pdu |
| 63 | +
|
| 64 | + :param data: The packet data to decode |
| 65 | + """ |
| 66 | + byte_count = byte2int(data[0]) |
| 67 | + self.values = [] |
| 68 | + for i in range(1, byte_count + 1, 2): |
| 69 | + self.values.append(struct.unpack('>H', data[i:i + 2])[0]) |
| 70 | + |
| 71 | + |
| 72 | +class CustomModbusRequest(ModbusRequest): |
| 73 | + |
| 74 | + function_code = 55 |
| 75 | + _rtu_frame_size = 8 |
| 76 | + |
| 77 | + def __init__(self, address=None, **kwargs): |
| 78 | + ModbusRequest.__init__(self, **kwargs) |
| 79 | + self.address = address |
| 80 | + self.count = 16 |
| 81 | + |
| 82 | + def encode(self): |
| 83 | + return struct.pack('>HH', self.address, self.count) |
| 84 | + |
| 85 | + def decode(self, data): |
| 86 | + self.address, self.count = struct.unpack('>HH', data) |
| 87 | + |
| 88 | + def execute(self, context): |
| 89 | + if not (1 <= self.count <= 0x7d0): |
| 90 | + return self.doException(ModbusExceptions.IllegalValue) |
| 91 | + if not context.validate(self.function_code, self.address, self.count): |
| 92 | + return self.doException(ModbusExceptions.IllegalAddress) |
| 93 | + values = context.getValues(self.function_code, self.address, |
| 94 | + self.count) |
| 95 | + return CustomModbusResponse(values) |
| 96 | + |
| 97 | +# --------------------------------------------------------------------------- # |
| 98 | +# This could also have been defined as |
| 99 | +# --------------------------------------------------------------------------- # |
| 100 | + |
| 101 | + |
| 102 | +class Read16CoilsRequest(ReadCoilsRequest): |
| 103 | + |
| 104 | + def __init__(self, address, **kwargs): |
| 105 | + """ Initializes a new instance |
| 106 | +
|
| 107 | + :param address: The address to start reading from |
| 108 | + """ |
| 109 | + ReadCoilsRequest.__init__(self, address, 16, **kwargs) |
| 110 | + |
| 111 | +# --------------------------------------------------------------------------- # |
| 112 | +# execute the request with your client |
| 113 | +# --------------------------------------------------------------------------- # |
| 114 | +# using the with context, the client will automatically be connected |
| 115 | +# and closed when it leaves the current scope. |
| 116 | +# --------------------------------------------------------------------------- # |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + with ModbusClient(host='localhost', port=5020) as client: |
| 121 | + client.register(CustomModbusResponse) |
| 122 | + request = CustomModbusRequest(1, unit=1) |
| 123 | + result = client.execute(request) |
| 124 | + print(result.values) |
0 commit comments