Пример #1
0
    def send(self, data):
        data.insert(0, self.messageid)
        tx = bytearray(str(data))
        tx.append(0)
        if TCP_DEBUG == 1:
            safe_print("o=> {0}".format(tx))

        self.eventTx.clear()
        self.response = ""
        self.skt.send(tx)
        self.eventTx.wait()
        return self.response
Пример #2
0
    def run(self):
        safe_print("Connecting to {0}:{1}\n".format(TCP_IP, TCP_PORT))
        self.skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.skt.connect((TCP_IP, TCP_PORT))
        self.eventDone.set()

        buffer = ""
        while self.enabled:
            rxstr = self.skt.recv(BUFFER_SIZE)
            if rxstr == '':
                self.enabled = False
                continue

            buffer += rxstr
            if '\0' in buffer:
                strlist = buffer.split('\0')
                if buffer.endswith('\0'):
                    buffer = ""
                else:
                    buffer = strlist[len(strlist) - 1]
                    strlist = strlist[:-1]

                for s in strlist:
                    if len(s) == 0:
                        continue
                    json = s.translate(None, '\n\r\0')
                    json = eval(json)
                    if TCP_DEBUG == 1:
                        safe_print("i<= {0}".format(json))
                    if json[0] == self.messageid:
                        self.messageid += 1
                        self.response = None
                        if len(json) > 1:
                            self.response = json[1]
                        self.eventTx.set()
                    elif json[0] == "Console":
                        for l in self.console_listeners:
                            l.callback(json[1])
                    else:
                        raise ValueError(
                            'Unexpected simulation response: {0}'.format(json))
            else:
                pass

        # Ending of the thread:
        safe_print("Thread {0} stopped".format(self.name))