Write data to USB HID using Javascript, HTML5, or any cross platform language (supports Android)

て烟熏妆下的殇ゞ 提交于 2019-12-20 10:35:21

问题


I've written an UI in HTML5 and JavaScript. I chose this implementation so that I could share the same code between both Android Chrome and Windows 8 RT.

Now, my next objective is to write data to a USB HID. The device that I'm communicating with is treated as an HID. I'm mentioning this part in case there's a way to write to an HID device rather than a USB RAW device.

I've done a lot of research on the matter. But, the HTML5 specs are changing so often that maybe I missed something.

Anyways, can someone suggest a language or implementation that I can use to share common code to access a USB device on Android and Windows 8 RT? If there isn't a way to share between the two, how about a suggestion to get USB communication only on Android?


Below is my research along with conclusions:

  • Silverlight: UNSUPPORTED. In terms of web plugin, not supported on Windows 8 RT or Android

  • Flash: Possible option. Supported in both operating systems, although they removed support for Android with release of Jelly Bean

  • HTML5: UNSUPPORTED

    • Looked at the GamePad API, but it seems to only support reading from a gamepad
    • Looked at Device tag, but this tag no longer exists in the current draft. I read that the "navigator" object replaces it in Javascript, but it seems completely different. Also, Device only targets acquiring video and audio
  • JavaScript: UNSUPPORTED. From my understanding, this is because it'd be a security risk.

  • Chrome Packaged App: UNSUPPORTED. Unsure about support in Windows 8 RT. But, it is not supported in Android Chrome, either.

    • A USB API exists that accomplishes exactly what I need, but the API is only available to a packaged app
  • Java Applet: UNSUPPORTED. Unsure about support in Windows 8 RT. But, it is not supported in Android Chrome, either.

    • Many resources available regarding how to communicate with USB devices. However, since java applets aren't supported on smartphones, this ends up being unsupported.
  • A couple of useful posts

    • how to send and receive data to serial port or USB port in asp.net?
    • Is Accessing USB from web application for cross browser cross os possible at all?
    • Is that possible to provide javascript API to control USB, LPT, COM devices?

回答1:


I've actually thought about how to do things similar to this....

Here's is one way sure way to do it if you have control over the computer which has the usb device attached:

Have the computer with the USB device run a web server such as apache/php. Have it only listen to localhost.

Then in the html page being viewed, execute an ajax post to localhost/somescript.php (or cgi or cfm or whatever).

In php/cgi script communicate to the USB device and then return a json string to the browser saying something happened.


UPDATE:


Another approach is to use Custom URL Protocols. You create an executable and "install" it on the client computer along with your custom url protocol.

Then you can invoke the executable from the browser using your custom url protocol.




回答2:


If the usb device you want to control is a barcode printer, you can use jZebra

https://code.google.com/p/jzebra/

This is an applet which directly communicates to locally connected printers.




回答3:


There is one more alternative Web Socket.Create a web socket server which will access the usb device in local system.

Connect your web server using Web Socket from Web Application.Web Socket has standard Api

You can do something like this

  var host = "wss://localhost:25000/test";

  Websokcet ws = new WebSocket(host);

You can create your web sokcet Server using RFC 6455

for older browser you can think of http server as well.




回答4:


Please have a look at JSFS. It works similar to Chris_vr's approach and is already a working solution.

https://github.com/jsfsproject/jsfs. It's free and licensed under GPL.




回答5:


How about this?

Writing to a serial port:

var writeSerial=function(str) {
  chrome.serial.write(connectionId, str2ab(str), onWrite);
}
// Convert string to ArrayBuffer
var str2ab=function(str) {
  var buf=new ArrayBuffer(str.length);
  var bufView=new Uint8Array(buf);
  for (var i=0; i<str.length; i++) {
    bufView[i]=str.charCodeAt(i);
  }
  return buf;
}

more at http://developer.chrome.com/apps/app_hardware.html



来源:https://stackoverflow.com/questions/15535409/write-data-to-usb-hid-using-javascript-html5-or-any-cross-platform-language-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!