コード例 #1
0
class TMCL_Bridge(object):
    '''
    Initialize the TMCL bridge.

    Parameters:
        host_connection: tmcl_interface to the main host.
        module_connection: tmcl_interface to the module.
        module_id: module ID to be used in control mode.
        host_id: host ID to be used in control mode.
    '''

    def __init__(self, host_connection, module_connections, module_id=3, host_id=2):
        self.__host = host_connection
        self.__modules = module_connections
        self.__slave = TMCL_Slave_Bridge(module_id, host_id)
        self.__logger = logging.getLogger(self.__module__)

    def process(self):
        '''
        1. Receive request from host
        2. Send request to module
        3. Receive reply from module
        4. Send reply to host
        '''
        if(self.__host.request_available()):
            request = self.receive_request()
            self.__logger.debug("Processing request ({}).".format(str(request)))
            if(self.__slave.filter(request)): # Control request
                self.__logger.debug("Request is a control request.")
                reply = self.__slave.handle_request(request)
                self.__logger.debug("Control reply ({}).".format(str(reply)))
                self.send_reply(reply)
            else: # Passthrough request
                self.__logger.debug("Request is not a control request. Passing through.")
                for module in self.__modules:
                    if(module.get("request_callback")):
                        request = module["request_callback"](request)
                    try:
                        module["module"].send_request_only(request)
                    except:
                        continue
                time.sleep(0.1)
                for module in self.__modules:
                    if(module["module"].data_available()):
                        reply = module["module"].receive_reply()
                        self.__logger.debug("Passthrough reply ({}).".format(str(reply)))
                        if(module.get("reply_callback")):
                            reply = module["reply_callback"](reply)
                        self.send_reply(reply)
                        break
        return self.__slave.status.stop

    def receive_request(self):
        return self.__host.receive_request()

    def send_request(self, request):
        return self.__module.send_request(request)

    def send_reply(self, reply):
        self.__host.send_reply(reply)
コード例 #2
0
 def __init__(self,
              host_connection,
              module_connections,
              module_id=3,
              host_id=2):
     self.__host = host_connection
     self.__modules = module_connections
     self.__slave = TMCL_Slave_Bridge(module_id, host_id)
     self.__logger = logging.getLogger(self.__module__)
コード例 #3
0
# Constants
MODULE_ADDRESS = 1
HOST_ADDRESS = 2
MODULE_ID_STRING = "0960"
VERSION_STRING = MODULE_ID_STRING + "V100"
BUILD_VERSION = 0

# Prepare Logger
logger = logging.getLogger(__name__)
logger.info("TMCL Slave on RS485 interface")

# Main program

logger.info("Initializing interface ...")
con = rs485_tmcl_interface()
slave = TMCL_Slave_Bridge(MODULE_ADDRESS, HOST_ADDRESS, VERSION_STRING,
                          BUILD_VERSION)
logger.info("Interface initialized.")

while (not (slave.status.stop)):
    if (con.request_available()):
        logger.debug("Request available.")
        request = con.receive_request()
        if (not (slave.filter(request))):
            continue
        logger.debug("Request for this slave detected.")
        reply = slave.handle_request(request)
        con.send_reply(reply)

logger.info("Closing interface ...")
con.close()
logger.info("Interface closed.")