本文介绍了在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!