如何处理RFID读写器接收到的不稳定数据?
我的应用程序必须每 2 秒连接一次 RFID 读取器并接收数据,因此它是在 django 框架上开发的。以下是 views.py 中的相关行:
HOST = '192.168.0.1'
PORT = 50007
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.settimeout(2)
soc.connect((HOST, PORT))
soc.send('tag.db.scan_tags(100)\r\n')
datum = soc.recv(128)
if datum.find("ok") > -1:
soc.send('tag.read_id()\r\n')
data = soc.recv(8192)
如果 RFID 读取器在其字段中发现任何 RFID 标签,应用程序会将接收到的数据呈现为模板 {{ data }}。当字段中没有标签或无法读取标签时,就会出现问题,{{ data }} 变量在页面上不会显示任何内容。
我希望我的应用程序能够显示最后可以读取的数据。如果没有新的数据到来,则只显示最新的数据。只有当新数据到来时,数据才会改变。这将使我的应用程序更加稳定。
请问有什么建议吗?非常感谢。
My application has to connect and receive data from RFID reader for every 2 seconds so it has developed on django framework. Here are related lines in views.py:
HOST = '192.168.0.1'
PORT = 50007
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.settimeout(2)
soc.connect((HOST, PORT))
soc.send('tag.db.scan_tags(100)\r\n')
datum = soc.recv(128)
if datum.find("ok") > -1:
soc.send('tag.read_id()\r\n')
data = soc.recv(8192)
The application would render received data to a template as {{ data }} if the RFID reader found any RFID tag in its field. The problem occurs when there is no tag in field or tags cannot be read, {{ data }} variable would show nothing on the page.
I want my application able to show the last data that can be read. If there is no new data come, just show the latest one. The data would be changed only if new data come. This will makes my application more stable.
Any suggestion, please? Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更简单的事情之一是使用 Django 的 缓存框架,并且将数据存储在本地内存、memcached 或数据库等中。您可以缓存收到的任何数据,如果没有收到数据,或者数据错误或其他任何情况,则使用缓存的数据:
您还可以将数据存储在其他位置方式,例如在模型上。您可能应该将 RFID 读取部分移出视图,并使用 celery (或其他内容)将其作为任务,保存结果,然后在视图中使用最新保存的数据
One of the easier things to do would be to use Django's cache framework, and store the data in local memory, or memcached, or the database, etc. You could cache any data recieved, and used the cached data if you don't recieve data, or it's erroneous or whatever:
You could also store the data other ways, on a model for instance. You should probably move the RFID reading portion out of the view, and use celery (or something else) to run it as a task, save the results, and just use the latest saved data in your view