Exemplo n.º 1
0
class Slaves(CompositeNode):
    def __init__(self):
        self.subnet = VirtualSubNet(self)
        return
    def configure(self,config):
        CompositeNode.configure(self,config)
        set_attribute(self, 'debug', debug, config, int)
#
#
        if debug: self.debug = debug
#
#
        self.port = self #intercept any calls to the port
        return

    def configuration(self):
        config = CompositeNode.configuration(self)
        get_attribute(self, 'debug', config, str)
        return config
    def start(self):
        self.device_map = {}
        if self.debug: print 'Start modbus serial slave node'
        CompositeNode.start(self)
        self.subnet.start()
        if self.debug: print 'modbus serial slave node started'
        return
    def stop(self):
        self.subnet.stop()
        CompositeNode.stop(self)
        return
    def _find_device(self, address):
        if self.device_map.has_key(address):
            return self.device_map[address]
        try:
            for d in self.children_nodes():
                if d.address == address:
                    self.device_map[address] = d
                    return d
        except:
            pass
        return None
    def buffer(self, initializer=None):
        return base.buffer(initializer)

    def crc(self, byte_array):
        return 0 #always answer that crc is good

    def read(self, header, n , timeout): #pretend to be a serial port but do nothing
        pass
        
    def command(self, buffer):  #called from tcp or serial threads
        cmd = server.ServerCommand(decode=buffer)
        device = self._find_device(cmd.slave_address)
        if device:
            if self.debug: print 'command received: ' + str(cmd)
            response = device.perform_command(cmd)
        else:
            if self.debug: print 'SerialServer, no device found'
            response = None
        return response
Exemplo n.º 2
0
class Slaves(CompositeNode):
    def __init__(self):
        self.subnet = VirtualSubNet(self)
        return

    def configure(self, config):
        CompositeNode.configure(self, config)
        set_attribute(self, 'debug', debug, config, int)
        #
        #
        if debug: self.debug = debug
        #
        #
        self.port = self  #intercept any calls to the port
        return

    def configuration(self):
        config = CompositeNode.configuration(self)
        get_attribute(self, 'debug', config, str)
        return config

    def start(self):
        self.device_map = {}
        if self.debug: print 'Start modbus serial slave node'
        CompositeNode.start(self)
        self.subnet.start()
        if self.debug: print 'modbus serial slave node started'
        return

    def stop(self):
        self.subnet.stop()
        CompositeNode.stop(self)
        return

    def _find_device(self, address):
        if self.device_map.has_key(address):
            return self.device_map[address]
        try:
            for d in self.children_nodes():
                if d.address == address:
                    self.device_map[address] = d
                    return d
        except:
            pass
        return None

    def buffer(self, initializer=None):
        return base.buffer(initializer)

    def crc(self, byte_array):
        return 0  #always answer that crc is good

    def read(self, header, n,
             timeout):  #pretend to be a serial port but do nothing
        pass

    def command(self, buffer):  #called from tcp or serial threads
        cmd = server.ServerCommand(decode=buffer)
        device = self._find_device(cmd.slave_address)
        if device:
            if self.debug: print 'command received: ' + str(cmd)
            response = device.perform_command(cmd)
        else:
            if self.debug: print 'SerialServer, no device found'
            response = None
        return response