Exemple #1
0
    def send(self, opcode, opType, motor, value, moduleID=None):
        """
        Send a TMCL datagram and read back a reply. This function blocks until
        the reply has been received
        """
        if not (type(opcode) == type(opType) == type(motor) == type(value) ==
                int):
            raise TypeError("Expected integer values")

        # If no module ID is given, use the default one
        if not moduleID:
            moduleID = self._MODULE_ID

        request = TMCL_Request(moduleID, opcode, opType, motor, value)

        if self._debug:
            request.dump()

        # Send the request
        self._send(self._HOST_ID, moduleID, request.toBuffer())

        # Read out the reply
        reply = TMCL_Reply(self._recv(self._HOST_ID, moduleID))

        if self._debug:
            reply.dump()

        return reply
    def handle_request(self, request):
        reply = TMCL_Reply(reply_address=self.__host_address, module_address=self.__module_address, status=TMCL_Status.SUCCESS, value=0, command=request.command)

        command_func = self._get_command_func(request.command)
        if(command_func):
            reply = command_func(request, reply)
        else:
            reply.status = TMCL_Status.INVALID_COMMAND

        if(not(reply.special)):
            #reply.reply_address = self.__host_address
            reply.calculate_checksum()

        return reply
Exemple #3
0
 def get_version_ascii(self, request, reply):
     self.__logger.debug("get_version_ascii")
     reply_data = bytearray(1) + self.__version_string.encode("ascii")
     reply_data[0] = self.__host_address
     reply = TMCL_Reply.from_buffer(reply_data)
     reply.special = True
     return reply
Exemple #4
0
    def send ( self, address, command, commandType, motorbank, value ):
        """
        Send a message to the specified module. This is a blocking function that
        will not return until a reply has been received from the module. 
        """
       
        "prepare TMCL request"
        request = TMCL_Request(address, command, commandType, motorbank, value)
        
        if self.debugEnabled:
            request.dump()
        
        "send request, wait, and handle reply"
        self.serial.write(request.toBuffer())
        reply = TMCL_Reply(struct.unpack(TMCL.PACKAGE_STRUCTURE, self.serial.read(TMCL.PACKAGE_LENGTH)))
        
        if self.debugEnabled:
            reply.dump()

        return reply
    def send_request(self, request, moduleID=None):
        """
        Send a TMCL_Request and read back a TMCL_Reply. This function blocks until
        the reply has been received.
        """

        if not moduleID:
            moduleID = self._MODULE_ID

        if self._debug:
            request.dump()

        self._send(self._HOST_ID, moduleID, request.toBuffer())
        reply = TMCL_Reply.from_buffer(self._recv(self._HOST_ID, moduleID))

        if self._debug:
            reply.dump()

        return reply
Exemple #6
0
logger.info("Test TMCL_Bridge")

logger.info("Initializing interfaces ...")
host = virtual_tmcl_interface()
modules = [{"module": virtual_tmcl_interface()}]
bridge = TMCL_Bridge(host, modules)
logger.info("Interfaces initialized.")

# Prepare requests
host.send_request_only(TMCL_Request(1, 2, 3, 4, 5))
host.send_request_only(TMCL_Request(3, 2, 3, 4, 5))
host.send_request_only(TMCL_Request(3, TMCL.COMMANDS["TMCL_UF0"], 0, 0,
                                    0))  # stop

# Prepare replies
modules[0]["module"].send_reply(TMCL_Reply(2, 1, 1, 2, 3))

while (not (bridge.process())):
    pass

if (host.data_available()):
    reply = host.receive_reply()
    assert ((reply.reply_address == 2) and (reply.module_address == 1)
            and (reply.status == 1) and (reply.command == 2)
            and (reply.value == 3)), "Received reply does not match sent reply"
else:
    assert False, "Not enough replies"
if (host.data_available()):
    reply = host.receive_reply()
    assert (reply.status != 100), "Status should not be OK here"
else: