简体   繁体   English

如何从 JavaScript 读取串口数据

[英]How to read serial port data from JavaScript

I connected an Arduino to my laptop using USB, and I can read the serial data using Processing.我使用 USB 将 Arduino 连接到我的笔记本电脑,我可以使用 Processing 读取串行数据。

Is there any way to get this data in real time into a local webbrowser?有什么方法可以将这些数据实时输入到本地网络浏览器中? For example, a text field that shows the value from the serial port?例如,显示串行端口值的文本字段? It does not have to be connected to the internet.它不必连接到互联网。

The JavaScript version of Processing does not support the following code, which would have been the ideal solution. Processing 的 JavaScript 版本不支持以下代码,这本来是理想的解决方案。

The Processing code is:处理代码是:

myPort = new Serial(this, Serial.list()[0], 9600);
// read a byte from the serial port
int inByte = myPort.read();
// print it
println(inByte);
// now send this value somewhere...?
// ehm...

There is no way to directly access the local machine from the web browser.无法从 Web 浏览器直接访问本地计算机。 For security reasons browsers have very limited access to the machines resources.出于安全原因,浏览器对机器资源的访问非常有限。

To do this one option would be to write an extension for the browser of your choosing.要做到这一点,一种选择是为您选择的浏览器编写一个扩展程序。 Even though extensions also have a variety of limitations.尽管扩展也有各种限制。

Option two would be to use local server to provide the functionality you need.选项二是使用本地服务器来提供您需要的功能。 Personally I recommend using node.js (it's light weight, fast and easy to implement).我个人推荐使用 node.js(它重量轻、快速且易于实现)。 You can read/write serial data using https://github.com/rwaldron/johnny-five (as @kmas suggested) or https://github.com/voodootikigod/node-serialport and than you can use http://socket.io/ to create a simple service and connect to it though the browser.您可以使用https://github.com/rwaldron/johnny-five (如@kmas 建议)或https://github.com/voodootikigod/node-serialport读取/写入串行数据,然后您可以使用http:// socket.io/创建一个简单的服务并通过浏览器连接到它。 Socket.io uses WebSockets in modern browsers and works excepionally well for real-time connections. Socket.io 在现代浏览器中使用 WebSockets,并且在实时连接方面工作得非常好。

I had a similar problem to solve.我有一个类似的问题要解决。 My Data acquisition system (DAQ) (like your arduino) relays data in HTTP, TCP, FTP, as well as serial.我的数据采集系统 (DAQ)(如您的 arduino)以 HTTP、TCP、FTP 以及串行方式中继数据。 I had to capture it on the server and then send it to my webpage in real-time.我必须在服务器上捕获它,然后将其实时发送到我的网页。

The hack I wrote uses nodejs at the server, and connects DAQ to the server using TCP sockets using the "net" module of nodejs and connects the server to the HTML page using socket.io.我写的hack在服务器上使用nodejs,使用nodejs的“net”模块使用TCP套接字将DAQ连接到服务器,并使用socket.io将服务器连接到HTML页面。

The code and context can be found in " How to get sensor data over TCP/IP in nodejs? ".代码和上下文可以在“ 如何在 nodejs 中通过 TCP/IP 获取传感器数据? ”中找到。

I use TCP as I wanted to transmit data over a long distance.我使用 TCP 是因为我想长距离传输数据。 You need to modify the socket protocol to serial.您需要将套接字协议修改为串行。

For serial-to-TCP redirection, you may use bloom from sensorMonkey for Windows or their processing sketch for *nix/Mac OS.对于串行到 TCP 重定向,您可以使用来自 Windows 的 sensorMonkey 的bloom 或其*nix/Mac OS 的处理草图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM