def send_message(self, message, tries=0, no_wait=False): """ sends input with write returns output with read. if skip_read is True, it only returns true, you have to read yourself. """ if message: self.write(message.as_bin()) # time.sleep(0.1) acknowledge = self.connection.read(1) self.slog(acknowledge, True) # if nak, we retry, if ack, we read, if other, we raise. if acknowledge == ensure_bytes(chr(ACK)): # everything alright. if not no_wait: return self.receive() return True elif acknowledge == ensure_bytes(chr(NAK)): # not everything allright. #if tries < 3: # return self.send_message(message, tries + 1, no_answer) #else: raise common.TransportLayerException("Could not send message") elif not acknowledge: # this happens quite a lot with the ingenico devices. # possibly a workaround would be nice. raise common.TransportTimeoutException( "No Answer, Possible Timeout") else: raise common.TransportLayerException( "Unknown Acknowledgment Byte %s" % conv.bs2hl(acknowledge))
def send_message(self, message, tries=0, no_wait=False): """ sends input with write returns output with read. if skip_read is True, it only returns true, you have to read yourself. """ if message: self.write(message.as_bin()) acknowledge = b'' ts_start = time() while not acknowledge: acknowledge = self.connection.read(1) # With ingenico devices, acknowledge is often empty. # Just retrying seems to help. if time() - ts_start > 1: break self.slog(acknowledge, True) # if nak, we retry, if ack, we read, if other, we raise. if acknowledge == ensure_bytes(chr(ACK)): # everything alright. if no_wait: return True return self.receive() elif acknowledge == ensure_bytes(chr(NAK)): # not everything allright. # if tries < 3: # return self.send_message(message, tries + 1, no_answer) # else: raise TransportLayerException('Could not send message') elif not acknowledge: raise TransportTimeoutException('No Answer, Possible Timeout') else: raise TransportLayerException( 'Unknown Acknowledgment Byte %s' % bs2hl(acknowledge))
def write_nak(self): try: self.slog([NAK]) finally: self.connection.write(ensure_bytes(chr(NAK)))
def write_ack(self): # writes an ack. try: self.slog([ACK]) finally: self.connection.write(ensure_bytes(chr(ACK)))
def write(self, something=None): if something: try: self.slog(conv.bs2hl(something)) finally: self.connection.write(ensure_bytes(something)) # !?