def recv(self): data = '' flag = utils.bitify(self.flag) escape_byte = utils.bitify(self.escape_byte) # detect start flag while True: yield self.port.bit_arrive.event data += self.port.get(1) if data.endswith(flag): data = '' break # receive data until end flag while True: # receive 8 bits for _ in range(8): yield self.port.bit_arrive.event data += self.port.get(1) # check if is escaping if data.endswith(escape_byte): # delete the escaper byte data = data[:-8] # get the escapee byte for _ in range(8): yield self.port.bit_arrive.event data += self.port.get(1) continue # end flag detected if data.endswith(flag): data = data[:-8] break mint.ret(utils.unbitify(data))
def recv(self, nbytes): while len(self.data) < nbytes: yield self.port.bit_arrive.event ret, self.data = utils.split_at(self.data, nbytes) mint.ret(ret)