简体   繁体   中英

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. 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? Since the code is run at the client browser, is it possible for me to 'embed' node.js on my webpage?

Or is there any other alternative for doing this? Applet and ActiveX are out of the picture though.

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! =)

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.

This is different from node.js, which is a server implementation that has access to all sorts of other file system APIs.

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.

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.

This is how GPSGate works: 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/

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. Code in JavaScript is only as powerful as the APIs provided to it from the browser.

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.

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.

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.

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.

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.

Can you tell us what browser(s) and OS('s) are targetted here, and why ActiveX and Java are ruled out?

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.

Without knowing more about your goal, I can't determine what the best solution would be.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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