如何读取和写入Android中的COM /串行端口的数据? [英] How to read and write data to COM/Serial ports in Android?

查看:1829
本文介绍了如何读取和写入Android中的COM /串行端口的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用android读取和写入设备的COM端口数据,我正在使用javax.comm包但是当我安装apk文件时,它没有显示设备的任何端口所以是否有任何我需要在清单文件中添加的权限?

I have to read and write data to the COM ports of the device using android, I am using javax.comm package for that but when I installed the apk file, it is not displaying any ports of the device so is there any permission which I need to add in the manifest file?

推荐答案

您的问题与操作系统有关。 Android在Linux下运行Linux ,Linux对待串口的方式与Windows不同。 javax.comm 还包含 win32com.dll 驱动程序文件,您将无法在Android设备上安装。如果您确实找到了实现目标的方法,那么您实际上无法在Linux环境中寻找COM端口。串口将使用不同的名称。

Your problem is one with operating systems. Android runs Linux under the hood, and Linux treats serial ports differently than Windows does. javax.comm also contains win32com.dll, a driver file, which you won't be able to install on an Android device. If you do find a way to achieve what you're trying to do, you can't actually look for a "COM" port in a Linux environment. The serial ports will go by different names.

 Windows Com Port   Linux equivalent  
      COM 1           /dev/ttyS0  
      COM 2           /dev/ttyS1
      COM 3           /dev/ttyS2 

因此,假设,如果你的想法是工作,你必须寻找这些名称。

So, hypothetically, if your idea were to work, you have to look for these names.

幸运的是,Android确实有与USB设备连接的规定(我假设你想要连接到,而不是并行或RS-232端口)。为此,您需要将设备设置为 USB Host。以下是您想要做的事情:

Luckily for you, Android does have provisions for interfacing with USB devices (Which I assume you want to connect to, as opposed to parallel or RS-232 ports). To do this, you will set up your device as a USB Host. Here's what you'll want to do:


  1. 获取 USBManager

  2. 查找你的设备。

  3. 获取 USBInterface USBEndpoint

  4. 打开连接。

  5. 传输数据。

  1. Get a USBManager.
  2. Find your device.
  3. Get the USBInterface and USBEndpoint.
  4. Open a connection.
  5. Transfer data.

这是我对你如何做的粗略估计。当然,您的代码将采用更成熟的方式。

Here's my rough estimate of how you'll do it. Your code will, of course, have a more mature way of doing things.

String YOUR_DEVICE_NAME;
byte[] DATA;
int TIMEOUT;

USBManager manager = getApplicationContext().getSystemService(Context.USB_SERVICE);
Map<String, USBDevice> devices = manager.getDeviceList();
USBDevice mDevice = devices.get(YOUR_DEVICE_NAME);
USBDeviceConnection connection = manager.openDevice(mDevice);
USBEndpoint endpoint = device.getInterface(0).getEndpoint(0);

connection.claimInterface(device.getInterface(0), true);
connection.bulkTransfer(endpoint, DATA, DATA.length, TIMEOUT);

额外的阅读材料: http://developer.android.com/guide/topics/connectivity/usb/host.html

这篇关于如何读取和写入Android中的COM /串行端口的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆