def send_raw(self, data, target): packet = str(target) + DEVICEID + b64.encode(data) sum = b64.checksum(packet) packet += sum + '\r\n' self._packetcond.acquire() self._packetlist.append(packet) self._packetcond.release() self._lock.acquire() debug('Sent packet %r', packet) self._serial.write(packet) self._serial.flush() self._lock.release() Timer(get_standoff(), lambda: self._check_recieved(packet)).start()
def _monitor(self): while True: line = self._serial.readline().strip() debug('Received raw comm input: %r', line) if len(line) < 4: continue if (not line.startswith(DEVICEID) and not line.startswith('d') and not line.startswith('e')): continue if line[1] == '$' and len(line) == 4: # ACK self._packetcond.acquire() index = None for (i, packet) in enumerate(self._packetlist): if line[2:4] == packet[-4:-2]: debug('Package ACK: %r', line) print('Package ACK: '+ line + '\n') index = i break if index is not None: try: self._packetlist.pop(index) except: pass if self._packetlist == []: self._packetcond.notify() self._packetcond.release() continue if not b64.valid(line): continue if b64.checksum(line[:-2]) != line[-2:]: continue self._lock.acquire() self._serial.write(line[1] + '$' + line[-2:] + '\r\n') self._serial.flush() self._lock.release() data = b64.decode(line[2:-2]) if line.startswith('d'): info('Debug message recieved: %r', data) print data elif line.startswith('e'): error('Error message recieved: %r', data) else: print data for callback in self._callbacks: thread.start_new_thread(callback, (data,))
def _monitor(self): while True: line = self._serial.readline().strip() debug('Received raw comm input: %r', line) if len(line) < 4: continue if (not line.startswith(DEVICEID) and not line.startswith('d') and not line.startswith('e')): continue if line[1] == '$' and len(line) == 4: # ACK self._packetcond.acquire() index = None for (i, packet) in enumerate(self._packetlist): if line[2:4] == packet[-4:-2]: debug('Package ACK: %r', line) print('Package ACK: ' + line + '\n') index = i break if index is not None: try: self._packetlist.pop(index) except: pass if self._packetlist == []: self._packetcond.notify() self._packetcond.release() continue if not b64.valid(line): continue if b64.checksum(line[:-2]) != line[-2:]: continue self._lock.acquire() self._serial.write(line[1] + '$' + line[-2:] + '\r\n') self._serial.flush() self._lock.release() data = b64.decode(line[2:-2]) if line.startswith('d'): info('Debug message recieved: %r', data) print data elif line.startswith('e'): error('Error message recieved: %r', data) else: print data for callback in self._callbacks: thread.start_new_thread(callback, (data, ))