简体   繁体   English

如何使用javascript通过客户端串口进行通信?

[英]how to communicate through serial port on client side using javascript?

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. 我做了一些谷歌搜索,发现带有node-serialport的node.js似乎是javascript方式。 However, in my case, the device is actually connected to the client's machine, and not the server. 但是,就我而言,设备实际上是连接到客户端的计算机,而不是服务器。

My question would be how do i implement node.js in this sense? 我的问题是如何在这个意义上实现node.js? Since the code is run at the client browser, is it possible for me to 'embed' node.js on my webpage? 由于代码是在客户端浏览器上运行的,我是否可以在我的网页上“嵌入”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. 更新:曾设法说服客户从网上加载applet,因此我们将通过applet路由。 Thanks all for your great info! 谢谢大家的好消息! =) =)

JavaScript in the browser only 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 must use some sort of browser extension. 要“突破”浏览器,您必须使用某种浏览器扩展。

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 applet的解决方案: 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

如果要从浏览器访问node.js函数,请尝试app.js。

This can only be done via Active X or a plugin like Java or Flash. 这只能通过Active X或Java或Flash等插件来完成。 Code in JavaScript is only as powerful as the APIs provided to it from the browser. JavaScript中的代码仅与从浏览器提供的API一样强大。

Want to sent an HTTP request to a server? 想要向服务器发送HTTP请求吗? JavaScript can do that (subject to the same-origin policy), because the browser has the XMLHttpRequest API. JavaScript可以做到这一点(取决于同源策略),因为浏览器具有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. 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函数可以从摄像头或麦克风中获取数据,理论上它也可以扩展以从其他设备获取数据。

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. Firefox扩展可以包含可以访问串行端口的本机XPCOM组件,您可能会发现已经有一种方法可以从浏览器chrome访问串行端口(扩展名为),因为安全令牌和智能卡系统支持串行读取器。 A Firefox extension could also be used to deliver the next solution, which requires placing a native component or application on the system. 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. 您可以使用node,或python,或任何其他可以创建串行连接和套接字的语言。 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. 要从浏览器应用程序访问代理,您需要授予页面特殊的安全权限,然后您可以将代理设置为HTTP或WebSocket服务器。 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. 您还可以从代理服务javascript,该代理将授予脚本HTTP和WebSocket访问其作为代理的源服务器。 Google Chrome extensions can access any destination and port with their socket client. Google Chrome扩展程序可以使用其套接字客户端访问任何目标和端口。 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. 另外,我认为可以在Google Chrome中设置配置以允许在某些页面或每个页面上使用此配置,同样允许网页使用本机客户端。

Without knowing more about your goal, I can't determine what the best solution would be. 如果不了解您的目标,我无法确定最佳解决方案。

读取串口数据的另一个选择是在服务器端使用sockjs和sockjs-client-node,在客户端使用sockjs-0.3.js。

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

相关问题 使用客户端 Javascript 与本地网络上的设备通信 - Communicate with devices on local network using client-side Javascript 在Node.js中如何与客户端JavaScript通信? - In Node.js how do I communicate with client side JavaScript? 使用Applet + Javascript从客户端(浏览器)读取串行端口 - Read serial port from client(Browser) using Applet + Javascript 从客户端 Web 浏览器与串行端口通信。 - Communicate with the serial port from client web browser. 如何使用客户端javascript向具有特定端口的服务器发送请求? - How to send a request using client-side javascript to a server with specific port? 如何从客户端的串行端口(RS-232)获取数据 - how to get the data from serial port (RS-232) on client side 如何在客户端JavaScript中检查端口可用性? - How to check port availability in client-side JavaScript? 我的Web应用程序如何与串行端口通信 - How can my web app communicate with serial port 如何使用Silverlight与客户端应用程序进行通信 - How to communicate to a client side application with silverlight 如何使用 AJAX 和 Flask 在 python 服务器和 javascript 客户端之间进行通信? - How to communicate between python server and javascript client using AJAX and Flask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM