How to communicate via serial port on client side using javascript?

Recently I received a request to add communication to a device connected via a serial port on a client machine through my web page.

I did some searches and found that node.js with node -serialport seems to be a javascript way. However, in my case, the device is actually connected to the client machine, and not to the server.

The question is how to implement node.js in this sense? Since the code runs in the client browser, is it possible for me to "embed" node.js in my web page?

Or is there another alternative for this? Applet and ActiveX are not true.

thanks

UPDATE: We were able to convince the client that the applet was downloaded from the Internet, so we will go through the applet’s route. Thank you all for your wonderful information! =)

+6
source share
6 answers

JavaScript in the browser has only access to the APIs provided by the browser: it lives in the browser sandbox, where it (by right) does not have access to the client file system or other equipment.

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

To “break out” of the browser, you must use some kind of browser extension.

+6
source

You will need to create a plug-in, applet or application on the client side in order to receive data in the client’s web browser before sending it to your server.

You can create a small application that reads the serial port of the client machine that creates the .js file, and then your web page includes this src of this “dynamically” created js file on the client machine, and your preliminary web page accesses the serial the port in a roundabout manner.

This is how GPSGate works: http://gpsgate.com/developer/gps_in_browser/

See also here: How to read from a serial port on a web page

And a solution based on Java applets: http://code.google.com/p/java-simple-serial-connector/

http://code.google.com/p/java-simple-serial-connector/wiki/jSSC_Terminal

+3
source

Try app.js if you want to access node.js functions from a browser.

+1
source

This can only be done through Active X or a plugin such as Java or Flash. JavaScript code is just as efficient as the APIs provided to it from a browser.

Want to send an HTTP request to the server? JavaScript can do this (according to policies of the same origin) because the browser has an XMLHttpRequest API. Want to ask what processes are running on your operating system right now? JavaScript cannot do this, because no browser provides an API to ask the OS which processes are running.

Not a single browser that I know implements any JavaScript API for serial port operations, so there is no way to do this without using a plugin.

However, this does not exclude the possibility of an API that ever exists: the getUserMedia function can capture data from a camera or microphone, and it could theoretically be expanded to receive data from other devices.

0
source

Another option (besides ActiveX or a Java applet with security permissions) could be the Google Native Client and the Pepper API, although this would be enough to access the serial port, I don't know.

The Firefox extension may include its own XPCOM component, which can access the serial port, and you may find that there is already access to the serial port from the chrome browser (which extensions), since the security token and smart card support serial readers. You can also use the Firefox extension to deliver the next solution, which requires you to place your own component or application on the system.

Can you tell us which browsers and OS are targeted here, and why are ActiveX and Java excluded?

Others have proposed their own proxy server, which provides a serial port through some protocol. You can use node for this, or python, or any other language that can create both a serial connection and a socket. To access the proxy server from a browser application, you need special security permissions for the page, and then you can make your proxy server an HTTP or WebSocket server. You can also use javascript from a proxy server, which will provide an HTTP script and WebSocket access to its source server, which is a proxy server. Google Chrome extensions can access any destination and port using their socket client. In addition, I believe that in Google Chrome you can configure the config to allow this for a specific or each page, the same thing that allows you to use your own client on a web page.

Without knowing more about your goal, I cannot determine what the best solution will be.

0
source

Another option for reading serial port data is to use sockjs and sockjs-client-node on the server side and sockjs-0.3.js on the client side.

0
source

Source: https://habr.com/ru/post/921450/


All Articles