목표는 작동하는 직렬 포트에서 읽는 것이지만 RFID 판독기이기 때문에 사용자가 다른 읽기가 버퍼되기 전에 시간 내에 이동할 수 없습니다. 중복 된 (또는 그 이상) 항목이 생성됩니다. 따라서 버퍼링 된 항목을 지우고 잠시 기다려야합니다.twisted python : 직렬 포트 일시 중지 읽기
질문은 수면 기능을 구현하고 입력 버퍼를 비우는 '꼬인'방법은 무엇입니까?
class ReaderProtocol(LineOnlyReceiver):
def connectionMade(self):
log.msg("Connected to serial port")
def lineReceived(self, line):
line = line.decode('utf-8')
log.msg("%s" % str(line))
time.sleep(2) # pauses, but still prints whats in buffer
...
log.startLogging(sys.stdout)
serialPort = SerialPort(ReaderProtocol, "/dev/ttyAMA0", reactor, 2400)
reactor.run()
EDIT : 여기
은 작동 용액
class ReaderProtocol(LineOnlyReceiver):
t, n = 0, 0
def __init__(self):
self.t = time.time()
def connectionMade(self):
log.msg("Connected to serial port")
def lineReceived(self, line):
self.n = time.time()
if self.n > self.t + 2:
line = line.decode('utf-8')
log.msg("%s" % str(line))
self.t = self.n
...
log.startLogging(sys.stdout)
serialPort = SerialPort(ReaderProtocol, "/dev/ttyAMA0", reactor, 2400)
reactor.run()
두 번째 줄부터 마지막 줄까지는''serialPort = SerialPort (ReaderProtocol(),/dev/ttyAMA0 ", reactor, 2400)''즉 클래스를 인스턴스화해야합니다. 그렇지 않으면 전혀 작동하지 않습니다 – stff00