简体   繁体   English

使用 JavaScript、HTML5 或任何跨平台语言(支持 Android)将数据写入 USB HID

[英]Write data to USB HID using JavaScript, HTML5, or any cross platform language (supports Android)

I've written an UI in HTML5 and JavaScript.我用 HTML5 和 JavaScript 编写了一个 UI。 I chose this implementation so that I could share the same code between both Android Chrome and Windows 8 RT.我选择了这个实现,以便我可以在 Android Chrome 和Windows 8 RT 之间共享相同的代码。

Now, my next objective is to write data to a USB HID.现在,我的下一个目标是将数据写入 USB HID。 The device that I'm communicating with is treated as an HID.我正在与之通信的设备被视为 HID。 I'm mentioning this part in case there's a way to write to an HID device rather than a USB RAW device.我提到这部分是为了防止有一种写入 HID 设备而不是 USB RAW设备的方法。

I've done a lot of research on the matter.我对这个问题做了很多研究。 But, the HTML5 specifications are changing so often that maybe I missed something.但是,HTML5 规范变化如此频繁,以至于我可能错过了一些东西。

**Anyway, is there a language or implementation that I can use to share common code to access a USB device on Android and Windows 8 RT? **无论如何,是否有一种语言或实现可用于共享通用代码以访问 Android 和 Windows 8 RT 上的 USB 设备?

If there isn't a way to share between the two, how about a suggestion to get USB communication only on Android?**如果两者之间无法共享,那么建议仅在 Android 上进行 USB 通信如何?**


Below is my research along with conclusions:以下是我的研究和结论:

  • Silverlight: unsupported . Silverlight:不受支持 In terms of web plugins, it is not supported on Windows 8 RT or Android在网络插件方面,Windows 8 RT 或 Android 不支持

  • Flash: Possible option.闪光:可能的选择。 It is supported in both operating systems, although they removed support for Android with release of Android 4.1 (Jelly Bean).两种操作系统都支持它,尽管它们在发布Android 4.1 (Jelly Bean) 时取消了对 Android 的支持。

  • HTML5: unsupported HTML5:不支持

  • I looked at the GamePad API, but it seems to only support reading from a gamepad.我查看了 GamePad API,但它似乎只支持从游戏手柄读取。

  • I 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.我读到“导航器”对象在 JavaScript 中替换了它,但它似乎完全不同。 Also, Device only targets acquiring video and audio此外,设备仅针对获取视频和音频

  • JavaScript: unsupported . JavaScript:不受支持 From my understanding, this is because it'd be a security risk.根据我的理解,这是因为它会存在安全风险。

  • Chrome Packaged App: unsupported . Chrome 打包应用程序:不受支持 I am unsure about support in Windows 8 RT.我不确定是否支持 Windows 8 RT。 But, it is not supported in Android Chrome, either.但是,Android Chrome 也不支持它。

  • A USB API exists that accomplishes exactly what I need, but the API is only available to a packaged app.存在一个USB API可以完全满足我的需要,但该 API 仅适用于打包的应用程序。

  • Java applet: unsupported . Java 小程序:不受支持 I am unsure about support in Windows 8 RT.我不确定是否支持 Windows 8 RT。 But, it is not supported in Android Chrome, either.但是,Android Chrome 也不支持它。

  • There are many resources available regarding how to communicate with USB devices.关于如何与 USB 设备通信,有许多可用资源。 However, since Java applets aren't supported on smartphones, this ends up being unsupported.但是,由于智能手机不支持 Java 小程序,因此最终不受支持。

  • A couple of useful posts几个有用的帖子

  • How can I send and receive data to a serial port or USB port in ASP.NET?如何在 ASP.NET 中向串行端口或 USB 端口发送和接收数据?

  • Is accessing USB from a web application for cross browser cross OS possible at all? 是否可以从跨浏览器跨操作系统的 Web 应用程序访问 USB?

  • Is that possible to provide a JavaScript API to control USB, LPT, and COM devices?是否可以提供 JavaScript API 来控制 USB、LPT 和 COM 设备?

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:如果您可以控制连接了 USB 设备的计算机,这是一种可靠的方法:

Have the computer with the USB device run a web server such as Apache and PHP.让带有 USB 设备的计算机运行 Web 服务器,例如 Apache 和 PHP。 Have it only listen to localhost .让它只听localhost

Then in the HTML page being viewed, execute an Ajax post to localhost/somescript.php (or CGI or cfm or whatever).然后在正在查看的 HTML 页面中,执行到 localhost/somescript.php(或CGI或 cfm 或其他)的 Ajax 帖子。

In the PHP/CGI script, communicate to the USB device and then return a JSON string to the browser saying something happened.在 PHP/CGI 脚本中,与 USB 设备通信,然后向浏览器返回一个 JSON 字符串,说明发生了一些事情。

Another approach is to use custom URL protocols.另一种方法是使用自定义 URL 协议。 You create an executable and "install" it on the client computer along with your custom URL protocol.您创建一个可执行文件并将它与您的自定义 URL 协议一起“安装”在客户端计算机上。

Then you can invoke the executable from the browser using your custom URL protocol.然后,您可以使用自定义 URL 协议从浏览器调用可执行文件。

If the USB device you want to control is a barcode printer, you can use jZebra .如果您要控制的 USB 设备是条码打印机,您可以使用jZebra

This is an applet which directly communicates to locally connected printers.这是一个直接与本地连接的打印机通信的小程序。

There is one more alternative Web Socket.Create a web socket server which will access the usb device in local system.还有一个替代的 Web Socket。创建一个 Web 套接字服务器,它将访问本地系统中的 USB 设备。

Connect your web server using Web Socket from Web Application.Web Socket has standard Api使用 Web 应用程序中的 Web Socket 连接您的 Web 服务器。Web Socket 具有标准 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您可以使用 RFC 6455创建您的 web sokcet 服务器

for older browser you can think of http server as well.对于较旧的浏览器,您也可以考虑 http 服务器。

Please have a look at JSFS.请看看JSFS。 It works similar to Chris_vr's approach and is already a working solution.它的工作原理类似于 Chris_vr 的方法,并且已经是一个可行的解决方案。

https://github.com/jsfsproject/jsfs . https://github.com/jsfsproject/jsfs It's free and licensed under GPL.它是免费的,并在 GPL 下获得许可。

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 is at http://developer.chrome.com/apps/app_hardware.html .更多信息在http://developer.chrome.com/apps/app_hardware.html

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

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