diff --git a/script/RobotTCP.py b/script/RobotTCP.py index d9d2ae0..5f9381a 100644 --- a/script/RobotTCP.py +++ b/script/RobotTCP.py @@ -1,4 +1,4 @@ - +import threading class RobotTCP(TcpDevice, Stoppable): def __init__(self, name, server, timeout = 500, retries = 1): @@ -17,26 +17,31 @@ class RobotTCP(TcpDevice, Stoppable): self.empty = None 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 "" + 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 , 0, 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 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 _sendReceive(self, msg_id, msg = ""): - 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 , 0, 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 len(rx)<4: - raise Exception("Invalid message size: " + str(len(rx)) ) - if rx[3] == "*": - raise Exception(rx[4:]) - return rx[4:] - def call(self, msg): id = "%03d" % self.msg_id self.msg_id = (self.msg_id+1)%1000