Пример #1
0
 def __init__(self):
     """Constructor"""
     
     Simulator.__init__(self, modbus_tcp.TcpServer())
     
     # create a slave and block
     slave = self.server.add_slave(1)
     slave.add_block("foo", HOLDING_REGISTERS, 0, 100)
Пример #2
0
    def __init__(self, *args, **kwargs):
        """Constructor"""
        Simulator.__init__(self, *args, **kwargs)
        # add a new command: cv will make possible to change a value
        self.add_command("cv", self.change_value)

        # create a slave and block
        slave = self.server.add_slave(1)
        slave.add_block("foo", HOLDING_REGISTERS, 0, 100)
Пример #3
0
    def __init__(self):
        """Constructor"""
        Simulator.__init__(self)
        # add a new command: cv will make possible to change a value
        self.add_command("cv", self.change_value)

        # create a slave and block
        slave = self.server.add_slave(1)
        slave.add_block("foo", HOLDING_REGISTERS, 0, 100)
Пример #4
0
    def __init__(self, mode, port, baud=None, hostname=None, verbose=None):
        self.rtu = None
        self.mode = mode
        if self.mode == 'rtu' and baud and port:
            self.rtu = serial.Serial(port=port, baudrate=baud)
            Simulator.__init__(self, ModbusRtuServer(self.rtu))
            # timeout is too fast for 19200 so increase a little bit
            self.server._serial.timeout *= 2
            self.server._serial.interCharTimeout *= 2
            LOGGER.info('Initializing modbus %s simulator: baud = %d port = %s parity = %s' % (self.mode, baud, port, self.rtu.parity))
            LOGGER.info('stop bits = %d xonxoff = %d' % (self.rtu.stopbits, self.rtu.xonxoff))
        elif self.mode == 'tcp' and hostname and port:
            Simulator.__init__(self, TcpServer(address=hostname, port=port))
            LOGGER.info('Initializing modbus %s simulator: addr = %s port = %s' % (self.mode, hostname, port))
        else:
            raise ModbusSimError('Unknown mode: %s' % (mode))

        self.server.set_verbose(True)
def main():
    """main"""
    #create the object for getting CPU data
    data_collector = SystemDataCollector(5)
    #create the thread in charge of calling the data collector
    system_monitor = WorkerThread(data_collector.collect)

    #create the modbus TCP simulator and one slave
    #and one block of analog inputs
    simu = Simulator(TcpServer())
    slave = simu.server.add_slave(1)
    slave.add_block("Cpu", defines.ANALOG_INPUTS, 0, 10)

    try:
        LOGGER.info("'quit' for closing the server")

        #start the data collect
        system_monitor.start()

        #start the simulator! will block until quit command is received
        simu.start()

    except Exception as excpt:
        print excpt

    finally:
        #close the simulator
        simu.close()
        #stop the data collect
        system_monitor.stop()