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

ilk*_*gin 2 android arduino baud-rate

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

有一个应用程序以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);
Run Code Online (Sandbox Code Playgroud)

然后在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;
}
Run Code Online (Sandbox Code Playgroud)

有一个开关箱结构可以将波特率设置为9600、14400或19200。但是我希望将其设置为115200

小智 6

你也可以试试这些。这些是从这个链接找到的

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
Run Code Online (Sandbox Code Playgroud)

波特率:

* 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
Run Code Online (Sandbox Code Playgroud)


epi*_*rns 5

这是修改后的函数,可以在上面将您的函数推广为其他波特率:

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;

}
Run Code Online (Sandbox Code Playgroud)