2

我有一个 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谁能告诉我该怎么做?

4

3 回答 3

6

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

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;

}
于 2014-07-14T12:18:17.807 回答
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

波特率:

* 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
于 2015-05-23T12:21:53.450 回答
3

可以在此处ftdi_sio.hcontrolTransfer()找到您可以使用的功能和 FTDI 芯片的一个很好的概述。

typedef enum {
  ftdi_8U232AM_48MHz_b300 = 0x2710,
  ftdi_8U232AM_48MHz_b600 = 0x1388,
  ftdi_8U232AM_48MHz_b1200 = 0x09c4,
  ftdi_8U232AM_48MHz_b2400 = 0x04e2,
  ftdi_8U232AM_48MHz_b4800 = 0x0271,
  ftdi_8U232AM_48MHz_b9600 = 0x4138,
  ftdi_8U232AM_48MHz_b19200 = 0x809c,
  ftdi_8U232AM_48MHz_b38400 = 0xc04e,
  ftdi_8U232AM_48MHz_b57600 = 0x0034,
  ftdi_8U232AM_48MHz_b115200 = 0x001a,
  ftdi_8U232AM_48MHz_b230400 = 0x000d,
  ftdi_8U232AM_48MHz_b460800 = 0x4006,
  ftdi_8U232AM_48MHz_b921600 = 0x8003,
} FTDI_8U232AM_48MHz_baudrate_t;
于 2015-07-27T16:00:56.527 回答