|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Pymodbus Asyncio Server Example |
| 4 | +-------------------------------------------------------------------------- |
| 5 | +
|
| 6 | +The asyncio server is implemented in pure python without any third |
| 7 | +party libraries (unless you need to use the serial protocols which require |
| 8 | +asyncio-pyserial). This is helpful in constrained or old environments where using |
| 9 | +twisted is just not feasible. What follows is an example of its use: |
| 10 | +""" |
| 11 | +# --------------------------------------------------------------------------- # |
| 12 | +# import the various server implementations |
| 13 | +# --------------------------------------------------------------------------- # |
| 14 | +import asyncio |
| 15 | +from pymodbus.server.asyncio import StartTcpServer |
| 16 | +from pymodbus.server.asyncio import StartUdpServer |
| 17 | +from pymodbus.server.asyncio import StartSerialServer |
| 18 | + |
| 19 | +from pymodbus.device import ModbusDeviceIdentification |
| 20 | +from pymodbus.datastore import ModbusSequentialDataBlock, ModbusSparseDataBlock |
| 21 | +from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext |
| 22 | + |
| 23 | +from pymodbus.transaction import ModbusRtuFramer, ModbusBinaryFramer |
| 24 | +# --------------------------------------------------------------------------- # |
| 25 | +# configure the service logging |
| 26 | +# --------------------------------------------------------------------------- # |
| 27 | +import logging |
| 28 | +FORMAT = ('%(asctime)-15s %(threadName)-15s' |
| 29 | + ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s') |
| 30 | +logging.basicConfig(format=FORMAT) |
| 31 | +log = logging.getLogger() |
| 32 | +log.setLevel(logging.DEBUG) |
| 33 | + |
| 34 | + |
| 35 | +async def run_server(): |
| 36 | + # ----------------------------------------------------------------------- # |
| 37 | + # initialize your data store |
| 38 | + # ----------------------------------------------------------------------- # |
| 39 | + # The datastores only respond to the addresses that they are initialized to |
| 40 | + # Therefore, if you initialize a DataBlock to addresses of 0x00 to 0xFF, a |
| 41 | + # request to 0x100 will respond with an invalid address exception. This is |
| 42 | + # because many devices exhibit this kind of behavior (but not all):: |
| 43 | + # |
| 44 | + # block = ModbusSequentialDataBlock(0x00, [0]*0xff) |
| 45 | + # |
| 46 | + # Continuing, you can choose to use a sequential or a sparse DataBlock in |
| 47 | + # your data context. The difference is that the sequential has no gaps in |
| 48 | + # the data while the sparse can. Once again, there are devices that exhibit |
| 49 | + # both forms of behavior:: |
| 50 | + # |
| 51 | + # block = ModbusSparseDataBlock({0x00: 0, 0x05: 1}) |
| 52 | + # block = ModbusSequentialDataBlock(0x00, [0]*5) |
| 53 | + # |
| 54 | + # Alternately, you can use the factory methods to initialize the DataBlocks |
| 55 | + # or simply do not pass them to have them initialized to 0x00 on the full |
| 56 | + # address range:: |
| 57 | + # |
| 58 | + # store = ModbusSlaveContext(di = ModbusSequentialDataBlock.create()) |
| 59 | + # store = ModbusSlaveContext() |
| 60 | + # |
| 61 | + # Finally, you are allowed to use the same DataBlock reference for every |
| 62 | + # table or you may use a separate DataBlock for each table. |
| 63 | + # This depends if you would like functions to be able to access and modify |
| 64 | + # the same data or not:: |
| 65 | + # |
| 66 | + # block = ModbusSequentialDataBlock(0x00, [0]*0xff) |
| 67 | + # store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) |
| 68 | + # |
| 69 | + # The server then makes use of a server context that allows the server to |
| 70 | + # respond with different slave contexts for different unit ids. By default |
| 71 | + # it will return the same context for every unit id supplied (broadcast |
| 72 | + # mode). |
| 73 | + # However, this can be overloaded by setting the single flag to False and |
| 74 | + # then supplying a dictionary of unit id to context mapping:: |
| 75 | + # |
| 76 | + # slaves = { |
| 77 | + # 0x01: ModbusSlaveContext(...), |
| 78 | + # 0x02: ModbusSlaveContext(...), |
| 79 | + # 0x03: ModbusSlaveContext(...), |
| 80 | + # } |
| 81 | + # context = ModbusServerContext(slaves=slaves, single=False) |
| 82 | + # |
| 83 | + # The slave context can also be initialized in zero_mode which means that a |
| 84 | + # request to address(0-7) will map to the address (0-7). The default is |
| 85 | + # False which is based on section 4.4 of the specification, so address(0-7) |
| 86 | + # will map to (1-8):: |
| 87 | + # |
| 88 | + # store = ModbusSlaveContext(..., zero_mode=True) |
| 89 | + # ----------------------------------------------------------------------- # |
| 90 | + store = ModbusSlaveContext( |
| 91 | + di=ModbusSequentialDataBlock(0, [17]*100), |
| 92 | + co=ModbusSequentialDataBlock(0, [17]*100), |
| 93 | + hr=ModbusSequentialDataBlock(0, [17]*100), |
| 94 | + ir=ModbusSequentialDataBlock(0, [17]*100)) |
| 95 | + |
| 96 | + context = ModbusServerContext(slaves=store, single=True) |
| 97 | + |
| 98 | + # ----------------------------------------------------------------------- # |
| 99 | + # initialize the server information |
| 100 | + # ----------------------------------------------------------------------- # |
| 101 | + # If you don't set this or any fields, they are defaulted to empty strings. |
| 102 | + # ----------------------------------------------------------------------- # |
| 103 | + identity = ModbusDeviceIdentification() |
| 104 | + identity.VendorName = 'Pymodbus' |
| 105 | + identity.ProductCode = 'PM' |
| 106 | + identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' |
| 107 | + identity.ProductName = 'Pymodbus Server' |
| 108 | + identity.ModelName = 'Pymodbus Server' |
| 109 | + identity.MajorMinorRevision = '2.3.0' |
| 110 | + |
| 111 | + # ----------------------------------------------------------------------- # |
| 112 | + # run the server you want |
| 113 | + # ----------------------------------------------------------------------- # |
| 114 | + # Tcp: |
| 115 | + # immediately start serving: |
| 116 | + await StartTcpServer(context, identity=identity, address=("0.0.0.0", 5020), allow_reuse_address=True, |
| 117 | + defer_start=False) |
| 118 | + |
| 119 | + # deferred start: |
| 120 | + # server = await StartTcpServer(context, identity=identity, address=("0.0.0.0", 5020), |
| 121 | + # allow_reuse_address=True, defer_start=True) |
| 122 | + # |
| 123 | + # asyncio.get_event_loop().call_later(20, lambda : server.serve_forever) |
| 124 | + # await server.serve_forever() |
| 125 | + |
| 126 | + # TCP with different framer |
| 127 | + # StartTcpServer(context, identity=identity, |
| 128 | + # framer=ModbusRtuFramer, address=("0.0.0.0", 5020)) |
| 129 | + |
| 130 | + # Udp: |
| 131 | + # server = await StartUdpServer(context, identity=identity, address=("0.0.0.0", 5020), |
| 132 | + # allow_reuse_address=True, defer_start=True) |
| 133 | + # # |
| 134 | + # await server.serve_forever() |
| 135 | + |
| 136 | + # !!! SERIAL SERVER NOT IMPLEMENTED !!! |
| 137 | + # Ascii: |
| 138 | + # StartSerialServer(context, identity=identity, |
| 139 | + # port='/dev/ttyp0', timeout=1) |
| 140 | + |
| 141 | + # RTU: |
| 142 | + # StartSerialServer(context, framer=ModbusRtuFramer, identity=identity, |
| 143 | + # port='/dev/ttyp0', timeout=.005, baudrate=9600) |
| 144 | + |
| 145 | + # Binary |
| 146 | + # StartSerialServer(context, |
| 147 | + # identity=identity, |
| 148 | + # framer=ModbusBinaryFramer, |
| 149 | + # port='/dev/ttyp0', |
| 150 | + # timeout=1) |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + asyncio.run(run_server()) |
| 155 | + |
0 commit comments