Receiving serial port data: real-time web display + logging (with downsampling)

时间:2022-04-16 03:54:52

I am working on a small project which involves displaying and recording (for later processing) data received through a serial port connection from some sort of measurement device. I am using a Raspberry Pi to read and store the received information: this is done with a small program written in Python which opens the serial device, reads a frame and stores the data in a MySQL database (there is no need to poll or interact with the device, data is sent automatically).

我正在开展一个小项目,涉及显示和记录(以后处理)通过某种测量设备的串行端口连接接收的数据。我正在使用Raspberry Pi来读取和存储收到的信息:这是用一个用Python编写的小程序完成的,该程序打开串行设备,读取帧并将数据存储在MySQL数据库中(无需轮询或交互)使用设备,数据自动发送)。

The serial data is formatted into frames about 2.5kbits long, which are sent repeatedly at 1200baud, which means that a new frame is received about every 2 seconds.

串行数据被格式化为大约2.5kbits的帧,这些帧以1200波特重复发送,这意味着大约每2秒接收一个新帧。

Now, even though the useful data is just a portion of the frame, that is way too much information to store for what I need, so what I'm currently doing is "downsampling" the data by reading a frame only once per minute. Currently this is done via a cron task which calls my logging script every minute.

现在,即使有用数据只是帧的一部分,这也是存储我需要的信息的太多信息,所以我目前正在做的是通过每分钟只读取一次帧来“下采样”数据。目前,这是通过cron任务完成的,该任务每分钟调用我的日志记录脚本。

The problem with my setup is that the PHP webpage used to display (and process) the received data (pulled from the MySQL database) cannot show new data more than once per minute.

我的设置问题是,用于显示(和处理)接收数据(从MySQL数据库中提取)的PHP网页无法每分钟显示多次新数据。

Thus here come my question:

这就是我的问题:

How would you do to make the webpage show the live data (which doesn't need to be saved), while keeping the logging to the MySQL database @ once per minute?

如何让网页显示实时数据(不需要保存),同时每分钟保持一次MySQL数据库的记录?

I guess the solution would involve some sort of daemon, which stores the data at the specified frequency (once per minute), while keeping the latest received data available for the php webpage (how?). What do you think? Do you have any examples of similar code/applications which I could use as a starting point?

我想解决方案将涉及某种守护进程,它以指定的频率(每分钟一次)存储数据,同时保持最新的接收数据可用于php网页(如何?)。你怎么看?你有任何类似的代码/应用程序的例子,我可以作为一个起点吗?

Thanks!

谢谢!

1 个解决方案

#1


0  

I don't know if I understand your problem correctly, but it appears you want to show a non-stop “stream” of data with your PHP script. If that's the case, I'm afraid this won't be so easy.

我不知道我是否正确理解了您的问题,但似乎您希望使用PHP脚本显示不间断的“数据流”。如果是这样的话,我担心这不会那么容易。

The basic idea of the HTTP protocol is request/response based. Your browser sends a request and receives a (static) response.

HTTP协议的基本思想是基于请求/响应。您的浏览器发送请求并收到(静态)响应。

You could build some sort of “streaming” server, but streaming (such as done by youtube.com) is also not much more than periodically sending static chunks of a video file, and the player re-assembles them into a video or audio “stream”.

您可以构建某种“流式”服务器,但流式传输(例如由youtube.com完成)也不仅仅是定期发送视频文件的静态块,而且播放器会将它们重新组合成视频或音频“流”。

You could, however, look into concepts like “web sockets” and “long polling”. For example, you could create a long-running PHP script that reads a certail file once every two seconds and outputs the value. (Remember to use flush(), or output will be buffered.)

但是,您可以查看“网络套接字”和“长轮询”等概念。例如,您可以创建一个长时间运行的PHP脚本,该脚本每两秒读取一次certail文件并输出该值。 (记得使用flush(),否则输出将被缓冲。)

A smart solution could even output a JavaScript snippet every two seconds, which again would update some sort of <div> container displaying charts and what not.

智能解决方案甚至可以每两秒输出一个JavaScript代码段,这也会更新某种显示图表的

容器,而不会更新。

There are for example implementations of progress meters implemented with this type of approach.

例如,使用这种方法实现的进度计的实现。

#1


0  

I don't know if I understand your problem correctly, but it appears you want to show a non-stop “stream” of data with your PHP script. If that's the case, I'm afraid this won't be so easy.

我不知道我是否正确理解了您的问题,但似乎您希望使用PHP脚本显示不间断的“数据流”。如果是这样的话,我担心这不会那么容易。

The basic idea of the HTTP protocol is request/response based. Your browser sends a request and receives a (static) response.

HTTP协议的基本思想是基于请求/响应。您的浏览器发送请求并收到(静态)响应。

You could build some sort of “streaming” server, but streaming (such as done by youtube.com) is also not much more than periodically sending static chunks of a video file, and the player re-assembles them into a video or audio “stream”.

您可以构建某种“流式”服务器,但流式传输(例如由youtube.com完成)也不仅仅是定期发送视频文件的静态块,而且播放器会将它们重新组合成视频或音频“流”。

You could, however, look into concepts like “web sockets” and “long polling”. For example, you could create a long-running PHP script that reads a certail file once every two seconds and outputs the value. (Remember to use flush(), or output will be buffered.)

但是,您可以查看“网络套接字”和“长轮询”等概念。例如,您可以创建一个长时间运行的PHP脚本,该脚本每两秒读取一次certail文件并输出该值。 (记得使用flush(),否则输出将被缓冲。)

A smart solution could even output a JavaScript snippet every two seconds, which again would update some sort of <div> container displaying charts and what not.

智能解决方案甚至可以每两秒输出一个JavaScript代码段,这也会更新某种显示图表的

容器,而不会更新。

There are for example implementations of progress meters implemented with this type of approach.

例如,使用这种方法实现的进度计的实现。