Poodletooth-iLand/panda/python/Lib/serial/buffer.py

15 lines
544 B
Python
Raw Normal View History

2015-03-03 16:10:12 -06:00
import serial
class BufferedReader(serial.Serial):
def __init__(self):
self.buffer = []
def read(self, size):
while len(self.buffer) < size:
self.buffer.append(serial.Serial.read(self, 1)) #block
self.buffer.extend(list(serial.Serial.read(self, self.inWaiting()))) #and get what's there
a, self.buffer = self.buffer[:size], self.buffer[size:] #take the requested amount
return ''.join(a)
def pushback(self, what):
self.buffer = list(what) + self.buffer