Reputation: 301
I have an Arduino which sends data serially in 115200 baud rate.
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);
Then in the getLineEncoding() function
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;
}
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?
Upvotes: 2
Views: 15796
Reputation: 455
A good overview of a what you can do with controlTransfer()
and FTDI-Chips can be found here ftdi_sio.h.
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;
Upvotes: 5
Reputation: 195
You can also try these.These are found from this 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
Baudrates:
* 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
Upvotes: 6
Reputation: 1278
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;
}
Upvotes: 7