USB开发中设置波特率

最近在做Android设备与外设通信,需要用的串口通信。如果使用android-serialport-api发现Android设备需要获取root权限,否则会报权限不足的异常。所以决定使用Android提供的USB开发。

使用方法网上一搜有一堆。不做具体的描述,但是关于波特率的设置却很少,自己就碰到了这个问题。各种百度谷歌发现一个可以解决的办法。
在获取到mDeviceConnection对象之后调用configUSB(paramInt);参数为需要设置的波特率
private boolean configUsb(int paramInt) {
    byte[] arrayOfByte = new byte[8];
    mDeviceConnection.controlTransfer(192, 95, 0, 0, arrayOfByte, 8, 1000);
    mDeviceConnection.controlTransfer(64, 161, 0, 0, null, 0, 1000);
    long l1 = 1532620800 / paramInt;
    for (int i = 3; ; i--) {
        if ((l1 <= 65520L) || (i <= 0)) {
            long l2 = 65536L - l1;
            int j = (short) (int) (0xFF00 & l2 | i);
            int k = (short) (int) (0xFF & l2);
            mDeviceConnection.controlTransfer(64, 154, 4882, j, null, 0, 1000);
            mDeviceConnection.controlTransfer(64, 154, 3884, k, null, 0, 1000);
            mDeviceConnection.controlTransfer(192, 149, 9496, 0, arrayOfByte, 8, 1000);
            mDeviceConnection.controlTransfer(64, 154, 1304, 80, null, 0, 1000);
            mDeviceConnection.controlTransfer(64, 161, 20511, 55562, null, 0, 1000);
            mDeviceConnection.controlTransfer(64, 154, 4882, j, null, 0, 1000);
            mDeviceConnection.controlTransfer(64, 154, 3884, k, null, 0, 1000);
            mDeviceConnection.controlTransfer(64, 164, 0, 0, null, 0, 1000);
            return true;
        }
        l1 >>= 3;
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值