|
8 | 8 | server_async.py [-h] [--comm {tcp,udp,serial,tls}] |
9 | 9 | [--framer {ascii,rtu,socket,tls}] |
10 | 10 | [--log {critical,error,warning,info,debug}] |
11 | | - [--port PORT] [--store {sequential,sparse,factory,none}] |
| 11 | + [--port PORT] |
12 | 12 | [--device_ids DEVICE_IDS] |
13 | 13 |
|
14 | 14 | -h, --help |
|
22 | 22 | -p, --port PORT |
23 | 23 | set port |
24 | 24 | set serial device baud rate |
25 | | - --store {sequential,sparse,factory,none} |
26 | | - set datastore type |
27 | 25 | --device_ids DEVICE IDs |
28 | 26 | set list of devices to respond to |
29 | 27 |
|
|
35 | 33 | import asyncio |
36 | 34 | import logging |
37 | 35 | import sys |
38 | | -from collections.abc import Callable |
39 | | -from typing import Any |
40 | 36 |
|
41 | 37 |
|
42 | 38 | try: |
43 | 39 | import helper # type: ignore[import-not-found] |
44 | 40 | except ImportError: |
45 | | - print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ |
| 41 | + print("*** ERROR --> THIS EXAMPLE needs to be run in the example directory, please see \n\ |
46 | 42 | https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ |
47 | 43 | for more information.") |
48 | 44 | sys.exit(-1) |
49 | 45 |
|
50 | 46 | from pymodbus import ModbusDeviceIdentification |
51 | 47 | from pymodbus import __version__ as pymodbus_version |
52 | | -from pymodbus.datastore import ( |
53 | | - ModbusDeviceContext, |
54 | | - ModbusSequentialDataBlock, |
55 | | - ModbusServerContext, |
56 | | - ModbusSparseDataBlock, |
57 | | -) |
58 | 48 | from pymodbus.server import ( |
59 | 49 | StartAsyncSerialServer, |
60 | 50 | StartAsyncTcpServer, |
61 | 51 | StartAsyncTlsServer, |
62 | 52 | StartAsyncUdpServer, |
63 | 53 | ) |
| 54 | +from pymodbus.simulator import DataType, SimData, SimDevice |
64 | 55 |
|
65 | 56 |
|
66 | 57 | _logger = logging.getLogger(__file__) |
|
70 | 61 | def setup_server(description=None, context=None, cmdline=None): |
71 | 62 | """Run server setup.""" |
72 | 63 | args = helper.get_commandline(server=True, description=description, cmdline=cmdline) |
73 | | - if context: |
| 64 | + if context: # pragma: no cover |
74 | 65 | args.context = context |
75 | | - datablock: Callable[[], Any] |
76 | | - if not args.context: |
| 66 | + if not args.context: # pragma: no cover |
77 | 67 | _logger.info("### Create datastore") |
78 | | - # The datastores only respond to the addresses that are initialized |
79 | | - # If you initialize a DataBlock to addresses of 0x00 to 0xFF, a request to |
80 | | - # 0x100 will respond with an invalid address exception. |
81 | | - # This is because many devices exhibit this kind of behavior (but not all) |
82 | | - if args.store == "sequential": # pragma: no cover |
83 | | - # Continuing, use a sequential block without gaps. |
84 | | - datablock = lambda : ModbusSequentialDataBlock(0x00, [17] * 100) # pylint: disable=unnecessary-lambda-assignment |
85 | | - elif args.store == "sparse": # pragma: no cover |
86 | | - # Continuing, or use a sparse DataBlock which can have gaps |
87 | | - datablock = lambda : ModbusSparseDataBlock({0x00: 0, 0x05: 1}) # pylint: disable=unnecessary-lambda-assignment |
88 | | - elif args.store == "factory" or True: # pragma: no cover # pylint: disable=condition-evals-to-constant |
89 | | - # Alternately, use the factory methods to initialize the DataBlocks |
90 | | - # or simply do not pass them to have them initialized to 0x00 on the |
91 | | - # full address range:: |
92 | | - datablock = lambda : ModbusSequentialDataBlock(0x00, [0x00] * 65536) # pylint: disable=unnecessary-lambda-assignment |
93 | 68 |
|
| 69 | + # Build data storage |
94 | 70 | if args.device_ids > 1: # pragma: no cover |
95 | | - # The server then makes use of a server context that allows the server |
96 | | - # to respond with different device contexts for different device ids. |
97 | | - # By default it will return the same context for every device id supplied |
98 | | - # (broadcast mode). |
99 | | - # However, this can be overloaded by setting the single flag to False and |
100 | | - # then supplying a dictionary of device id to context mapping:: |
101 | | - context = { |
102 | | - device_id : ModbusDeviceContext( |
103 | | - di=datablock(), |
104 | | - co=datablock(), |
105 | | - hr=datablock(), |
106 | | - ir=datablock(), |
107 | | - ) |
108 | | - for device_id in range(args.device_ids) |
109 | | - } |
110 | | - |
111 | | - single = False |
| 71 | + args.context = [SimDevice(device_id, SimData(0, datatype=DataType.REGISTERS, values=[17]*100)) |
| 72 | + for device_id in range(args.device_ids)] |
112 | 73 | else: |
113 | | - context = ModbusDeviceContext( |
114 | | - di=datablock(), co=datablock(), hr=datablock(), ir=datablock() |
115 | | - ) |
116 | | - single = True |
117 | | - |
118 | | - # Build data storage |
119 | | - args.context = ModbusServerContext(devices=context, single=single) |
| 74 | + args.context = SimDevice(0, SimData(0, datatype=DataType.REGISTERS, values=[17]*100)) |
120 | 75 |
|
121 | 76 | # ----------------------------------------------------------------------- # |
122 | 77 | # initialize the server information |
|
0 commit comments