Esempio n. 1
0
 def writeMessage(self,
                  messageID,
                  param1=0,
                  param2=0,
                  destID=c.GENERIC_USB_ID,
                  sourceID=c.HOST_CONTROLLER_ID,
                  dataPacket=None):
     """ Send message to device given messageID, parameters 1 & 2, destination and sourceID ID, and optional data packet, 
     where dataPacket is an array of numeric values. The method converts all the values to hex according to the protocol
     specification for the message, and sends this to the device."""
     if dataPacket != None:
         # If a data packet is included then header consists of concatenation of: messageID (2 bytes),number of bytes in dataPacket (2 bytes), destination byte with MSB=1 (i.e. or'd with 0x80), sourceID byte
         try:
             dataPacketStr = pack(c.getPacketStruct(messageID), *dataPacket)
         except error as e:
             raise error, "Error packing message " + hex(
                 messageID
             ) + "; probably the packet structure is recorded incorrectly in c.getPacketStruct()"
         message = pack(c.HEADER_FORMAT_WITH_DATA, messageID,
                        len(dataPacketStr), destID | 0x80,
                        sourceID) + dataPacketStr
     else:
         # If no data packet then header consists of concatenation of: messageID (2 bytes),param 1 byte, param2 bytes,destination byte, sourceID byte
         message = pack(c.HEADER_FORMAT_WITHOUT_DATA, messageID, param1,
                        param2, destID, sourceID)
     if DEBUG_MODE: self.disp(message, "TX:  ")
     numBytesWritten = self.device.write(message)
Esempio n. 2
0
 def writeMessage(self,messageID,param1=0,param2=0,destID=c.GENERIC_USB_ID,sourceID=c.HOST_CONTROLLER_ID,dataPacket=None):
     """ Send message to device given messageID, parameters 1 & 2, destination and sourceID ID, and optional data packet, 
     where dataPacket is an array of numeric values. The method converts all the values to hex according to the protocol
     specification for the message, and sends this to the device."""
     if dataPacket!=None:
         # If a data packet is included then header consists of concatenation of: messageID (2 bytes),number of bytes in dataPacket (2 bytes), destination byte with MSB=1 (i.e. or'd with 0x80), sourceID byte
         try:
             dataPacketStr=pack(c.getPacketStruct(messageID) , *dataPacket)
         except error as e:
             raise error, "Error packing message " +hex(messageID)+"; probably the packet structure is recorded incorrectly in c.getPacketStruct()"
         message=pack(c.HEADER_FORMAT_WITH_DATA , messageID , len(dataPacketStr) , destID|0x80 , sourceID) + dataPacketStr
     else:
         # If no data packet then header consists of concatenation of: messageID (2 bytes),param 1 byte, param2 bytes,destination byte, sourceID byte
         message=pack(c.HEADER_FORMAT_WITHOUT_DATA,messageID,param1,param2,destID,sourceID)
     if DEBUG_MODE: self.disp(message,"TX:  ")
     numBytesWritten=self.device.write(message)
Esempio n. 3
0
 def readMessage(self):
     """ Read a single message from the device and return tuple of messageID, parameters 1 & 2, destination and sourceID ID, and data packet 
     (if included), where dataPacket is a tuple of all the message dependent parameters decoded from hex, 
     as specified in the protocol documentation. Normally the user doesn't need to call this method as it's automatically called by query()"""
     # Read 6 byte header from device
     headerRaw = self.device.read(c.NUM_HEADER_BYTES)
     if headerRaw == "":
         raise MessageReceiptError, "Timeout reading from the device"
     # Check if a data packet is attached (i.e. get the 5th byte and check if the MSB is set)
     isDataPacket = unpack("B", headerRaw[4])[0] >> 7
     # Read data packet if it exists, and interpret the message accordingly
     if isDataPacket:
         header = unpack(c.HEADER_FORMAT_WITH_DATA, headerRaw)
         messageID = header[0]
         dataPacketLength = header[1]
         param1 = None
         param2 = None
         destID = header[2]
         sourceID = header[3]
         destID = destID & 0x7F
         dataPacketRaw = self.device.read(dataPacketLength)
         if DEBUG_MODE: self.disp(headerRaw + dataPacketRaw, "RX:  ")
         try:
             dataPacket = unpack(c.getPacketStruct(messageID),
                                 dataPacketRaw)
         except error as e:
             # If an error occurs, it's likely due to a problem with the manual inputted data for packet structure in aptconsts
             raise
     else:
         if DEBUG_MODE: self.disp(headerRaw, "RX:  ")
         header = unpack(c.HEADER_FORMAT_WITHOUT_DATA, headerRaw)
         messageID = header[0]
         param1 = header[1]
         param2 = header[2]
         destID = header[3]
         sourceID = header[4]
         dataPacket = None
     # Return tuple containing all the message parameters
     return (messageID, param1, param2, destID, sourceID, dataPacket)
Esempio n. 4
0
 def readMessage(self):
     """ Read a single message from the device and return tuple of messageID, parameters 1 & 2, destination and sourceID ID, and data packet 
     (if included), where dataPacket is a tuple of all the message dependent parameters decoded from hex, 
     as specified in the protocol documentation. Normally the user doesn't need to call this method as it's automatically called by query()"""
     # Read 6 byte header from device
     headerRaw=self.device.read(c.NUM_HEADER_BYTES)
     if headerRaw=="": raise MessageReceiptError, "Timeout reading from the device"
     # Check if a data packet is attached (i.e. get the 5th byte and check if the MSB is set)
     isDataPacket=unpack("B",headerRaw[4])[0]>>7
     # Read data packet if it exists, and interpret the message accordingly
     if isDataPacket:
         header=unpack(c.HEADER_FORMAT_WITH_DATA,headerRaw)
         messageID=header[0]
         dataPacketLength=header[1]
         param1=None
         param2=None
         destID=header[2]
         sourceID=header[3]
         destID=destID&0x7F
         dataPacketRaw=self.device.read(dataPacketLength)
         if DEBUG_MODE: self.disp(headerRaw+dataPacketRaw,"RX:  ")
         try:
             dataPacket=unpack(c.getPacketStruct(messageID),dataPacketRaw)
         except error as e:
             # If an error occurs, it's likely due to a problem with the manual inputted data for packet structure in aptconsts
             raise
     else:
         if DEBUG_MODE: self.disp(headerRaw,"RX:  ")
         header=unpack(c.HEADER_FORMAT_WITHOUT_DATA,headerRaw)
         messageID=header[0]
         param1=header[1]
         param2=header[2]
         destID=header[3]
         sourceID=header[4]
         dataPacket=None
     # Return tuple containing all the message parameters
     return (messageID,param1,param2,destID,sourceID,dataPacket)