通过Flask将串行数据传递给javascript [英] Passing serial data to javascript via Flask

查看:152
本文介绍了通过Flask将串行数据传递给javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在推动从Flask到Javascript的数据,主要是通过修改这里找到的代码: https://github.com/clebio/flask-d3



现在我试图从COM端口读取数据并通过Flask将其传递给的JavaScript。在使用PySerial之前,我已经做了一些这样的事情,但是我不确定将它合并到Flask中的正确方法。



我觉得实现这样一个东西,在每个请求中,端口被打开,读取一行并关闭。

这并不是非常有效率,理想情况下,我可以打开COM端口一次,当Flask从客户端收到请求时运行readline,只有当它收到一个命令明确这样做。我怀疑这涉及线程,但这是我的经验之外。
任何想法?

解决方案

我使用 Tornado 为Arduino + Raspberry Pi项目:

https://github.com/lilspikey/train



基本上我会跑一个单独的线程从串行端口读取,然后解码数据,然后通过websockets作为JSON数据发送。

你也许可以做类似的事情。只需将您读取的数据存储在某个地方,稍后可以通过Flash应用读取。这可能是一个简单的内存数据结构(需要是线程安全的),或者像写入数据库一样复杂。您需要确保您在Flash应用程序读取旧数据时将其删除,但这将是关键所在。


I've been poking around with pushing data from Flask to Javascript, mainly by modifying the code found here: https://github.com/clebio/flask-d3

Now I'm trying to read data from a COM Port and pass it through Flask to Javascript. I've done some of this before using PySerial, but am not sure the right way to approach combining it into Flask.

I feel comfortable implementing something where, with each request, the port is opened, reads a line, and is closed.

That's not very efficient, so ideally I would be able to open the COM port once, run a readline when Flask receives a request from the client, and close only when it receives a command to explicitly do so. I suspect this involves threading, but that is outside of my experience. Any thoughts?

解决方案

I've done something similar using Tornado for an Arduino + Raspberry Pi project:

https://github.com/lilspikey/train

Basically I would run a separate thread to read from the serial port and then decode the data, before sending it as JSON data via websockets.

You could probably do something similar. Just store the data you've read somewhere that can later be read by the Flash app. This could be a simple in memory data structure (will need to be threadsafe) or as complex as writing it to a database. You'd need to make sure you removed old data as it gets read by the Flash app, but that would be the crux of it.

这篇关于通过Flask将串行数据传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆