当前位置: 移动技术网 > 移动技术>移动开发>Android > Android USB转串口通信开发实例详解

Android USB转串口通信开发实例详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

 android usb转串口通信开发实例详解

好久没有写文章了,年前公司新开了一个项目,是和usb转串口通信相关的,需求是用安卓平板通过usb转接后与好几个外设进行通信,一直忙到最近,才慢慢闲下来,趁着这个周末不忙,记录下usb转串口通信开发的基本流程。

我们开发使用的是usb主机模式,即:安卓平板作为主机,usb外设作为从机进行数据通信。整个开发流程可以总结为以下几点:

1.发现设备

usbmanager usbmanager = (usbmanager) context.getsystemservice(context.usb_service);
map<string, usbdevice> usblist = usbmanager.getdevicelist();

通过usbmanager这个系统提供的类,我们可以枚举出当前连接的所有usb设备,我们主要需要的是usbdevice对象,关于usbdevice这个类,官方是这样注释的:

this class represents a usb device attached to the android device with the android device
 acting as the usb host.

是的,这个类就代表了android所连接的usb设备。

2.打开设备

接下来,我们需要打开刚刚搜索到的usb设备,我们可以将平板与usb外设之间的连接想象成一个通道,只有把通道的门打开后,两边才能进行通信。

一般来说,在没有定制的android设备上首次访问usb设备的时候,默认我们是没有访问权限的,因此我们首先要判断对当前要打开的usbdevice是否有访问权限:

if (!usbmanager.haspermission(usbdevice)) {
    usbpermissionreceiver = new usbpermissionreceiver();
    //申请权限
    intent intent = new intent(action_device_permission);
    pendingintent mpermissionintent = pendingintent.getbroadcast(context, 0, intent, 0);
    intentfilter permissionfilter = new intentfilter(action_device_permission);
    context.registerreceiver(usbpermissionreceiver, permissionfilter);
    usbmanager.requestpermission(usbdevice, mpermissionintent);
    }

这里我们声明一个广播usbpermissionreceiver,当接受到授权成功的广播后做一些其他处理:

 private class usbpermissionreceiver extends broadcastreceiver {
    public void onreceive(context context, intent intent) {
      string action = intent.getaction();
      if (action_device_permission.equals(action)) {
        synchronized (this) {
          usbdevice device = intent.getparcelableextra(usbmanager.extra_device);
          if (device.getdevicename().equals(usbdevice.getdevicename()) {
            if (intent.getbooleanextra(usbmanager.extra_permission_granted, false)) {
             //授权成功,在这里进行打开设备操作
            } else {
             //授权失败
            }
          }
        }
      }
    }
  }

接下来,我们要找到具有数据传输功能的接口usbinterface,从它里边儿找到数据输入和输出端口usbendpoint,一般情况下,一个usbdevice有多个usbinterface,我们需要的一般是第一个,所以:

usbinterface=usbdevice.getinterface(0);

同样的,一个usbinterface有多个usbendpoint,有控制端口和数据端口等,因此我们需要根据类型和数据流向来找到我们需要的数据输入和输出两个端口:

for (int index = 0; index < usbinterface.getendpointcount(); index++) {
        usbendpoint point = usbinterface.getendpoint(index);
        if (point.gettype() == usbconstants.usb_endpoint_xfer_bulk) {
          if (point.getdirection() == usbconstants.usb_dir_in) {
            usbendpointin = point;
          } else if (point.getdirection() == usbconstants.usb_dir_out) {
            usbendpointout = point;
          }
        }
      }

最后,才是真正的打开usb设备,我们需要和usb外设建立一个usbdeviceconnection,它的注释很简介的说明了它的用途:

this class is used for sending and receiving data and control messages to a usb device.

它的获取也很简单,就一句代码:

usbdeviceconnection = usbmanager.opendevice(usbdevice);

到这里,理论上平板和usb外设之间的连接已经建立了,也可以首发数据了,但是,我们大部分情况下还需要对usb串口进行一些配置,比如波特率,停止位,数据控制等,不然两边配置不同,收到的数据会乱码。具体怎么配置,就看你使用的串口芯片是什么了,目前流行的有pl2303,ch340等,由于篇幅问题,需要具体配置串口代码的朋友私信我我发给你。

3.数据传输

到这里,我们已经可以与usb外设进行数据传输了,首先来看怎么向usb设备发送数据。

 1.向usb外设发送数据

在第二步中,我们已经获取了数据的输出端口usbendpointin,我们向外设发送数据就是通过这个端口来实现的。来看怎么用:

int ret = usbdeviceconnection.bulktransfer(usbendpointout, data, data.length, default_timeout);

bulktransfer这个函数用于在给定的端口进行数据传输,第一个参数就是此次传输的端口,这里我们用的输出端口,第二个参数是要发送的数据,类型为字节数组,第三个参数代表要发送的数据长度,最后一个参数是超时,返回值代表发送成功的字节数,如果返回-1,那就是发送失败了。

2.接受usb外设发送来的数据

同理,我们已经找到了数据输入端口usbendpointin,因为数据的输入是不定时的,因此我们可以另开一个线程,来专门接受数据,接受数据的代码如下:

int inmax = inendpoint.getmaxpacketsize(); 
bytebuffer bytebuffer = bytebuffer.allocate(inmax); 
usbrequest usbrequest = new usbrequest(); 
usbrequest.initialize(connection, inendpoint); 
usbrequest.queue(bytebuffer, inmax); 
if(connection.requestwait() == usbrequest){ 
  byte[] retdata = bytebuffer.array(); 
  for(byte byte1 : retdata){ 
    system.err.println(byte1); 
  } 
}

以上,就是usb转串口通信的基本流程,有些地方写的不是很全面,比如接收usb外设数据的方法应该还有别的,不足之处欢迎指正。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网