def run_updating_server(config_list, backup_filename, log): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # # Run datastore_backup_on_start to use the most recent values of the datablocks, as the layout in the master config will only reflect initial values # If this is the first time this is used, the backup file will match up with what is laid out in the master config (due to master.py) datastore_config = datastore_backup_on_start(backup_filename) if datastore_config == -1: print(backup_filename) print( "Issue with backup file - either not created or empty. Exiting program." ) sys.exit() store = ModbusSlaveContext( di=ModbusSequentialDataBlock(datastore_config['di']['start_addr'], datastore_config['di']['values']), co=ModbusSequentialDataBlock(datastore_config['co']['start_addr'], datastore_config['co']['values']), hr=ModbusSequentialDataBlock(datastore_config['hr']['start_addr'], datastore_config['hr']['values']), ir=ModbusSequentialDataBlock(datastore_config['ir']['start_addr'], datastore_config['ir']['values'])) # Could have multiple slaves, with their own addressing. Since we have 1 PLC device handled by every async_plc.py, it is not necessary context = ModbusServerContext(slaves=store, single=True) # setup a thread with target as datastore_backup_to_yaml to start here, before other threads # this will continuously read from the context to write to a backup yaml file backup_thread = Thread(target=datastore_backup_to_yaml, args=(context, backup_filename)) backup_thread.daemon = True backup_thread.start() # start register behaviors. Updating writer is started off, which will spawn a thread for every holding register based on the config thread = Thread(target=updating_writer, args=(context, config_list, time, log, backup_filename)) thread.daemon = True thread.start() # Starting the server server_config = config_list['SERVER'] framer = configure_server_framer(server_config) if server_config['type'] == 'serial': StartSerialServer(context, port=server_config['port'], framer=framer) elif server_config['type'] == 'udp': StartUdpServer(context, identity=identity, address=(server_config['address'], int(server_config['port']))) elif server_config['type'] == 'tcp': if server_config['framer'] == 'RTU': StartTcpServer(context, identity=identity, address=(server_config['address'], int(server_config['port'])), framer=framer) else: StartTcpServer(context, address=(server_config['address'], int(server_config['port'])))
def main(): log = LoggerUtility.get_logger("dht11.log") log.info("**************************** START ****************************") # Initial the dht device, with data pin connected to: log.info("Initializing the device DHT11 on pin 18...") dhtDevice = adafruit_dht.DHT11(board.D18) # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # log.info("Setting up data block and context...") block = ModbusDHTDataBlock(dhtDevice, log) # block = ModbusSequentialDataBlock(0, [3]*5) # print(block.getValues(0,2)) store = {0x01: ModbusSlaveContext(di=block, co=block, hr=block, ir=block)} context = ModbusServerContext(slaves=store, single=False) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # log.info("Getting identity...") identity = ModbusUtility.get_modbus_device_identification("DHT11 Server") # ----------------------------------------------------------------------- # # run the TCP Server # ----------------------------------------------------------------------- # log.info("Starting TCP Server...") StartTcpServer(context, identity=identity, address=("0.0.0.0", 5000))
def prepare_and_run_server(aas_): # prepare data context store = {} for key, value in aas.config["slave_id"].items(): store[value] = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [0xffff] * 255), co=ModbusSequentialDataBlock(0, [0xffff] * 255), hr=ModbusSequentialDataBlock(0, [0xffff] * 255), ir=ModbusSequentialDataBlock(0, [0xffff] * 255)) aas_.context = ModbusServerContext(slaves=store, single=False) # prepare server identity identity = ModbusDeviceIdentification() identity.VendorName = 'FEKT VUTBR' identity.ProductCode = 'AAS' identity.VendorUrl = 'https://www.fekt.vut.cz/' identity.ProductName = 'AAS modbus server' identity.ModelName = 'AAS module' identity.MajorMinorRevision = "{}".format(__version__) thread = Thread(target=get_written_values, args=(aas_,)) thread.start() # get IP address for communication over network cmd = "hostname -I | cut -d\' \' -f1" ip = subprocess.check_output(cmd, shell=True) ip = str(ip, "ascii") StartTcpServer(aas_.context, identity=identity, address=(ip, aas_.config["port"]))
def run_custom_db_server(address, port): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # coil_block = ModbusSequentialDataBlock(1, [0] * 256) discrete_input_block = ModbusSequentialDataBlock(10001, [0] * 256) input_register_block = ModbusSequentialDataBlock(30001, register_data) holding_register_block = ModbusSequentialDataBlock(40001, register_data) store = ModbusSlaveContext(di=discrete_input_block, co=coil_block, hr=holding_register_block, ir=input_register_block, zero_mode=True) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # 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.3.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # # p = Process(target=device_writer, args=(queue,)) # p.start() StartTcpServer(context, identity=identity, address=(address, port))
def modbus_master(module, properties): log.debug('Modbus master module : ' + str(module)) # Modbus Master #--------------------------------------------------------------------------# # initialize your data store #--------------------------------------------------------------------------# store = ModbusSlaveContext(co=ModbusSequentialDataBlock(0, [0] * 100), hr=ModbusSequentialDataBlock(0, [0] * 100)) context = ModbusServerContext(slaves=store, single=True) #--------------------------------------------------------------------------# # initialize the server information #--------------------------------------------------------------------------# identity = ModbusDeviceIdentification() identity.VendorName = 'ASO+AKO' identity.ProductCode = 'DYODE' identity.VendorUrl = 'yoloswag' identity.ProductName = 'DYODE' identity.ModelName = 'BSides LV release' identity.MajorMinorRevision = '0.9' #--------------------------------------------------------------------------# # run the server you want #--------------------------------------------------------------------------# time = 1 # 5 seconds delay loop = LoopingCall(f=modbus_master_update, a=(module, properties, context)) loop.start(time, now=False) # initially delay by time StartTcpServer(context, identity=identity, address=("0.0.0.0", \ properties['port_out']))
def main(): """ Server launcher """ parser = OptionParser() parser.add_option("-c", "--conf", help="The configuration file to load", dest="file") parser.add_option("-D", "--debug", help="Turn on to enable tracing", action="store_true", dest="debug", default=False) (opt, arg) = parser.parse_args() # enable debugging information if opt.debug: try: server_log.setLevel(logging.DEBUG) protocol_log.setLevel(logging.DEBUG) except Exception as e: print("Logging is not supported on this system") # parse configuration file and run try: conf = Configuration(opt.file) StartTcpServer(context=conf.parse()) except ConfigurationException as err: print(err) parser.print_help()
def run_callback_server(ip, porta, queue, queue2): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # diDataBlock = CallbackDataBlock(di_map(), queue, queue2) coDataBlock = CallbackDataBlock(co_map(), queue, queue2) hrDataBlock = CallbackDataBlock(hr_map(), queue, queue2) irDataBlock = CallbackDataBlock(ir_map(), queue, queue2) store = ModbusSlaveContext(di=diDataBlock, co=coDataBlock, hr=hrDataBlock, ir=irDataBlock) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # identity = ModbusDeviceIdentification() identity.VendorName = 'LHF Instrumentacao' identity.ProductCode = 'mOHM 1.0' identity.VendorUrl = 'lhf.ind.br' identity.ProductName = 'Miliohmimetro' identity.MajorMinorRevision = '1.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # StartTcpServer(context, identity=identity, address=(ip, porta))
def run_custom_db_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # block = CustomDataBlock([0]*100) store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # 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.3.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # # p = Process(target=device_writer, args=(queue,)) # p.start() StartTcpServer(context, identity=identity, address=("localhost", 5020))
def run_PiCam_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [1] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [0] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100)) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # identity = ModbusDeviceIdentification() identity.VendorName = 'Robot system design' identity.ProductCode = 'PI' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # time = utilities_modbus.REGISTER_REFRESH_FREQUENCY loop = LoopingCall(f=updating_server, a=(context, )) loop.start(time, now=False) # initially delay by time StartTcpServer(context, identity=identity, address=(utilities_modbus.IP, utilities_modbus.PORT))
def run_callback_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # queue = Queue() devices = read_device_map("device-mapping") block = CallbackDataBlock(devices, queue) store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # 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() # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # p = Process(target=device_writer, args=(queue,)) p.start() StartTcpServer(context, identity=identity, address=("localhost", 5020))
def run_dbstore_update_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # block = ModbusSequentialDataBlock(0x00, [0] * 0xff) store = SqlSlaveContext(block) context = ModbusServerContext(slaves={1: store}, single=False) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # 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() # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # time = 5 # 5 seconds delay loop = LoopingCall(f=updating_writer, a=(context,)) loop.start(time, now=False) # initially delay by time loop.stop() StartTcpServer(context, identity=identity, address=("", 5020))
def run_updating_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # InitialValueList = [0] * 29 + CheckInitialValues() store = ModbusSlaveContext( hr=ModbusSequentialDataBlock(0, InitialValueList)) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # 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.3.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # time = scan_interval # 5 seconds delay loop = LoopingCall(f=updating_writer, a=(context, )) loop.start(time, now=False) # initially delay by time StartTcpServer(context, identity=identity, address=(ip_address, port))
def run_async_server(): store_feed1 = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [17] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100)) context_feed1 = ModbusServerContext(slaves=store_feed1, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # # If you don't set this or any fields, they are defaulted to empty strings. # ----------------------------------------------------------------------- # identity_feed1 = ModbusDeviceIdentification() identity_feed1.VendorName = 'Pymodbus' identity_feed1.ProductCode = 'PM' identity_feed1.VendorUrl = 'http://github.com/bashwork/pymodbus/' identity_feed1.ProductName = 'Pymodbus Server' identity_feed1.ModelName = 'Pymodbus Server' identity_feed1.MajorMinorRevision = '1.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # StartTcpServer(context_feed1, identity=identity_feed1, address=(Remote_IO_ip_address, modbusTCP_port))
def run_updating_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # 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 # ----------------------------------------------------------------------- # 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.3.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # time = 5 # 5 seconds delay loop = LoopingCall(f=updating_writer, a=(context,)) loop.start(time, now=False) # initially delay by time StartTcpServer(context, identity=identity, address=("localhost", 5020))
def main(): from pymodbus.server.asynchronous import StartTcpServer from pymodbus.datastore import (ModbusServerContext, ModbusSlaveContext, ModbusSparseDataBlock) logging.basicConfig(level=logging.DEBUG) class CallbackDataBlock(ModbusSparseDataBlock): def setValues(self, address, values): super(CallbackDataBlock, self).setValues(address, values) # Set cover status based on the incoming command value. if values == [1]: s = Status(mirror_cover1_open=1, mirror_cover2_open=1, baffle_open=1) elif values == [0]: s = Status(mirror_cover1_closed=1, mirror_cover2_closed=1, baffle_closed=1) super(CallbackDataBlock, self).setValues(131, [s.value]) store = ModbusSlaveContext( hr=CallbackDataBlock.create(), # holding registers ) context = ModbusServerContext(slaves=store, single=True) StartTcpServer(context)
def run_async_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [17] * 100), ir=ModbusSequentialDataBlock(0, [17] * 100)) store.register(CustomModbusRequest.function_code, 'cm', ModbusSequentialDataBlock(0, [17] * 100)) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information 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' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # StartTcpServer(context, identity=identity, address=("192.168.0.119", 5020), custom_functions=[CustomModbusRequest])
def run_simulation_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # store = ModbusSlaveContext( co=ModbusSparseDataBlock({100: 0, 110: 0, 120: 0, 130: 0,}),#initiate Coils di=ModbusSparseDataBlock({100: 0, 110: 0}), #initiate discrete inputs ir=ModbusSparseDataBlock({100: 0, 110: 0, 120:0, 130:0, 140:0}), #initiate input registers hr=ModbusSparseDataBlock({100: 60000, 110: 10000, 120:655}), zero_mode=True) #initiate holding registers context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # identity = ModbusDeviceIdentification() identity.VendorName = 'akiUP' identity.ProductCode = 'IOP' identity.VendorUrl = 'https://github.com/akiUp/ICSonPySim' identity.ProductName = 'ICSonPy' identity.ModelName = 'ICS Simulation' identity.MajorMinorRevision = '0.0.1' # ----------------------------------------------------------------------- # # run the Modbus Server with looping call of simulation # ----------------------------------------------------------------------- # time = 0.1 # process frequency delay in seconds, increase if you want to slow down the process loop = LoopingCall(f=Process, mbcontext=(context,)) # Main caleer function continiously calls the Process() function loop.start(time, now=False) # initially delay by time StartTcpServer(context, identity=identity, address=("0.0.0.0", 502))
def main(args): # for test get the server parameters and test data from the test database db = dbfn.db("../test.sqlite3") q_test_params = "SELECT {cols} from test_params where id = ?" cols = ["id","modbus_servers", "field_name"] test_params = db.getColDict( q_test_params, cols, (args.test, ) ) #print(test_params) q_data = "select key, value from test_data where test = ? order by seq" test_val = db.getRowDict(q_data, (args.test, ) ) #print(test_val) #q_limit = "select key, match_type from test_data where test = ?" #test_limit = db.getRowDict(q_limit, (args.test, ) ) #print(test_limit) db.close() # based on test server parameter load the serer details from the main database #load server details db = dbfn.db("../db.sqlite3") #print(params) #db.close() store = dict() for s in test_params["modbus_servers"].split(","): server = int(s) params = db.getConnectionParams(server) section = db.getDataFormat(server,0) block = make_block(section, test_val, test_params) store1 = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [17]*100), co=ModbusSequentialDataBlock(0, [117]*100), hr=block, ir=ModbusSequentialDataBlock(0, [217]*100)) store[params["unit"]] = store1 db.close() context = ModbusServerContext(slaves=store, single=False) #for k in store: # print(k) # print(context[k]) #print("CTX", context) for s in context: print("********************Slave", s) 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.2.0' print("Starting Server at:", params["port"]) StartTcpServer(context, identity=identity, address=("localhost", params["port"]))
def testTcpServerStartup(self): ''' Test that the modbus tcp asynchronous server starts correctly ''' with patch('twisted.internet.reactor') as mock_reactor: if IS_PYTHON3: console = False call_count = 1 else: console = True call_count = 2 StartTcpServer(context=None, console=console) self.assertEqual(mock_reactor.listenTCP.call_count, call_count) self.assertEqual(mock_reactor.run.call_count, 1)
def run_async_server(self): 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.3.0' self.communicating = True ## Start TCP Server StartTcpServer(self.context, identity=identity, address=("localhost", 5020)) self.communicating = False
def run_updating_server(): store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [0] * 100), co=ModbusSequentialDataBlock(0, [0] * 100), hr=ModbusSequentialDataBlock(0, [0] * 100), ir=ModbusSequentialDataBlock(0, [0] * 100)) context = ModbusServerContext(slaves=store, single=True) loop = LoopingCall(f=updating_writer, a=(context, )) loop.start(0.00001, now=True) try: StartTcpServer(context, address=("10.10.10.2", 502)) except ValueError: log("ERROR Staring TCP Server")
def run_custom_db_server(address, port): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # block = CustomDataBlock([ 0x556e, 0x6974, 0x3233, 0x2d41, # 64bit chr Unit23-A 0xffff, # 16bit UINT 65535 0xfc19, # 16ibt INT -32768 0xffff, 0xfffa, # 32bit UINT 4294967290 0x8000, 0x0000, # 32bit INT -2147483648 0x437e, 0xe2c6, # 32bit Float 254.88583 0x420a, 0xc326, # 32bit Float 34.69057 0x427d, 0x7aeb, # 32bit Float 63.37004 0x4107, 0x0e38, # 32bit Float 8.440971 0xffff, # 8bit UINT 0, 8bit int 0x0007, # 8bits 0000 0000 0x07, # 8bits 0000 0111 ]) store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block, zero_mode=True) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # 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.3.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # # p = Process(target=device_writer, args=(queue,)) # p.start() StartTcpServer(context, identity=identity, address=(address, port))
def run_server(esmart_serial_port_path: str, device_addr: int, modbus_host: str, modbus_port: int) -> None: mon = ESmartMonitor(esmart_serial_port_path, device_addr) th = threading.Thread(target=mon.run) th.daemon = True th.start() ir_block = RegistersBlock(mon, ModbusRegisterType.InputRegister) co_block = RegistersBlock(mon, ModbusRegisterType.Coil) hr_block = RegistersBlock(mon, ModbusRegisterType.HoldingRegister) empty = BaseModbusDataBlock() store = ModbusSlaveContext(di=empty, co=co_block, hr=hr_block, ir=ir_block, zero_mode=True) context = ModbusServerContext(slaves=store, single=True) StartTcpServer(context, address=(modbus_host, modbus_port))
def run_modbus_server(): # initialize modbus server store with 0 on the first 100 addresses store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [0] * 100), co=ModbusSequentialDataBlock(0, [0] * 100), hr=ModbusSequentialDataBlock(0, [0] * 100), ir=ModbusSequentialDataBlock(0, [0] * 100)) context = ModbusServerContext(slaves=store, single=True) # simulator loop configuration interval = 5 loop = LoopingCall(f=simulator_function, a=(context, )) loop.start(interval, now=False) # start server on localhost StartTcpServer(context, address=("localhost", 5020))
def run_server(): 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: # immediately start serving: global context StartTcpServer(context, identity=identity, address=("0.0.0.0", 2502))
def run(self): time.sleep(2) logger.info('Start Modbus') """**************declare le nb de mots ***********************""" store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [0] * 100), co=ModbusSequentialDataBlock(0, [0] * 100), hr=ModbusSequentialDataBlock(0, [0] * 100), ir=ModbusSequentialDataBlock(0, [0] * 100)) context = ModbusServerContext(slaves=store, single=True) """**************************ecrit les entrees dans le module modbus************************""" def signed(value): packval = struct.pack('<h', value) return struct.unpack('<H', packval)[0] def updating_writer(a): #print (int (onewire.Temp[0]*10)) context = a[0] register = 3 slave_id = 0 address = 10 # mot w10 values = [(int(onewire.Temp[0] * 10)), (int(onewire.Temp[1] * 10)), (int(onewire.Temp[2] * 10)), (int(onewire.Temp[3] * 10)), (int(api.output[0])), (int(api.output[1])), (int(api.output[2])), (int(api.output[3]))] context[slave_id].setValues(register, address, values) #print (values) """*********lit les valeurs du module modbus ************************""" def read_context(a): context = a[0] register = 3 slave_id = 0 address = 30 # mot w30 value = context[slave_id].getValues(register, address, 10) read = LoopingCall(f=read_context, a=(context, )) read.start(.2) write = LoopingCall(f=updating_writer, a=(context, )) write.start(.2) StartTcpServer(context)
def run_updating_server(): # initialize data store 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 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' loop = LoopingCall(f=update_context, a=(context, )) loop.start(0.5, now=False) StartTcpServer(context, identity=identity, address=("0.0.0.0", 502))
def run_updating_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # store1 = ModbusSlaveContext(di=ModbusSparseDataBlock({0x10: 12}), cp=ModbusSparseDataBlock({0x10: 13}), hr=ModbusSparseDataBlock({0x10: 14}), ir=ModbusSparseDataBlock({0x100: 7000})) store2 = ModbusSlaveContext(di=ModbusSparseDataBlock({0x10: 12}), cp=ModbusSparseDataBlock({0x10: 13}), hr=ModbusSparseDataBlock({0x10: 14}), ir=ModbusSparseDataBlock({0x100: 7000})) slaves = { 28: ModbusSlaveContext(slaves=store1), 48: ModbusSlaveContext(slaves=store2) } context = ModbusServerContext(slaves=slaves, single=False) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # 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' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # time = 0.1 loop = LoopingCall(f=updating_writer, a=(context, )) loop.start(time, now=False) # initially delay by time StartTcpServer(context, identity=identity, address=("0.0.0.0", 5020))
def run_update_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, range(1, 101)), co=ModbusSequentialDataBlock(0, range(101, 201)), hr=ModbusSequentialDataBlock(0, range(201, 301)), ir=ModbusSequentialDataBlock(0, range(301, 401))) context = ModbusServerContext(slaves=store, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # 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' # connect to simulation HOST = '127.0.0.1' PORT = 55555 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((HOST, PORT)) # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # time = 1 # 5 seconds delay loop = LoopingCall(f=updating_writer, a=(context, sock)) loop.start(time, now=False) # initially delay by time StartTcpServer(context, identity=identity, address=(purge_ip_address, modbusTCP_port))
def run_modbus_server(): # initialize your data store queue = Queue() devices = read_device_map() block = CallbackDataBlock(devices, queue) store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) context = ModbusServerContext(slaves=store, single=True) # initialize the server information identity = ModbusDeviceIdentification() identity.VendorName = 'BUCHI Labortechnik AG' identity.ProductCode = 'R-300' identity.VendorUrl = 'https://www.buchi.com/' identity.ProductName = 'Rotavapor R-300' identity.ModelName = 'Rotavapor Modbus Server' identity.MajorMinorRevision = '1.0.0.0' # run updating thread t1 = Thread(target=updating_writer, args=(context, )) t1.start() # run writing thread t2 = Thread(target=device_writer, args=(queue, )) t2.start() # run modbus server if modbus_type == 'TCP': StartTcpServer(context, identity=identity, address=(modbus_ip, modbus_tcpport)) elif modbus_type == 'RTU': # this part doesn't work because bug in pymodbus, more info: https://github.com/riptideio/pymodbus/issues/514 StartSerialServer(context, framer=ModbusRtuFramer, identity=identity, port=modbus_port, baudrate=modbus_baudrate, parity=modbus_parity)