def testModbusServerFactory(self): ''' Test the base class for all the clients ''' factory = ModbusServerFactory(store=None) self.assertEqual(factory.control.Identity.VendorName, '') identity = ModbusDeviceIdentification(info={0x00: 'VendorName'}) factory = ModbusServerFactory(store=None, identity=identity) self.assertEqual(factory.control.Identity.VendorName, 'VendorName')
def __init__(self, address, port = MODBUS_PORT): block = ModbusSequentialDataBlock(0x00, [0]*0x3ff) store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) self.context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'MockPLCs' identity.ProductCode = 'MP' identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' identity.ProductName = 'MockPLC 3000' identity.ModelName = 'MockPLC Ultimate' identity.MajorMinorRevision = '1.0' ModbusServerFactory.__init__(self, self.context, ModbusSocketFramer, identity)
def __init__(self, address, port = MODBUS_PORT): store = ModbusSlaveContext( di = ModbusSequentialDataBlock(0, [0]*100), co = ModbusSequentialDataBlock(0, [0]*100), hr = ModbusSequentialDataBlock(0, [0]*100), ir = ModbusSequentialDataBlock(0, [0]*100)) self.context = ModbusServerContext(slaves=store, single=True) identity = ModbusDeviceIdentification() identity.VendorName = 'MockPLCs' identity.ProductCode = 'MP' identity.VendorUrl = 'http://github.com/bashwork/pymodbus/' identity.ProductName = 'MockPLC 3000' identity.ModelName = 'MockPLC Ultimate' identity.MajorMinorRevision = '1.0' ModbusServerFactory.__init__(self, self.context, ModbusSocketFramer, identity)
def _simulator(self): """ Starts the snmp simulator """ ports = [502]+range(20000,25000) for port in ports: try: reactor.listenTCP(port, ModbusServerFactory(self._parse())) log.info('listening on port %d' % port) return port except twisted_error.CannotListenError: pass
def _simulator(self): ''' Starts the snmp simulator ''' ports = [502] + range(20000, 25000) for port in ports: try: reactor.listenTCP(port, ModbusServerFactory(self._parse())) print 'listening on port', port return port except twisted_error.CannotListenError: pass
def start_server(inverters): from twisted.internet import reactor store = { int(i.id): create_context(i) for i in inverters if i.modbus_enabled } if len(store) == 0: return context = ModbusServerContext(slaves=store, single=False) factory = ModbusServerFactory(context, ModbusSocketFramer, None) reactor.listenTCP(5020, factory, interface='0.0.0.0')
def rrun(factory): reactor.removeAll() port = random.randrange(500, 599) print "Listening @ %s" % port reactor.listenTCP(port, factory) reactor.callLater(10, rrun, factory) 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' framer = ModbusSocketFramer factory = ModbusServerFactory(context, framer, identity) print "Starting Reactor...." reactor.callLater(2, rrun, factory) reactor.run()
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' # ------------------------------------------------------------ # initialize the datastore # ------------------------------------------------------------ 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 factory # ------------------------------------------------------------ address = ("", Defaults.Port) factory = ModbusServerFactory(context, None, identity) # ------------------------------------------------------------ # start the servers # ------------------------------------------------------------ log.info("Starting Modbus TCP Server on %s:%s" % address) reactor.listenTCP(address[1], factory, interface=address[0]) RunDebugModbusFrontend(factory)
def run_async_server(self, address): # ----------------------------------------------------------------------- # # initialize your data store # ----------------------------------------------------------------------- # # The datastores only respond to the addresses that they are initialized to # Therefore, if you initialize a DataBlock to addresses from 0x00 to 0xFF, # a request to 0x100 will respond with an invalid address exception. # This is because many devices 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 devices 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 you may use a seperate 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) # ----------------------------------------------------------------------- # self.dataStore = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, [17]*100), co=ModbusSequentialDataBlock(0, [17]*100), hr=ModbusSequentialDataBlock(0, [17]*100), ir=ModbusSequentialDataBlock(0, [17]*100)) self.context = ModbusServerContext(slaves=self.dataStore, single=True) # ----------------------------------------------------------------------- # # initialize the server information # ----------------------------------------------------------------------- # # If you don't set this or any fields, they are defaulted to empty strings. # ----------------------------------------------------------------------- # 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' # ----------------------------------------------------------------------- # # run the server (listener) # ----------------------------------------------------------------------- # framer = ModbusSocketFramer factory = ModbusServerFactory(self.context, framer, self.identity) reactor.listenTCP(address[1], factory, interface=address[0])
# Servidor para clientes Modbus IP como Mango m2m modbuses = {} contexts = {} for ccc,port in SITIOS.items(): context = ModbusSlaveContext(d=AdminDataBlock('ed', ccc), c=AdminDataBlock('sd', ccc), i=AdminDataBlock('ea', ccc), h=AdminDataBlock('re', ccc), slave=port - 500, sitio=ccc ) contexts[port - 500] = context # Servidor Modbus IP administrativo | estado de sitios y robots mbfactory = ModbusServerFactory(ModbusServerContext(contexts, single=False)) modbuses['aaa'] = mbfactory reactor.listenTCP(500, mbfactory) # No esperar a que se conecten los robots. # Empezas a escuchar desde el principio for ccc, port in SITIOS.items(): if ccc not in modbuses.keys(): mbfactory = ModbusServerFactory2(None) # None store #mbfactory.protocol.ccc = ccc mbfactory.ccc = ccc modbuses[ccc] = mbfactory if port != 500: reactor.listenTCP(port, mbfactory) print modbuses