def run_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) # 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 = '2.3.0' # run the server you want after creating tasks sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(("127.0.0.1", 9977)) loop = asyncio.get_event_loop() loop.create_task(start_servers(context, identity, loop)) loop.create_task(updating_writer(context, 0.3, sock)) loop.run_forever() sock.close()
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 = 'DYODEv2' identity.VendorUrl = 'http://github.com/wavestone-cdt/dyode' identity.ProductName = 'DYODE' identity.ModelName = 'Very Low Cost @ BlackHat Arsenal' identity.MajorMinorRevision = '1.0' #--------------------------------------------------------------------------# # run the server you want #--------------------------------------------------------------------------# time = 1 # 1 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 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 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_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 __init__(self,port=1234,sub_topic="modbus_server/write_to_registers",pub_topic="modbus_server/read_from_registers"): """ Creates a Modbus TCP Server object .. note:: The default port for modbus is 502. This modbus server uses port 1234 by default, otherwise superuser rights are required. .. note:: Use "startServer" to start the listener. :param port: Port for the modbus TCP server :type port: int :param sub_topic: ROS topic name for the subscriber that updates the modbus registers :type sub_topic: string :param pub_topic: ROS topic name for the publisher that publishes a message, once there is something written to the writeable modbus registers :type pub_topic: string """ chr = CustomHoldingRegister(ADDRESS_WRITE_START, [17]*100,sub_topic,pub_topic) self.store = ModbusSlaveContext( di = ModbusSequentialDataBlock(ADDRESS_WRITE_START, [17]*100), co = ModbusSequentialDataBlock(ADDRESS_WRITE_START, [17]*100), hr = chr, ir = ModbusSequentialDataBlock(ADDRESS_WRITE_START, [17]*100)) self.context = ModbusServerContext(slaves=self.store, single=True) self.identity = ModbusDeviceIdentification() self.identity.VendorName = 'Pymodbus' self.identity.ProductCode = 'PM' self.identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' self.identity.ProductName = 'Pymodbus Server' self.identity.ModelName = 'Pymodbus Server' self.identity.MajorMinorRevision = '1.0' self.store.setValues(2,0,[0]*1) self.post = Post(self) framer = ModbusSocketFramer self.server = ModbusTcpServer(self.context, framer, self.identity, address=("0.0.0.0", port))
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)) store.register(CustomModbusRequest.function_code, 'cm', 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.1.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # # Tcp: StartTcpServer(context, identity=identity, address=("localhost", 5020), custom_functions=[CustomModbusRequest])
async def ieb_server(event_loop): store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [0] * 100), co=ModbusSequentialDataBlock(512, [0] * 100), hr=ModbusSequentialDataBlock(512, [0] * 100), ir=ModbusSequentialDataBlock(0, [0] * 100), ) context = ModbusServerContext(slaves=store, single=True) server = await StartTcpServer( context, address=("127.0.0.1", 5020), loop=event_loop, allow_reuse_address=True, allow_reuse_port=True, ) task = event_loop.create_task(server.serve_forever()) yield server server.server_close() task.cancel() with contextlib.suppress(asyncio.CancelledError): await task
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.2.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 #StartSerialServer(context, identity=identity, port="/dev/tty.usbserial-AK066TL5", framer=ModbusRtuFramer) StartSerialServer(context, identity=identity, port="/dev/tty.usbserial-AK066OZW", framer=ModbusRtuFramer)
def run_updating_server(): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # 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) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # identity = ModbusDeviceIdentification() identity.VendorName = 'dreams by Eqinov' identity.ProductName = 'demo pilotage wattsense' identity.MajorMinorRevision = '1.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # timeRead = 1 # 1 second delay readLoop = LoopingCall(f=read_context, a=(context,)) readLoop.start(timeRead, now=False) # initially delay by time timeWrite = 30 # 1 second delay writeLoop = LoopingCall(f=write_context, a=(context,)) writeLoop.start(timeWrite, now=False) # initially delay by time StartTcpServer(context, identity=identity, address=("0.0.0.0", 5020))
def run_server(): store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [101] * 100), co=ModbusSequentialDataBlock(0, [111] * 100), hr=ModbusSequentialDataBlock(0, [1101] * 100), ir=ModbusSequentialDataBlock(0, [111] * 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=("0.0.0.0", 9001))
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 __init__(self, threadID, name, config, modbusRs2modbusTcp, modbusTcp2modbusRs): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.config = config self.log = logging.getLogger(name) self.modbusRs2modbusTcp = modbusRs2modbusTcp # kolejka do odbierania danych z modbusRs self.modbusTcp2modbusRs = modbusTcp2modbusRs # kolejka do wysylania danych do modbusRS self.tick = 0.01 self.interval = 0.1 self.exitFlag = False self.store = ModbusSlaveContext( # Tworzymy rejestr. Inicjalizujemy wszystko zerami. di=ModbusSequentialDataBlock(0, [00] * 1), co=ModbusSequentialDataBlock(0, [00] * 1), hr=ModbusSequentialDataBlock(0, [00] * 100000), ir=ModbusSequentialDataBlock(0, [00] * 1)) # Multipleksacja na podstawie adresów rejestrów HR a nie na poziomie Unit Id. self.context = ModbusServerContext(slaves=self.store, single=True) self.identity = ModbusDeviceIdentification() self.identity.VendorName = 'pymodbus' self.identity.ProductCode = 'PM' self.identity.VendorUrl = 'itcloud' self.identity.ProductName = 'pymodbus Server' self.identity.ModelName = 'pymodbus Server' self.identity.MajorMinorRevision = '1.0' self.framer = ModbusSocketFramer self.server = ModbusTcpServer(self.context, self.framer, self.identity, (self.config["modbusTcp"]["bindIp"], self.config["modbusTcp"]["bindPort"]))
def __init__(self, slave_id, **kwargs): """Modbusスレーブの初期化 Args: slave_id: RPi-GP10のスレーブデバイスID slave: i2cスレーブアドレス strobe: ストローブピン番号 trigger: トリガーピン番号 """ d = {} d['i2caddr'] = kwargs.get('slave', 0x20) d['strobe'] = kwargs.get('strobe', 14) d['trigger'] = kwargs.get('trigger', 15) self._gp10 = Gp10(**d) self._output = 0xFF self._slave_id = slave_id self._polarity = {"output": 0x00, "input": 0x00} self._strobe = {"enable": False, "time": 1, "remain": -1} self._trigger = {"enable": False, "values": [False] * 8} Gp10ModbusSlave.gp10_trigger["gp10"] = self._gp10 # Modbus データストア self._store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [False]*17), co=ModbusSequentialDataBlock(0, [False]*27), hr=ModbusSequentialDataBlock(0, [0, self._strobe["time"]]), ir=ModbusSequentialDataBlock(9999, [0]*2))
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 run_server(ip_address, num_fields, port=502): """ Initiates a modbus server (device) :param ip_address: ip_adress under which the Modbus device should be accessible :param port: port to which the server listens :param num_fields: length of the registers :return: None """ # configure the device and initialize the registers store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [0] * num_fields), co=ModbusSequentialDataBlock(0, [0] * num_fields), hr=ModbusSequentialDataBlock(0, [0] * num_fields), ir=ModbusSequentialDataBlock(0, [0] * num_fields)) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'Modbus Replica' identity.ProductCode = 'MS' identity.ProductName = 'Streampipes Modbus Simulator' identity.ModelName = 'Modbus Slave' identity.MajorMinorRevision = '2.3.0' StartTcpServer(context, identity=identity, address=(ip_address, port))
def main(): 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) identity = ModbusDeviceIdentification() identity.VendorName = 'pymodbus' identity.ProductCode = 'PM' identity.VendorUrl = 'http://github.com/simplyautomationized' identity.ProductName = 'pymodbus Server' identity.ModelName = 'pymodbus Server' identity.MajorMinorRevision = '1.0' time = 5 # 5 seconds delaytime = 5 # 5 seconds delay writer = LoopingCall(read_context, a=(context, )) loop = LoopingCall(updating_writer, a=(context, )) loop.start(.5) # initially delay by time writer.start(.1) StartTcpServer(context, identity=identity) #, address=("localhost", 502)) #cleanup async tasks temp.setEnabled(False) loop.stop() writer.stop() GPIO.cleanup()
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_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/riptideio/pymodbus/' identity.ProductName = 'pymodbus Server' identity.ModelName = 'pymodbus Server' identity.MajorMinorRevision = '2.2.0' # ----------------------------------------------------------------------- # # run the server you want # ----------------------------------------------------------------------- # time = 0.5 # 5 seconds delay loop = LoopingCall(f=updating_writer, a=(context, )) loop.start(time, now=False) # initially delay by time StartSerialServer(context, framer=ModbusRtuFramer, identity=identity, port='/dev/ttyS0', timeout=1, baudrate=460800)
def run_server(device, baud=9600): store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [17] * 100), co=ModbusSequentialDataBlock(0, [17] * 100), hr=ModbusSequentialDataBlock(0, [25185] * 1024), ir=ModbusSequentialDataBlock(0, [25185] * 1024)) slaves = { 1: copy.deepcopy(store), 2: copy.deepcopy(store), 3: copy.deepcopy(store), 4: copy.deepcopy(store), 5: copy.deepcopy(store), 6: copy.deepcopy(store), 246: copy.deepcopy(store) } context = ModbusServerContext( slaves=slaves, single=False, ) # RTU Server StartSerialServer(context, identity=None, port=device, framer=ModbusRtuFramer, stopbits=1, bytesize=8, parity=serial.PARITY_NONE, baudrate=baud)
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 run(self): store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [i for i in range(100)],), co=ModbusSequentialDataBlock(0, [i for i in range(100)]), hr=ModbusSequentialDataBlock(0, [i for i in range(100)]), ir=ModbusSequentialDataBlock(0, [i for i in range(100)]), zero_mode=True ) for key, value in store.store.items(): log.debug('number of coils/registers in {}: {}'.format(key, len(value.values))) 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' self.server = ModbusTcpServer(context, identity=identity, address=("localhost", 5020)) log.info("starting modbus tcp server [localhost:5020]: {}".format(self.server)) self.server.serve_forever()
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() # socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,link=/tmp/ttyp0,raw,echo=0,ospeed=9600 StartSerialServer( context, framer=ModbusRtuFramer, identity=identity, port="/tmp/ttyp0", timeout=0.005, baudrate=9600, )
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=("192.168.95.10", 502))
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-PORTAIL' identity.VendorUrl = 'https://github.com/haxom/' identity.ProductName = 'SIMU-ICS' identity.ModelName = 'PORTAIL' 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(): 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() StartTlsServer( context, identity=identity, certfile="server.crt", keyfile="server.key", address=("127.0.0.1", 8020), )
def run_server(): block1 = ModbusSequentialDataBlock(0x00, [0] * 0xFF) block2 = ModbusSequentialDataBlock(0x10, [0] * 0xFF) block3 = ModbusSequentialDataBlock(0x00, [0] * 0xFF) block4 = ModbusSequentialDataBlock(0x00, [0] * 0xFF) store1 = ModbusSlaveContext(co=block3, di=block4, hr=block1, ir=block2) store2 = ModbusSlaveContext(co=block3, di=block4, hr=block1, ir=block2) slaves = {0xFF: store1} context = ModbusServerContext(slaves=slaves, single=False) 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' interval = 1 server = ModbusTcpServer(context, identity=identity, address=('0.0.0.0', 502)) t = threading.Thread(target=server.serve_forever, daemon=True) t.start() loop = LoopingCall(f=updatevalues, a=server) loop.start(interval, now=True) reactor.run()
def __init__(self, ip, port, unit, data, parent): super(MBTCPServer, self).__init__(parent) self.slave = MBSlave(ModbusSequentialDataBlock(1, data[0]), ModbusSequentialDataBlock(1, data[1]), ModbusSequentialDataBlock(1, data[2]), ModbusSequentialDataBlock(1, data[3])) self.ctx = ModbusServerContext(slaves={unit: self.slave}, single=False) self.server = ModbusTcpServer(self.ctx, framer=MSF, address=(ip, port))
def basic_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) StartTcpServer(context, address=(HOST, PORT))
def main(): args = parse_settings() if args.verbose: print args # arduino_dev_port = "/dev/ttyUSB0" # COM port on win # listen_conf = ("192.168.0.104", 10503) listen_conf = (args.listen_address, args.listen_port) store = ModbusSlaveContext(di=ModbusSequentialDataBlock(0, [False] * 20), co=ModbusSequentialDataBlock(0, [False] * 20), hr=ModbusSequentialDataBlock(0, [3] * 20), ir=ModbusSequentialDataBlock(0, [4] * 20)) context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'SCADALiaris' # identity.ProductCode = 'RealPLC 1' identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' identity.ProductName = 'pymodbus Server' identity.ModelName = 'pymodbus Server' identity.MajorMinorRevision = 'v0.5' time = args.poll_interval if not args.random: if args.rpi: rpi = Rpi1Wire() identity.ProductCode = 'RPiPLC 1' loop = LoopingCall(f=rpi_plc, a=(context, rpi, args)) loop.start(time, now=False) StartTcpServer(context, identity=identity, address=listen_conf) else: try: serial_port = serial.Serial(port=args.serial_port, baudrate=115200, bytesize=8, parity=serial.PARITY_NONE, stopbits=1) except serial.SerialException as ex: print "ERROR", ex sys.exit(-1) identity.ProductCode = 'ArduinoPLC 1' loop = LoopingCall(f=arduino_plc, a=(context, serial_port, args)) loop.start(time, now=False) StartTcpServer(context, identity=identity, address=listen_conf) else: identity.ProductCode = 'RandomPLC 1' loop = LoopingCall(f=random_plc, a=(context, args)) loop.start(time, now=False) StartTcpServer(context, identity=identity, address=listen_conf)
def __init__(self, output): """ Initialize a new instance of the logger :param output: The output file to save to """ self.output = output self.context = ModbusSlaveContext( di = ModbusSequentialDataBlock.create(), co = ModbusSequentialDataBlock.create(), hr = ModbusSequentialDataBlock.create(), ir = ModbusSequentialDataBlock.create())
def setValues(self, address, value): """ Sets the requested values to the holding registers :param address: The starting address :type address: int :param values: The new values to be set :type values: list[int] """ #print "address",address,"value",value ModbusSequentialDataBlock.setValues(self,address, value)
def setValues(self, address, value): """ Sets the requested values to the holding registers and publishes they new values on a rostopic :param address: The starting address :type address: int :param values: The new values to be set :type values: list[int] """ # print "address",address,"value",value ModbusSequentialDataBlock.setValues(self,address, value) if address >= self.reading_start: msg = HoldingRegister() msg.data = value self.pub.publish(msg)