def sendWriteCommand(self, cmd, prepend_ack=True): if not self.connected: raise IcePAPException(IcePAPException.ERROR, "Connection error", "no connection with the Icepap sytem") # BUG FOUND DOING AND ACK TO ALL COMMANDS BY DEFAULT if cmd.startswith('PROG') or cmd.startswith('RESET') or \ cmd.startswith('*PROG') or cmd.startswith(':') or \ cmd.startswith('_'): prepend_ack = False if cmd.count('RESET') > 0: prepend_ack = False if prepend_ack: ack_cmd = cmd if cmd.find('#') != 0: ack_cmd = '#' + cmd ans = self.sendWriteReadCommand(ack_cmd) if ans.find('OK') == -1: msg = 'Error sending command %s, icepap answered %s' % (cmd, ans) iex = IcePAPException(IcePAPException.ERROR, "SendWriteCommand failed the 'ACK'", msg) raise iex return try: message = cmd cmd = cmd + "\n" self.lock.acquire() self.IcPaSock.send(cmd) self.writeLog(message) self.lock.release() if self.DEBUG: print "SEND:>%s<" % cmd except socket.timeout as msg: if self.DEBUG: print "socket TIME OUT" self.writeLog(message + " " + msg) self.lock.release() self.disconnect() iex = IcePAPException(IcePAPException.TIMEOUT, "Connection Timeout", msg) raise iex except socket.error as msg: if self.DEBUG: print "socket ERROR: %s" % msg self.writeLog(message + " " + str(sys.exc_info())) self.lock.release() self.disconnect() # print "Unexpected error:", sys.exc_info() iex = IcePAPException(IcePAPException.ERROR, "Error sending command to the Icepap", msg) raise iex
def sendData(self, data): try: self.lock.acquire() # self.IcPaSock.send(data) self.IcPaSock.sendall(data) self.lock.release() except socket.timeout as msg: self.lock.release() # print msg iex = IcePAPException(IcePAPException.TIMEOUT, "Connection Timeout", msg) raise iex except socket.error as msg: self.lock.release() # print msg iex = IcePAPException(IcePAPException.ERROR, "Error sending data to the Icepap", msg) raise iex
def connect(self): total_sleep = 0 inc = self.timeout / 10.0 while (not self.connected) and (total_sleep < self.timeout): time.sleep(inc) total_sleep += inc if self.connected: return True raise IcePAPException(IcePAPException.ERROR, "Connection error", "no connection with the Icepap sytem")
def disconnect(self): if self.DEBUG: print "disconnecting from icepap..." if (self.Status == CStatus.Disconnected): if self.DEBUG: print "I was already disconnected!..." return try: self.IcPaSock.close() self.closeLogFile() self.Status = CStatus.Disconnected self.connected = 0 except BaseException: iex = IcePAPException(IcePAPException.ERROR, "Error disconnecting the Icepap") raise iex
def sendWriteReadCommand(self, command): try: message = command self.tty.write(command + '\r\n') time.sleep(0.02) newdata = self.readline() newdata = self.readline() message = message + "\t\t[ " + newdata + " ]" self.writeLog(message) return newdata except BaseException: iex = IcePAPException( IcePAPException.Error, "Error sending command to the IcePAP(Serial)") raise iex
def sendWriteReadCommand(self, cmd, size=8192): if not self.connected: raise IcePAPException(IcePAPException.ERROR, "Connection error", "no connection with the Icepap sytem") try: # print "sendWriteReadCommand" message = cmd cmd = cmd + "\n" self.lock.acquire() self.IcPaSock.send(cmd) data = self.IcPaSock.recv(size) if data.count("$") > 0: ################################################ # WORKAROUND ################################################ # AS IT IS SAID IN https://docs.python.org/3/howto/sockets.html # SECTION "Using a Socket" # # A protocol like HTTP uses a socket for only one # transfer. The client sends a request, the reads a # reply. That's it. The socket is discarded. This # means that a client can detect the end of the reply # by receiving 0 bytes. # # But if you plan to reuse your socket for further # transfers, you need to realize that there is no # "EOT" (End of Transfer) on a socket. I repeat: if a # socket send or recv returns after handling 0 bytes, # the connection has been broken. If the connection # has not been broken, you may wait on a recv forever, # because the socket will not tell you that there's # nothing more to read (for now). Now if you think # about that a bit, you'll come to realize a # fundamental truth of sockets: messages must either # be fixed length (yuck), or be delimited (shrug), or # indicate how long they are (much better), or end by # shutting down the connection. The choice is entirely # yours, (but some ways are righter than others). # # WE SHOULD WAIT UNTIL THE TERMINATOR CHAR '$' IS # FOUND while data.count('$') < 2: data = data + self.IcPaSock.recv(size) ################################################ self.lock.release() data = data.rstrip("\n\r") message = message + "\t\t[ " + data + " ]" self.writeLog(message) if self.DEBUG: print "\t\tSEND: %s\t\tRECEIVE: %s" % (cmd, data) return data except socket.timeout as msg: if self.DEBUG: print "socket TIME OUT" self.writeLog(message + " " + str(msg)) self.lock.release() self.disconnect() iex = IcePAPException(IcePAPException.TIMEOUT, "Connection Timeout", msg) raise iex except socket.error as msg: if self.DEBUG: print "socket ERROR: %s" % msg a, b, c = sys.exc_info() e, f = b self.writeLog(message + " " + str(sys.exc_info())) self.lock.release() self.disconnect() if e == errno.ECONNRESET or e == errno.EPIPE: # print "Disconnected socket\n" # self.connect_retry() pass else: iex = IcePAPException(IcePAPException.ERROR, "Error sending command to the Icepap", msg) raise iex