1919from pymodbus .datastore import ModbusSequentialDataBlock
2020from pymodbus .datastore import ModbusSlaveContext , ModbusServerContext
2121
22- from pymodbus .transaction import ModbusRtuFramer
22+ from pymodbus .transaction import ModbusRtuFramer , ModbusBinaryFramer
2323# --------------------------------------------------------------------------- #
2424# configure the service logging
2525# --------------------------------------------------------------------------- #
2626import logging
27- logging .basicConfig ()
27+ FORMAT = '%(asctime)-15s %(levelname)-8s %(module)-8s:%(lineno)-8s %(message)s'
28+ logging .basicConfig (format = FORMAT )
2829log = logging .getLogger ()
2930log .setLevel (logging .DEBUG )
3031
32+ SERIAL_PORT = "/tmp/ptyp0"
33+ TCP_PORT = 5020
34+
3135
3236def run_server ():
3337 # ----------------------------------------------------------------------- #
@@ -85,11 +89,11 @@ def run_server():
8589 # store = ModbusSlaveContext(..., zero_mode=True)
8690 # ----------------------------------------------------------------------- #
8791 store = ModbusSlaveContext (
88- di = ModbusSequentialDataBlock (0 , [17 ]* 100 ),
89- co = ModbusSequentialDataBlock (0 , [17 ]* 100 ),
90- hr = ModbusSequentialDataBlock (0 , [17 ]* 100 ),
91- ir = ModbusSequentialDataBlock (0 , [17 ]* 100 ))
92- context = ModbusServerContext (slaves = store , single = True )
92+ di = ModbusSequentialDataBlock (0 , [17 ]* 100 ),
93+ co = ModbusSequentialDataBlock (0 , [17 ]* 100 ),
94+ hr = ModbusSequentialDataBlock (0 , [17 ]* 100 ),
95+ ir = ModbusSequentialDataBlock (0 , [17 ]* 100 ))
96+ context = ModbusServerContext (slaves = { 1 : store , 2 : store }, single = False )
9397
9498 # ----------------------------------------------------------------------- #
9599 # initialize the server information
@@ -108,18 +112,23 @@ def run_server():
108112 # run the server you want
109113 # ----------------------------------------------------------------------- #
110114 # Tcp:
111- StartTcpServer (context , identity = identity , address = ("localhost" , 5020 ))
115+ StartTcpServer (context , identity = identity , address = ("localhost" , TCP_PORT ))
112116
113117 # Udp:
114- # StartUdpServer(context, identity=identity, address=("localhost", 502))
118+ # StartUdpServer(context, identity=identity,
119+ # address=("localhost", TCP_PORT))
115120
116121 # Ascii:
117- # StartSerialServer(context, identity=identity,
118- # port='/dev/pts/3', timeout=1)
119-
122+ # StartSerialServer(context, identity=identity,
123+ # port=SERIAL_PORT, timeout=1)
124+
125+ # Binary:
126+ # StartSerialServer(context, identity=identity, framer=ModbusBinaryFramer,
127+ # port=SERIAL_PORT, timeout=1)
128+
120129 # RTU:
121130 # StartSerialServer(context, framer=ModbusRtuFramer, identity=identity,
122- # port='/dev/ptyp0' , timeout=.005, baudrate=9600)
131+ # port=SERIAL_PORT , timeout=.005, baudrate=9600)
123132
124133
125134if __name__ == "__main__" :
0 commit comments