Note that there are some explanatory texts on larger screens.

plurals
  1. POtwisted.protocols.basic.LineReceiver only reading one entry (then nothing)
    text
    copied!<p>I'm using the following protocol class to read and parse data coming in from an RFID serial connection. The connection works fine just using pyserial and spitting out what it reads. twisted seems to have an issue reading more than one entry - I'll be able to scan in one of the test accounts and it stops there.</p> <p>Here's the confusing part - it's not consistent. Sometimes it works as expected, but most times not. What am I doing wrong here?</p> <pre><code>import time, sys from twisted.internet import reactor, fdesc from twisted.python import usage, log from twisted.protocols.basic import LineReceiver from twisted.internet.serialport import SerialPort from twisted.web.server import Site from twisted.web.static import File from autobahn.websocket import listenWS from autobahn.wamp import WampServerFactory, WampServerProtocol, exportRpc test_people = [ ('8400346926', 'Mary Jones', 'http://placehold.it/150x150&amp;text=Mary+Jones'), ('6500129A82', 'Bob Smith', 'http://placehold.it/150x150&amp;text=Bob+Smith'), ] class ReaderProtocol(LineReceiver): t, n, first_pass_time = 0, 0, 0 first_pass, line, _read_cache, __buffer = None, None, (None, None), '' def __init__(self, wsFactory): log.msg("Connected to wsfactory %s" % str(wsFactory)) self.t = time.time() self.wsFactory = wsFactory self.__buffer = '' def connectionMade(self): log.msg("Connected to serial port") def find_person(self, id): for p in test_people: if p[0] == id: return p def dataReceived(self, data): stripped_data = data.decode('utf-8').strip() if len(stripped_data) == 7: self.__buffer = stripped_data elif len(stripped_data) == 3 and len(self.__buffer) == 7: d = self.__buffer + stripped_data self.__buffer = '' if len(d) == 10: return self.lineReceived(d) else: log.msg("Incorrect data size. Resetting buffer.") else: self.__buffer = '' log.msg("Incorrect frame size.") return def lineReceived(self, line): i = line.decode('utf-8').strip() self.n = time.time() if not self.first_pass: self.first_pass = i self.first_pass_time = time.time() self.logLogin(i) elif time.time() - self.first_pass_time &gt; 1.5: # make sure past 1.5 seconds self.first_pass = i self.first_pass_time = time.time() else: # Make sure that it's not just noise if self.first_pass_time &gt; 0 and self.n - self.first_pass_time &lt; 1.5: # not noise self.first_pass = None self.first_pass_time = 0 self.logLogin(i) else: self.__buffer = '' return def logLogin(self, code): f = code != self._read_cache[0] or self._read_cache[1] + 30 &lt;= self.n if self.n &gt; self.t + 1.5 and f: info = self.find_person(code) if info: log.msg("ID: %s - Name: %s - Image URL: %s" % (info[0], info[1], info[2])) j = "%s|%s|%s" % (info[0], info[1], info[2]) self.wsFactory.dispatch("http://example.com/serial#login", j) self.t = self.n else: self.wsFactory.dispatch("http://example.com/serial#error", "Unable to find this person.") self._read_cache = (code, self.n) else: pass # this can be safely ignored return </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload