def communicate(self,msg,regex,timeout,isFlush=True): """ | Clears rawBuffer and packetBuffer. | Sends *msg*. | Waits until either the packet buffer has a match of *regex* or until *timeout* seconds have passed. | If there was a match it returns it. | If there was no match an Exception is raised. Args: * msg (str): The string/message to send * regex (str): A regex, same syntax as the standard python *re* module uses * timeout (float): Timeout in seconds * isFlush (bool): Indicates if flush or not before communicating Returns: A *re.MatchObject* instance """ for i in range (self.retries+1): if isFlush: with self.pollingThreadLock: while True: data = self.driver.receive(999) if len(data)>0: displayData = pUtils.formatHex(data) + '\n' self.updateConsoleBuffer(displayData) self.updateLogFileBuffer(displayData) else: break self.flushRawBuffer() self.flushPacketBuffer() try: self.transmit(msg) t = self.receive(regex,timeout) if i>0: if 'packetRetryCountSuccessList' not in self.configurationManager.selfStats: self.configurationManager.selfStats['packetRetryCountSuccessList']=[] self.configurationManager.selfStats['packetRetryCountSuccessList'].append(i) return t except Exception,e: from MTP.core.Sequencer import sanitizeString packetRetryInfoEntry = {'i':i,'msg':pUtils.formatHex(msg),'regex':pUtils.formatHex(regex),'timeout':timeout,'exceptionStr':sanitizeString(str(e))} if 'packetRetryInfoEntryList' not in self.configurationManager.selfStats: self.configurationManager.selfStats['packetRetryInfoEntryList']=[] self.configurationManager.selfStats['packetRetryInfoEntryList'].append(packetRetryInfoEntry)
def pollingFunction(self): """ | Reads from the specific instantiated driver (see :ref:`label_drivers`). | Updates all buffers with the new data. | Parses rawBuffer and places hte packets into packetBuffer. | | This function is meant to be called by (and only by) the *pollingThread*. Args: None Returns: None """ with self.pollingThreadLock: #Read data = self.driver.receive(999) #If new data, update buffers if len(data)>0: self.updateRawBuffer(data) displayData = pUtils.formatHex(data) + '\n' self.updateConsoleBuffer(displayData) self.updateLogFileBuffer(displayData) self.parsePackets()
def printVal(filePath, x, y, **kwargs): try: img = loadImage(filePath) except IOError as e: pprint('Error: ', color=COLOR.RED, endLine=False) pprint('[I/O] ({0}): {1}'.format(e.errno, e.strerror)) exit(1) except Exception: pprint('Error: ', color=COLOR.RED, endLine=False) pprint('Unsupported image format') exit(1) start = (img.width * y + x) * img.bytesPerPixel pprint(pUtils.formatHex(img.data[start:start + img.bytesPerPixel]))
def test_Test_formatHex_2(self): data = '\x00\x01\x02\x03\xFF' self.assertEqual(pUtils.formatHex(data),'00 01 02 03 FF')
def test_Test_formatHex_1(self): data = bytearray('\x00\x01\x02\x03\xFF') self.assertEqual(pUtils.formatHex(data),'00 01 02 03 FF')
def receive(self,regex,timeout): """ | Waits until either there is a match of *regex* or until *timeout* seconds have passed. | If there was a match it returns it. | If there was no match an Exception is raised. Args: * regex (str): A regex, same syntax as the standard python *re* module uses * timeout (float): Timeout in seconds * interval (float): Time in seconds to sleep between checks to the buffer Returns: A *re.MatchObject* instance """ timeAnchor = time.time() while(time.time()-timeAnchor<timeout): t = self.getPacket(regex) if t : return t sleep(self.readRetryInterval) raise Exception('Communicator: Did not receive expected answer within timeout. regex='+pUtils.formatHex(regex))