如何从JavaScript读取串口数据 [英] How to read serial port data from JavaScript

查看:307
本文介绍了如何从JavaScript读取串口数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用USB将Arduino连接到我的笔记本电脑,我可以使用Processing读取串行数据。

I connected an Arduino to my laptop using USB, and I can read the serial data using 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.

处理的JavaScript版本不支持以下代码,这可能是理想的解决方案。

The JavaScript version of Processing does not support the following code, which would have been the ideal solution.

处理代码为:

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...


推荐答案

那里无法从Web浏览器直接访问本地计算机。出于安全原因,浏览器对机器资源的访问权限非常有限。

There is no way to directly access the local machine from the web browser. 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.

选项二是使用本地服务器来提供所需的功能。我个人建议使用node.js(它重量轻,快速且易于实现)。您可以使用 https://github.com/rwaldron/johnny-five 读取/写入串行数据(如@kmas建议的那样) )或 https://github.com/voodootikigod/node-serialport ,你可以使用 http://socket.io/ 创建一个简单的服务并通过浏览器连接到它。 Socket.io在现代浏览器中使用WebSockets,并且非常适合实时连接。

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). 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. Socket.io uses WebSockets in modern browsers and works excepionally well for real-time connections.

这篇关于如何从JavaScript读取串口数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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