def run_payload_server(): # ----------------------------------------------------------------------- # # build your payload # ----------------------------------------------------------------------- # builder = BinaryPayloadBuilder(byteorder=Endian.Little, wordorder=Endian.Little) builder.add_string('abcdefgh') builder.add_bits([0, 1, 0, 1, 1, 0, 1, 0]) builder.add_8bit_int(-0x12) builder.add_8bit_uint(0x12) builder.add_16bit_int(-0x5678) builder.add_16bit_uint(0x1234) builder.add_32bit_int(-0x1234) builder.add_32bit_uint(0x12345678) builder.add_32bit_float(22.34) builder.add_32bit_float(-22.34) builder.add_64bit_int(-0xDEADBEEF) builder.add_64bit_uint(0x12345678DEADBEEF) builder.add_64bit_uint(0xDEADBEEFDEADBEED) builder.add_64bit_float(123.45) builder.add_64bit_float(-123.45) # ----------------------------------------------------------------------- # # use that payload in the data store # ----------------------------------------------------------------------- # # Here we use the same reference block for each underlying store. # ----------------------------------------------------------------------- # block = ModbusSequentialDataBlock(1, builder.to_registers()) store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # # If you don't set this or any fields, they are defaulted to empty strings. # ----------------------------------------------------------------------- # identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.5' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # StartTcpServer(context, identity=identity, address=("localhost", 5020))
def Mod(): import time from pymodbus.server.sync import StartTcpServer from pymodbus.device import ModbusDeviceIdentification from pymodbus.datastore import ModbusSequentialDataBlock from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.0' #Run Modbus server using shared variable 'context' StartTcpServer(context, identity=identity, address=("127.0.0.1", 502))
def run_server(): block = ModbusSequentialDataBlock.create() store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.5' StartTcpServer(context, identity=identity, address=("0.0.0.0", 502))
def run_server(publisher): block = CustomDataBlock([0] * 100, publisher) store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '2.3.0' print("Modbus server started") StartTcpServer(context, identity=identity, address=("0.0.0.0", 5022), allow_reuse_address=True)
def run_server(): store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [17] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100), ) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = "Pymodbus" identity.ProductCode = "PM" identity.VendorUrl = "http://github.com/riptideio/pymodbus/" identity.ProductName = "Pymodbus Server" identity.ModelName = "Pymodbus Server" identity.MajorMinorRevision = version.short() StartTcpServer(context, identity=identity, address=("127.0.0.1", 5020))
def run_payload_server(): """ Creates the Modbus server and starts it. """ store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [17] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100)) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'AWS Greengrass Samples' identity.ProductCode = 'AWS' identity.VendorUrl = 'https//github.com/aws-samples/aws-greengrass-samples' identity.ProductName = 'Sample Local Server' identity.ModelName = 'LocalServer' identity.MajorMinorRevision = '1.0' StartTcpServer(context, identity=identity, address=("127.0.0.1", 5020))
def run_server(self, callback=None, debug=True): """Runs the modbus tcp or rtu server with given register information. if increment is true, the register values are dynamic and incrementing by one every interval provided in cycle_s argument""" if debug: logging.basicConfig() log = logging.getLogger() log.setLevel(logging.DEBUG) try: #Data callback function will be executed as a separate thread if callback != None: thread = Thread(target=callback, args=(self.context, ), daemon=True) thread.start() if self.xmlData.get('modbusType') == 'tcp/ip': print( f"Running server on IP: {self.xmlData.get('ip')} and port {self.xmlData.get('port')}" ) StartTcpServer(self.context, identity=self.deviceIdentity, address=(self.xmlData.get('ip'), self.xmlData.get('port'))) elif self.xmlData.get('modbusType') == 'rtu': print( f"Running server on COM: {self.xmlData.get('com')} and baudrate {self.xmlData.get('baud')}" ) StartSerialServer(self.context, timeout=self.xmlData.get('timeout'), framer=ModbusRtuFramer, identity=self.deviceIdentity, port=self.xmlData.get('com'), stopbits=self.xmlData.get('stopbits'), bytesize=self.xmlData.get('bytesize'), parity=self.xmlData.get('parity'), baudrate=self.xmlData.get('baud')) except KeyboardInterrupt: print('Server stopped')
def init(): slaves = { UNIT: ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [0]*50), co=ModbusSequentialDataBlock(0, [0]*50), hr=ModbusSequentialDataBlock(0, [0]*50), ir=ModbusSequentialDataBlock(0, [0]*50) ) } context = ModbusServerContext(slaves=slaves, single=False) identity = ModbusDeviceIdentification() identity.VendorName = 'HAXOM' identity.ProductCode = 'SIMU-ICS-EOLIENNE' identity.VendorUrl = 'https://github.com/haxom/' identity.ProductName = 'SIMU-ICS' identity.ModelName = 'EOLIENNE' identity.MajorMinorRevision = '1.0.0' print 'Launching Modbus server, listening on %s:%d' % (listen_int, listen_port) StartTcpServer(context, identity=identity, address=(listen_int, listen_port))
def run_server(): store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [5] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100)) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.5' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # # Tcp: StartTcpServer(context, identity=identity, address=("localhost", 5020))
def run_server(): data_list = turn_float_int(simulate_env()) log.debug("register data is {}".format(data_list)) store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [17] + data_list), ## start at index 1 not 0 # hr=ModbusSequentialDataBlock(0, [20.1,20.1,20.1,20.1,20.1]), ir=ModbusSequentialDataBlock(0, [17] * 100)) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.5' StartTcpServer(context, identity=identity, address=("192.168.0.110", 5020))
def init(): slaves = { UNIT: ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [17] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100)) } context = ModbusServerContext(slaves=slaves, single=False) identity = ModbusDeviceIdentification() identity.VendorName = 'HAXOM' identity.ProductCode = 'SIMU-ICS-TEMPLATE' identity.VendorUrl = 'https://github.com/haxom/' identity.ProductName = 'SIMU-ICS' identity.ModelName = 'TEMPLATE' identity.MajorMinorRevision = '1.0.0' print(f'Modbus slave launched on {listen_int}:{listen_port}') StartTcpServer(context, identity=identity, address=(listen_int, listen_port))
def run_server(): cc_store = ModbusSlaveContext( di = ModbusSequentialDataBlock(0, [30] * 100), # discrete input space co = ModbusSequentialDataBlock(0, [40] * 100), # c o space hr = ModbusSequentialDataBlock(0, [0] * 81) , # ir = ModbusSequentialDataBlock(0, [50] * 100), ) # input registers space rb_store = ModbusSlaveContext( di = ModbusSequentialDataBlock(0, [30] * 100), # discrete input space co = ModbusSequentialDataBlock(0, [40] * 100), # c o space hr = ModbusSequentialDataBlock(0, [1] * 20) , # ir = ModbusSequentialDataBlock(0, [50] * 100), ) # input registers space store = {0x01 : cc_store , 0x09 : rb_store } context = ModbusServerContext(slaves=store, single=False) identity = ModbusDeviceIdentification() identity.VendorName = 'Gionji' identity.ProductCode = 'AGB' identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server Sim' identity.ModelName = 'Pymodbus Sim ' identity.MajorMinorRevision = '0.1' # UPDATER time = 1 updater = updaterThread(context, updating_writer) updater.start() #updating_writer(context) StartTcpServer(context, identity=identity, address=("", 5020))
def run_server(): store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [0] * 10000), co=ModbusSequentialDataBlock(0, [0] * 10000), hr=ModbusSequentialDataBlock(0, [0] * 10000), ir=ModbusSequentialDataBlock(0, [0] * 10000)) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' # identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.5' #----------------------------------------------------- print("debug started\n\n") print(dir(store)) store.setValues(3, 53, [30000]) print("\n\ndebug finished") #----------------------------------------------------- print("Listening on {}:{}".format(HOST, PORT)) StartTcpServer(context, identity=identity, address=(HOST, PORT))
def run_server(): slaves = { 0x01: ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [17]*100), co=ModbusSequentialDataBlock(0, [17]*100), hr=ModbusSequentialDataBlock(0, [17]*100), ir=ModbusSequentialDataBlock(0, [17]*100)), 0x02: ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [17]*100), co=ModbusSequentialDataBlock(0, [17]*100), hr=ModbusSequentialDataBlock(0, [17]*100), ir=ModbusSequentialDataBlock(0, [17]*100)), 0x03: ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [17]*100), co=ModbusSequentialDataBlock(0, [17]*100), hr=ModbusSequentialDataBlock(0, [17]*100), ir=ModbusSequentialDataBlock(0, [17]*100)), } context = ModbusServerContext(slaves=slaves, single=False) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # # If you don't set this or any fields, they are defaulted to empty strings. # ----------------------------------------------------------------------- # identity = ModbusDeviceIdentification() identity.VendorName = 'KMU' identity.ProductCode = 'PM' identity.VendorUrl = '' identity.ProductName = 'For packet sniffing test' identity.ModelName = '' identity.MajorMinorRevision = '2.3.0' StartTcpServer(context, identity=identity, address=("192.168.70.128", 502))
store = ModbusSlaveContext(di=di, co=co, hr=hr, ir=ir) context = ModbusServerContext(slaves=store, single=True) return context #---------------------------------------------------------------------------# # initialize the server information #---------------------------------------------------------------------------# # If you don't set this or any fields, they are defaulted to empty strings. #---------------------------------------------------------------------------# identity = ModbusDeviceIdentification() identity.VendorName = 'VOLTTRON' identity.ProductCode = 'VT' identity.VendorUrl = 'http://github.com/VOLTTRON/volttron' identity.ProductName = 'VOLTTRON Modbus Test Device' identity.ModelName = 'VOLTTRON Modbus Test Device' identity.MajorMinorRevision = '1.0' abstraction = DeviceAbstraction(args.config) #Create the deamon as soon as we've loaded the device configuration. createDaemon() context = abstraction.get_server_context() #---------------------------------------------------------------------------# # run the server you want #---------------------------------------------------------------------------# StartTcpServer(context, identity=identity, address=(args.interface, args.port))
simulated_modbus_server_identity.VendorName = 'pymodbus' simulated_modbus_server_identity.ProductCode = 'PM' simulated_modbus_server_identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' simulated_modbus_server_identity.ProductName = 'pymodbus Server' simulated_modbus_server_identity.ModelName = 'pymodbus Server' simulated_modbus_server_identity.MajorMinorRevision = '1.0' try: # Print Bluetooth scanning status log.debug("'Bluetooth device scanning enabled' setting:") log.debug(BLUETOOTH_DEVICE_SCANNING_ENABLED) # Initialize the thread that will start updating our Modbus server's registers CONTINUE_UPDATING_MODBUS_REGISTERS = True update_registers_thread = Thread(target=update_modbus_registers, args=(simulated_modbus_server_context, )) update_registers_thread.daemon = True update_registers_thread.start() # Run the server log.debug("Starting Modbus server; press CTRL+C or CTRL+Z to exit...") StartTcpServer(simulated_modbus_server_context, identity=simulated_modbus_server_identity, address=(MODBUS_SERVER_ADDRESS, MODBUS_SERVER_PORT)) except (KeyboardInterrupt, SystemExit): log.debug("Stopping Modbus server...") log.debug("Stopping update thread...") CONTINUE_UPDATING_MODBUS_REGISTERS = False log.debug("Script terminated.")
# False which is based on section 4.4 of the specification, so address(0-7) # will map to (1-8):: # # store = ModbusSlaveContext(..., zero_mode=True) # ----------------------------------------------------------------------- # store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [17] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100)) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # # If you don't set this or any fields, they are defaulted to empty strings. # ----------------------------------------------------------------------- # identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.5' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # # Tcp: StartTcpServer(context, identity=identity, address=("localhost", 5020))
#!/usr/bin/python #RTU Server from config import * from pymodbus.server.sync import StartTcpServer from pymodbus.device import ModbusDeviceIdentification from pymodbus.datastore import ModbusSequentialDataBlock from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.INFO) store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [0] * 100)) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() StartTcpServer(context, identity=identity, address=(FAKE_FIELD_IP, FAKE_FIELD_PORT))
def run_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # # The datastores only respond to the addresses that they are initialized to # Therefore, if you initialize a DataBlock to addresses of 0x00 to 0xFF, a # request to 0x100 will respond with an invalid address exception. This is # because many devs exhibit this kind of behavior (but not all):: # # block = ModbusSequentialDataBlock(0x00, [0]*0xff) # # Continuing, you can choose to use a sequential or a sparse DataBlock in # your data context. The difference is that the sequential has no gaps in # the data while the sparse can. Once again, there are devs that exhibit # both forms of behavior:: # # block = ModbusSparseDataBlock({0x00: 0, 0x05: 1}) # block = ModbusSequentialDataBlock(0x00, [0]*5) # # Alternately, you can use the factory methods to initialize the DataBlocks # or simply do not pass them to have them initialized to 0x00 on the full # address range:: # # store = ModbusSlaveContext(di = ModbusSequentialDataBlock.create()) # store = ModbusSlaveContext() # # Finally, you are allowed to use the same DataBlock reference for every # table or you may use a separate DataBlock for each table. # This depends if you would like functions to be able to access and modify # the same data or not:: # # block = ModbusSequentialDataBlock(0x00, [0]*0xff) # store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) # # The server then makes use of a server context that allows the server to # respond with different slave contexts for different unit ids. By default # it will return the same context for every unit id supplied (broadcast # mode). # However, this can be overloaded by setting the single flag to False and # then supplying a dictionary of unit id to context mapping:: # # slaves = { # 0x01: ModbusSlaveContext(...), # 0x02: ModbusSlaveContext(...), # 0x03: ModbusSlaveContext(...), # } # context = ModbusServerContext(slaves=slaves, single=False) # # The slave context can also be initialized in zero_mode which means that a # request to address(0-7) will map to the address (0-7). The default is # False which is based on section 4.4 of the specification, so address(0-7) # will map to (1-8):: # # store = ModbusSlaveContext(..., zero_mode=True) # ----------------------------------------------------------------------- # store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [17] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100)) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # # If you don't set this or any fields, they are defaulted to empty strings. # ----------------------------------------------------------------------- # identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '2.3.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # # Tcp: # StartTcpServer(context, identity=identity, address=("192.168.0.119", 5020)) StartTcpServer(context, identity=identity, address=("localhost", 5020))
def testStartTcpServer(self): ''' Test the tcp server starting factory ''' with patch.object(ModbusTcpServer, 'serve_forever') as mock_server: with patch.object(socketserver.TCPServer, 'server_bind') as mock_binder: StartTcpServer()
builder_co.add_bits([0, 0, 0, 0, 0, 0, 0, 0]) # temperature builder_hr = BinaryPayloadBuilder() builder_hr.add_8bit_int(70) # wanted to use input registers for temperature, but they are read only #builder_ir = BinaryPayloadBuilder() #builder_ir.add_8bit_int(70) #di_block = ModbusSequentialDataBlock(0x01, builder_di.to_coils()) co_block = ModbusSequentialDataBlock(0x101, builder_co.to_coils()) hr_block = ModbusSequentialDataBlock(0x201, builder_hr.to_registers()) #ir_block = ModbusSequentialDataBlock(0x301, builder_ir.to_registers()) #store = ModbusSlaveContext(di=di_block, co=co_block, hr=hr_block, ir=ir_block) store = ModbusSlaveContext(co=co_block, hr=hr_block) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '2.2.0' address = ("localhost", 5020) print("Server started at {}".format(address)) StartTcpServer(context, identity=identity, address=address)
if __name__ == '__main__': update_reg = UpdateRegisters(context) update_reg.start() def terminate_self(): log.info("Stopping the simulator") try: update_reg.stop() except: log.exception("Error stopping the simulator gracefully.") log.info("Killing self..") os.kill(os.getpid(), 9) try: # Tcp: StartTcpServer(context, identity=identity, address=("0.0.0.0", 502)) except KeyboardInterrupt: terminate_self() except Exception as ex: log.exception("Caught exception! Terminating..") terminate_self() # Udp: #StartUdpServer(context, identity=identity, address=("localhost", 502)) # Ascii: #StartSerialServer(context, identity=identity, port='/dev/pts/3', timeout=1) # RTU: #StartSerialServer(context, framer=ModbusRtuFramer, identity=identity, port='/dev/pts/3', timeout=.005)
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Set the logging level (default: %(default)s)', default='INFO') parser.add_argument('--host', dest='host', default='localhost', help='Host to serve on (default: %(default)s)') parser.add_argument('--port', dest='port', type=int, default=5020, help='Port to serve on (default: %(default)i)') args = parser.parse_args() FORMAT = ('%(asctime)-15s %(threadName)-15s' ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s') logging.basicConfig(level=logging.getLevelName(args.loglevel), format=FORMAT) from piplate_modbus.contexts import DAQCPlateIdentification, get_server_context logging.getLogger(__name__).info('Starting server on {}:{}'.format( args.host, args.port)) StartTcpServer(context=get_server_context(), identity=DAQCPlateIdentification, address=(args.host, args.port))
def thread_modbus_server(): StartTcpServer(context, address=(tcpipvals["modServerIp"], tcpipvals["modServerPort"]))
ir=ModbusSequentialDataBlock(0, [17] * 100)) context = ModbusServerContext(slaves=store, single=True) #---------------------------------------------------------------------------# # initialize the server information #---------------------------------------------------------------------------# # If you don't set this or any fields, they are defaulted to empty strings. #---------------------------------------------------------------------------# identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/riptideio/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.0' #---------------------------------------------------------------------------# # run the server you want #---------------------------------------------------------------------------# # Tcp: StartTcpServer(context, identity=identity, address=("10.0.2.15", 5020)) # Udp: #StartUdpServer(context, identity=identity, address=("localhost", 502)) # Ascii: #StartSerialServer(context, identity=identity, port='/dev/pts/3', timeout=1) # RTU: # StartSerialServer(context, framer=ModbusRtuFramer, identity=identity, port='/dev/ptyp0', timeout=.005, baudrate=9600)
def controller_server(): StartTcpServer(context, identity=identity, address=(server_ip['ip'], int(server_ip['port'])))
start += 1 print "byte", False, start, count hr = ModbusSequentialDataBlock(start, [0] * count) store = ModbusSlaveContext(di=di, co=co, hr=hr, ir=ir) context = ModbusServerContext(slaves=store, single=True) return context #---------------------------------------------------------------------------# # initialize the server information #---------------------------------------------------------------------------# # If you don't set this or any fields, they are defaulted to empty strings. #---------------------------------------------------------------------------# identity = ModbusDeviceIdentification() identity.VendorName = 'VOLTTRON' identity.ProductCode = 'VT' identity.VendorUrl = 'http://github.com/VOLTTRON/volttron' identity.ProductName = 'VOLTTRON Modbus Test Device' identity.ModelName = 'VOLTTRON Modbus Test Device' identity.MajorMinorRevision = '1.0' abstraction = DeviceAbstraction(args.config) context = abstraction.get_server_context() #---------------------------------------------------------------------------# # run the server you want #---------------------------------------------------------------------------# StartTcpServer(context, identity=identity, address=("localhost", args.port))
#!/usr/bin/env python from pymodbus.server.sync import StartTcpServer from pymodbus.server.sync import StartUdpServer from pymodbus.server.sync import StartSerialServer from pymodbus.device import ModbusDeviceIdentification from pymodbus.datastore import ModbusSequentialDataBlock from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext from pymodbus.transaction import ModbusRtuFramer import logging store = ModbusSlaveContext( di = ModbusSequentialDataBlock(0, [17]*100), co = ModbusSequentialDataBlock(0, [17]*100), hr = ModbusSequentialDataBlock(0, [17]*100), ir = ModbusSequentialDataBlock(0, [17]*100)) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.0' StartTcpServer(context, identity=identity, address=("172.20.0.2", 502))
self.thread.start() def cancel(self): self.thread.cancel() #Asignamos valores para conexión TCP/IP serverIp = "192.168.1.107" #La IP de esta PC serverPort = 5020 #El puerto para la escucha clientIp = "192.168.1.100" #La IP de la otra PC clientPort = 5021 #El puerto para la lectura/escritura #CONFIGURAMOS EL CLIENTE MODBUS TCP client = ModbusTcpClient(clientIp, clientPort) print(client.connect()) readReg = readHoldRegisters(client, 4014, 2) #CONFIGURAMOS EL SERVIDOR MODBUS TCP myBlock = dataServer( 4015, [1000, 8000]) #Creamos el bloque de datos del servidor Modbus TCP store = contextSlave( di=None, co=None, hr=myBlock, ir=None) #Asignamos los bloques de datos al servidor Modbus TCP context = contextServer(slaves=store, single=True) StartTcpServer(context, address=(serverIp, serverPort)) #Encendemos el servidor ModbusTCP while True: print("SERVER ACTIVE") sleep(5)
def run_server(): store = ModbusSlaveContext(ir=ModbusSequentialDataBlock(30001, [152, 276]), zero_mode=True) context = ModbusServerContext(slaves=store, single=True) StartTcpServer(context, address=("localhost", 5020))