javascript 如何使用javascript通过客户端的串口进行通信?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11681407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to communicate through serial port on client side using javascript?
提问by ipohfly
Recently received a request to add communication to a device connected via serialport on the client's machine through my webpage.
最近收到一个请求,通过我的网页向通过客户端机器上的串行端口连接的设备添加通信。
I'd done some googling and found that node.js with node-serialport seems to be the javascript way of doing it. However, in my case, the device is actually connected to the client's machine, and not the server.
我做了一些谷歌搜索,发现带有 node-serialport 的 node.js 似乎是这样做的 javascript 方式。但是,就我而言,设备实际上是连接到客户端的机器,而不是服务器。
My question would be how do i implement node.js in this sense? Since the code is run at the client browser, is it possible for me to 'embed' node.js on my webpage?
我的问题是如何在这个意义上实现 node.js?由于代码在客户端浏览器上运行,我是否可以在我的网页上“嵌入”node.js?
Or is there any other alternative for doing this? Applet and ActiveX are out of the picture though.
或者有没有其他替代方法可以做到这一点?Applet 和 ActiveX 不在图片中。
Thanks
谢谢
UPDATES:Had managed to convince the client to have applet loaded from the web, so we'll be going through the applet route. Thanks all for your great info! =)
更新:已成功说服客户端从 Web 加载小程序,因此我们将通过小程序路线。感谢大家提供的精彩信息!=)
回答by Hamish
JavaScript in the browseronly has access to the APIs provided by the browser: it lives in the browser sandbox where it (rightly) has no access to the client file system or other hardware.
浏览器中的JavaScript只能访问浏览器提供的 API:它存在于浏览器沙箱中,它(正确地)无法访问客户端文件系统或其他硬件。
This is different from node.js, which is a server implementation that has access to all sorts of other file system APIs.
这与 node.js 不同,node.js 是一种可以访问各种其他文件系统 API 的服务器实现。
To "break out" of the browser you mustuse some sort of browser extension.
要“突破”浏览器,您必须使用某种浏览器扩展。
回答by Matthew Lock
You will have to create a plugin, an applet, or a client side application to get the data into the client's web browser before being sent off to your server.
您必须创建一个插件、一个小程序或一个客户端应用程序,以便在将数据发送到您的服务器之前将数据输入到客户端的 Web 浏览器中。
You could make a small app that reads the serial port of the clients machine that creates a .js file, and then your web page includes that src of that "dynamically" created js file on the client's machine and presto your webpage gets access to the serial port in a roundabout way.
您可以制作一个小应用程序来读取创建 .js 文件的客户端机器的串行端口,然后您的网页包含该客户端机器上“动态”创建的 js 文件的 src 并且您的网页可以访问串行端口以迂回的方式。
This is how GPSGate works: http://gpsgate.com/developer/gps_in_browser/
GPSGate 是这样工作的:http://gpsgate.com/developer/gps_in_browser/
See also here: How to read from Serial port in a webpage
另请参见此处:如何从网页中的串行端口读取
And a java applet based solution: http://code.google.com/p/java-simple-serial-connector/
以及基于 Java 小程序的解决方案:http: //code.google.com/p/java-simple-serial-connector/
http://code.google.com/p/java-simple-serial-connector/wiki/jSSC_Terminal
http://code.google.com/p/java-simple-serial-connector/wiki/jSSC_Terminal
回答by Andrey Sidorov
回答by Pradeep Kumar
Another option to read the serial port data is using sockjs and sockjs-client-node at the server side and sockjs-0.3.js at the client side.
读取串口数据的另一个选择是在服务器端使用 sockjs 和 sockjs-client-node,在客户端使用 sockjs-0.3.js。
回答by apsillers
This can only be done via Active X or a plugin like Java or Flash. Code in JavaScript is only as powerful as the APIs provided to it from the browser.
这只能通过 Active X 或 Java 或 Flash 等插件来完成。JavaScript 中的代码仅与浏览器提供给它的 API 一样强大。
Want to sent an HTTP request to a server? JavaScript can do that (subject to the same-origin policy), because the browser has the XMLHttpRequest
API. Want to query what processes are running on the operating system right now? JavaScript cannot do that, because no browser provides an API for querying the OS about what processes it is running.
想要向服务器发送 HTTP 请求?JavaScript 可以做到这一点(受同源策略约束),因为浏览器具有XMLHttpRequest
API。想查询操作系统上正在运行哪些进程?JavaScript 不能这样做,因为没有浏览器提供 API 来查询操作系统正在运行的进程。
No browser that I know of implements any kind of JavaScript API for performing serial port operations, so there is no way to do it without using a plugin.
我所知道的浏览器都没有实现任何类型的 JavaScript API 来执行串行端口操作,因此不使用插件就无法做到这一点。
However, this does not preclude the possibility of such an API existing someday: the getUserMedia
function can grab data from a camera or microphone, and it could theoretically be expanded to get data from other devices as well.
然而,这并不排除有朝一日这样的 API 存在的可能性:该getUserMedia
功能可以从摄像头或麦克风获取数据,并且理论上可以扩展以从其他设备获取数据。
回答by Timothy Meade
Another option (other than ActiveX or Java applet with security permissions) might be Google Native Client and the Pepper API, though whether this would be sufficient to access a serial port, I do not know.
另一种选择(除了具有安全权限的 ActiveX 或 Java 小程序)可能是 Google Native Client 和 Pepper API,尽管这是否足以访问串行端口,我不知道。
A Firefox extension can include a native XPCOM component that could access the serial port, and you might find there is already a way to access the serial port from browser chrome (which extensions are) because the security token and smartcard system supports serial readers. A Firefox extension could also be used to deliver the next solution, which requires placing a native component or application on the system.
Firefox 扩展可以包含可以访问串行端口的本机 XPCOM 组件,您可能会发现已经有一种方法可以从浏览器 chrome(哪些扩展)访问串行端口,因为安全令牌和智能卡系统支持串行读取器。Firefox 扩展也可用于提供下一个解决方案,这需要在系统上放置本机组件或应用程序。
Can you tell us what browser(s) and OS('s) are targetted here, and why ActiveX and Java are ruled out?
您能否告诉我们这里针对的是哪些浏览器和操作系统,以及为什么排除 ActiveX 和 Java?
Others have suggested a native running proxy exposing the serial port through some protocol. You could use node for this, or python, or any other language that can create both a serial connection and a socket. To access the proxy from a browser application you would need special security permissions to be granted to the page, and then you can make your proxy an HTTP or WebSocket server. You can also serve javascript from the proxy which would grant the script HTTP and WebSocket access to it's origin server which is the proxy. Google Chrome extensions can access any destination and port with their socket client. Also, I believe a config can be set in Google Chrome to allow this for certain or every page, the same with allowing native client to be used by a webpage.
其他人建议使用本机运行代理通过某种协议公开串行端口。为此,您可以使用 node、python 或任何其他可以创建串行连接和套接字的语言。要从浏览器应用程序访问代理,您需要向页面授予特殊的安全权限,然后您可以将代理设置为 HTTP 或 WebSocket 服务器。您还可以从代理提供 javascript,这将授予脚本 HTTP 和 WebSocket 访问其作为代理的原始服务器的权限。Google Chrome 扩展程序可以使用其套接字客户端访问任何目的地和端口。另外,我相信可以在 Google Chrome 中设置一个配置,以允许特定或每个页面这样做,与允许网页使用本机客户端相同。
Without knowing more about your goal, I can't determine what the best solution would be.
在不了解您的目标的情况下,我无法确定最佳解决方案是什么。