在 Android 上将 USB 波特率从 9600 更改为 115200 [英] Changing USB Baud Rate from 9600 to 115200 on Android

查看:148
本文介绍了在 Android 上将 USB 波特率从 9600 更改为 115200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Arduino,它以 115200 波特率串行发送数据.

I have an Arduino which sends data serially in 115200 baud rate.

有一个应用程序以 9600 波特率从 Arduino 接收数据.代码是

There is an application that receives data from Arduino in 9600 baud rate. The code is

    // Arduino USB serial converter setup
    // Set control line state
    mUsbConnection.controlTransfer(0x21, 0x22, 0, 0, null, 0, 0);
    // Set line encoding.
    mUsbConnection.controlTransfer(0x21, 0x20, 0, 0, getLineEncoding(9600), 7, 0);
    //mUsbConnection.controlTransfer(0x21, 0x20, 0x001A, 0, getLineEncoding(9600), 7, 0);

然后在getLineEncoding()函数中

private byte[] getLineEncoding(int baudRate) {
    final byte[] lineEncodingRequest = { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };
    switch (baudRate) {
    case 14400:
        lineEncodingRequest[0] = 0x40;
        lineEncodingRequest[1] = 0x38;
        break;

    case 19200:
        lineEncodingRequest[0] = 0x00;
        lineEncodingRequest[1] = 0x4B;
        break;
    }

    return lineEncodingRequest;
}

有一个开关盒结构可以将波特率设置为 9600、14400 或 19200.但我希望它是 115200 谁能告诉我我该怎么做?

There is a switch case stracture for setting the baud rate as 9600, 14400 or 19200. But I want it to be 115200 Can anyone tell me how I can do that?

推荐答案

这是一个修改后的函数,可将上述函数推广到其他波特率:

Here is a modified function that generalizes your function above for other baud rates:

private byte[] getLineEncoding(int baudRate) {
    final byte[] lineEncodingRequest = { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 };
    //Get the least significant byte of baudRate, 
    //and put it in first byte of the array being sent
    lineEncodingRequest[0] = (byte)(baudRate & 0xFF);

    //Get the 2nd byte of baudRate,
    //and put it in second byte of the array being sent
    lineEncodingRequest[1] = (byte)((baudRate >> 8) & 0xFF);

    //ibid, for 3rd byte (my guess, because you need at least 3 bytes
    //to encode your 115200+ settings)
    lineEncodingRequest[2] = (byte)((baudRate >> 16) & 0xFF);

    return lineEncodingRequest;

}

这篇关于在 Android 上将 USB 波特率从 9600 更改为 115200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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