安卓 3.1+ USB 作为虚拟 COM 端口
我有一个第三方 USB 设备,当插入 Windows 机器时,它被识别为串行设备并分配给 COM 4 端口。我可以与该设备进行通信,就像与通过串行端口连接的设备进行通信一样。例如,我可以通过 USB 连接将“abc”串行写入设备。
我一直在寻找一种在 Android 中做类似事情的方法。如果我尝试 Usb Host 方法,并使用 UsbManager 打开 UsbDevice,我可以获得一个带有 2 个端点的接口。我尝试过使用 UsbDeviceConnection 中的方法发送控制消息,但该方法对所有内容都返回 -1(尽管我不知道该方法的参数应该使用什么)。
有没有办法获得一个我可以写入的OutputStream,将字节发送到USB设备?现在我正在考虑重新编译内核以包含虚拟 COM 端口驱动程序并编写一些本机代码来执行此操作。
谢谢!
编辑:我正在使用 FTDI 串行到 USB 转换器电路。这与安卓兼容吗?
I have a third party usb device, that when plugged into a Windows machine, is recognized as a serial device and assigned to the COM 4 port. I can communicate with the device just like I would with a device connected via a serial port. For instance, I can write "abc" serially to the device via the USB connection.
I have been searching for a way to do a similar thing in Android. If I try the Usb Host method, and use a UsbManager to open the UsbDevice, I can get one interface, with 2 endpoints. I have tried sending control messages using the method in UsbDeviceConnection, but the method returns -1 for everything (though I don't know what I should use for the parameters of that method).
Is there a way to get an OutputStream that I can write to that will send bytes to the USB device? Right now I am looking at recompiling the kernel to include a virtual COM port driver and write some native code to be able to do this.
Thanks!
Edit: I am using the FTDI serial to USB converter circuit. Is this compatible with Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的设备中的linux内核支持FTDI,您可以像普通COM端口一样访问该设备。它将被命名为
/dev/ttyUSB0
。如果您的内核不支持此类设备,您可以使用 UsbHost 接口编写自己的驱动程序。检查内核的 FTDI 驱动程序源代码以获取指南。
If linux kernel in your device supports FTDI, you can access this device as normal COM port. It will be named something like
/dev/ttyUSB0
.If your kernel does not supports this type of devices, you can write your own driver using UsbHost interfaces. Check kernel's FTDI driver source code for guide.
您可以使用第 3 方驱动程序来实现此目的,
来自 SlickDevLabs 的这个驱动程序非常好用,但要花费 100 美元。
http://slickdevlabs.com/slick-usb-2-serial-library/
You can use 3rd party drivers for this purpose,
this driver from SlickDevLabs works so nice,but cost you 100$.
http://slickdevlabs.com/slick-usb-2-serial-library/
如果您不介意 root 手机并编写一些 C 语言,您可以使用 Android 驱动程序与 Android 中的 FTDI 进行通信:
http://www.ftdichip.com/Drivers/D2XX.htm
那里有一个 libftd2xx-jni.so ,您可以将其与您的 C 代码链接 - 我已经测试过它并且 有用。
If you don't mind rooting your phone and writing some C, you can communicate with the FTDI in Android using the Android driver here:
http://www.ftdichip.com/Drivers/D2XX.htm
There is a libftd2xx-jni.so in there that you can link with your C code - I've tested it and it works.
在常见情况下,强烈不建议生根设备,因为我可以为我的设备执行此操作,但我不能为在 Android 市场上分发的公共应用程序执行此操作。在这种情况下,我认为 controlTransfer() 是处理它的唯一方法。
Rooting device is strongly unrecommended in common case, because i can do this for my device but i can't do this for public app, which is distributed on android market. In that case i think controlTransfer() is the only way to deal with it.