diff --git a/script/RobotTCP.py b/script/RobotTCP.py index 5f9381a..4fd16e6 100644 --- a/script/RobotTCP.py +++ b/script/RobotTCP.py @@ -18,8 +18,8 @@ class RobotTCP(TcpDevice, Stoppable): self.working_mode = None self.status = None self.lock = threading.Lock() - def _sendReceive(self, msg_id, msg = ""): + """ self.lock.acquire() try: tx = self.header if (self.header != None) else "" @@ -28,19 +28,44 @@ class RobotTCP(TcpDevice, Stoppable): raise Exception("Exceeded maximum message size") self.getLogger().finer("TX = '" + str(tx)+ "'") if (self.trailer != None): tx = tx + self.trailer - rx = self.sendReceive(tx, None, self.trailer , 0, self.timeout, self.retries) + start = time.time() + sending = True + while(True): + if sending: + rx = self.sendReceive(tx, None, self.trailer , 0, self.timeout, self.retries) + else: + rx = self.wa + rx=rx[:-1] #Remove 0A + self.getLogger().finer("RX = '" + str(rx) + "'") + if rx[:3] != msg_id: + if (time.time()-start) >= timeout: + raise Exception("Received invalid message id: " + str(rx[:3]) + " - expecting:" + msg_id ) + else: + if len(rx)<4: + raise Exception("Invalid message size: " + str(len(rx)) ) + if rx[3] == "*": + raise Exception(rx[4:]) + return rx[4:] + finally: + self.lock.release() + """ + tx = self.header if (self.header != None) else "" + tx = tx + msg_id + " " + msg + if (len(tx)>127): + raise Exception("Exceeded maximum message size") + self.getLogger().finer("TX = '" + str(tx)+ "'") + if (self.trailer != None): tx = tx + self.trailer + rx = self.sendReceive(tx, None, self.trailer , msg_id, self.timeout, self.retries) rx=rx[:-1] #Remove 0A self.getLogger().finer("RX = '" + str(rx) + "'") - if rx[:3] != msg_id: - print rx - raise Exception("Received invalid message id: " + str(rx[:3]) + " - expecting:" + msg_id ) + if rx[:3] != msg_id: + if (time.time()-start) >= timeout: + raise Exception("Received invalid message id: " + str(rx[:3]) + " - expecting:" + msg_id ) if len(rx)<4: raise Exception("Invalid message size: " + str(len(rx)) ) if rx[3] == "*": raise Exception(rx[4:]) return rx[4:] - finally: - self.lock.release() def call(self, msg): id = "%03d" % self.msg_id