77twisted library as its backend. This allows it to scale to many thousands
88of nodes which can be helpful for testing monitoring software.
99"""
10+
1011# --------------------------------------------------------------------------- #
1112# import the various server implementations
1213# --------------------------------------------------------------------------- #
13- from pymodbus .server import StartTcpServer , ServerStop
14-
14+ from pymodbus .datastore import ModbusDeviceContext , ModbusSequentialDataBlock , ModbusServerContext
1515from pymodbus .pdu .device import ModbusDeviceIdentification
16- from pymodbus .datastore import ModbusSequentialDataBlock
17- from pymodbus .datastore import ModbusDeviceContext , ModbusServerContext
16+ from pymodbus .server import ServerStop , StartTcpServer
1817
1918
2019class MockModbusServer (object ):
2120 # --------------------------------------------------------------------------- #
2221 # configure the service logging
2322 # --------------------------------------------------------------------------- #
2423 import logging
25- FORMAT = ( '%(asctime)-15s %(threadName)-15s'
26- ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s' )
24+
25+ FORMAT = "%(asctime)-15s %(threadName)-15s %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
2726 logging .basicConfig (format = FORMAT )
2827 log = logging .getLogger ()
2928 log .setLevel (logging .DEBUG )
@@ -83,9 +82,7 @@ def run_async_server(self):
8382 #
8483 # store = ModbusDeviceContext(..., zero_mode=True)
8584 # ----------------------------------------------------------------------- #
86- store = ModbusDeviceContext (
87- hr = ModbusSequentialDataBlock (0 , [0 ]* 3000 ),
88- ir = ModbusSequentialDataBlock (0 , [0 ]* 3000 ))
85+ store = ModbusDeviceContext (hr = ModbusSequentialDataBlock (0 , [0 ] * 3000 ), ir = ModbusSequentialDataBlock (0 , [0 ] * 3000 ))
8986 self .context = ModbusServerContext (devices = store , single = True )
9087
9188 # ----------------------------------------------------------------------- #
@@ -94,12 +91,12 @@ def run_async_server(self):
9491 # If you don't set this or any fields, they are defaulted to empty strings.
9592 # ----------------------------------------------------------------------- #
9693 identity = ModbusDeviceIdentification ()
97- identity .VendorName = ' Pymodbus'
98- identity .ProductCode = 'PM'
99- identity .VendorUrl = ' http://github.com/bashwork/pymodbus/'
100- identity .ProductName = ' Pymodbus Server'
101- identity .ModelName = ' Pymodbus Server'
102- identity .MajorMinorRevision = ' 1.5'
94+ identity .VendorName = " Pymodbus"
95+ identity .ProductCode = "PM"
96+ identity .VendorUrl = " http://github.com/bashwork/pymodbus/"
97+ identity .ProductName = " Pymodbus Server"
98+ identity .ModelName = " Pymodbus Server"
99+ identity .MajorMinorRevision = " 1.5"
103100
104101 # ----------------------------------------------------------------------- #
105102 # run the server you want
@@ -112,7 +109,7 @@ def stop_async_server(self):
112109 ServerStop ()
113110
114111 def update_context (self , register , address , values ):
115- """ Update values of the active context. It should be noted
112+ """Update values of the active context. It should be noted
116113 that there is a race condition for the update.
117114
118115 :param register: Type of register to update,
@@ -123,30 +120,26 @@ def update_context(self, register, address, values):
123120 """
124121 assert register == 3 or register == 4
125122 device_id = 0x00
126- old_values = self .context [device_id ].getValues (register ,
127- address , count = 1 )
128- self .log .debug ("Change value at address {} from {} to {}" .format (
129- address , old_values , values ))
123+ old_values = self .context [device_id ].getValues (register , address , count = 1 )
124+ self .log .debug ("Change value at address {} from {} to {}" .format (address , old_values , values ))
130125 self .context [device_id ].setValues (register , address , values )
131126
132127 def update_holding_register (self , address , value ):
133- """ Update value of a holding register.
128+ """Update value of a holding register.
134129
135130 :param address: Address to update
136131 :param value: Value to save
137132 """
138- self .log .debug ("Update holding register: {}:{}" .format (address ,
139- int (value )))
133+ self .log .debug ("Update holding register: {}:{}" .format (address , int (value )))
140134 self .update_context (3 , address , [int (value )])
141135
142136 def update_input_register (self , address , value ):
143- """ Update value of an input register.
137+ """Update value of an input register.
144138
145139 :param address: Address to update
146140 :param value: Value to save
147141 """
148- self .log .debug ("Update input register: {}:{}" .format (address ,
149- int (value )))
142+ self .log .debug ("Update input register: {}:{}" .format (address , int (value )))
150143 self .update_context (4 , address , [int (value )])
151144
152145
0 commit comments