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

查看:152
本文介绍了如何从 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 版本的 Processing 不支持以下代码,这本来是理想的解决方案.

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

推荐答案

没有办法从网页浏览器直接访问本地机器.出于安全原因,浏览器对机器资源的访问非常有限.

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天全站免登陆