在Android上将USB波特率从9600更改为115200

时间:2014-07-14 12:00:00

标签: android arduino baud-rate

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

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

    // 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 有谁能告诉我如何做到这一点?

3 个答案:

答案 0 :(得分:4)

这是一个修改过的函数,它将上面的函数概括为其他波特率:

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;

}

答案 1 :(得分:3)

你也可以尝试这些。这些是从link

中找到的
conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
                    conn.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
                    conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
                    conn.controlTransfer(0x40, 0x03, 0x001A, 0, null, 0, 0);//Baud rate 115200

波特率:

* 0x2710 ----------------- 300
* 0x1388 ----------------- 600
* 0x09C4 ----------------- 1200
* 0x04E2 ----------------- 2400
* 0x0271 ----------------- 4800
* 0x4138 ----------------- 9600
* 0x809C ----------------- 19200
* 0xC04E ----------------- 38400
* 0x0034 ----------------- 57600
* 0x001A ----------------- 115200
* 0x000D ----------------- 230400
* 0x4006 ----------------- 460800
* 0x8003 ----------------- 921600

答案 2 :(得分:2)

可以在此处ftdi_sio.h找到有关使用RunStarted和FTDI-Chips可以执行的操作的完整概述。

controlTransfer()