我是android的新手。我正在开发一个Android和PC之间的串行通信应用程序,我在这里做了一些代码,用于将一些数据从android连续发送到PC。但它正在使用19200的波特率,但我想要115200.我曾经把波特率控制转移的速率值,但它不适用于我请帮助我如何使用115200波特率从android到PC串行发送数据
这就是我用波特率115200所做的,但它适用于19200:
package com.medeQuip.communication;
import java.nio.ByteBuffer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements Runnable{
private static final String TAG = "MedeQuip Demo Communication";
private static final byte IGNORE_00 = (byte) 0x00;
ToggleButton buttonLed;
EditText textOut;
Button buttonSend;
TextView textIn;
String stringToRx;
private UsbManager usbManager;
private UsbDevice deviceFound;
private UsbDeviceConnection usbDeviceConnection;
private UsbInterface usbInterfaceFound = null;
private UsbEndpoint endpointOut = null;
private UsbEndpoint endpointIn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textOut = (EditText)findViewById(R.id.textout);
textIn = (TextView)findViewById(R.id.textin);
buttonSend = (Button)findViewById(R.id.send);
buttonSend.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
final String textToSend = textOut.getText().toString();
if(textToSend!=""){
stringToRx = "";
textIn.setText("");
Thread threadsendArduinoText =
new Thread(new Runnable(){
@Override
public void run() {
sendArduinoText(textToSend);
}});
threadsendArduinoText.start();
}
}});
usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
}
@Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
String action = intent.getAction();
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
setDevice(device);
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
if (deviceFound != null && deviceFound.equals(device)) {
setDevice(null);
}
}
}
private void setDevice(UsbDevice device) {
usbInterfaceFound = null;
endpointOut = null;
endpointIn = null;
for (int i = 0; i < device.getInterfaceCount(); i++) {
UsbInterface usbif = device.getInterface(i);
UsbEndpoint tOut = null;
UsbEndpoint tIn = null;
int tEndpointCnt = usbif.getEndpointCount();
if (tEndpointCnt >= 2) {
for (int j = 0; j < tEndpointCnt; j++) {
if (usbif.getEndpoint(j).getType()
== UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (usbif.getEndpoint(j).getDirection()
== UsbConstants.USB_DIR_OUT) {
tOut = usbif.getEndpoint(j);
} else if (usbif.getEndpoint(j).getDirection()
== UsbConstants.USB_DIR_IN) {
tIn = usbif.getEndpoint(j);
}
}
}
if (tOut != null && tIn != null) {
// This interface have both USB_DIR_OUT
// and USB_DIR_IN of USB_ENDPOINT_XFER_BULK
usbInterfaceFound = usbif;
endpointOut = tOut;
endpointIn = tIn;
}
}
}
if (usbInterfaceFound == null) {
return;
}
deviceFound = device;
if (device != null) {
UsbDeviceConnection connection =
usbManager.openDevice(device);
if (connection != null &&
connection.claimInterface(usbInterfaceFound, true)) {
connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
connection.controlTransfer(0x21, 32, 0x001A, 0,
new byte[] { (byte) 0x80, 0x25, 0x00,
0x00, 0x00, 0x00, 0x08 },
7, 0);
usbDeviceConnection = connection;
Thread thread = new Thread(this);
thread.start();
} else {
usbDeviceConnection = null;
}
}
}
@SuppressWarnings("unused")
private void sendArduinoCommand(int control) {
synchronized (this) {
if (usbDeviceConnection != null) {
/*String text = textOut.getText().toString();
byte[] b = new byte[text.length()];
b = text.getBytes();
for(int i=0; i< b.length; i++){
//b[0] = message[i];
//Log.d(TAG, "sendArduinoTextb[0]: " + b[0]);
usbDeviceConnection.bulkTransfer(endpointOut,b, b.length, 0);
}*/
}
}
}
private void sendArduinoText(String s) {
synchronized (this) {
if (usbDeviceConnection != null) {
Log.d(TAG, "sendArduinoText: " + s);
/*
usbDeviceConnection.bulkTransfer(endpointOut,
message, message.length, 0);
*/
String text = textOut.getText().toString();
byte[] b = new byte[text.length()];
b = text.getBytes();
// for(int i=0; i< b.length; i++){
//b[0] = message[i];
//Log.d(TAG, "sendArduinoTextb[0]: " + b[0]);
usbDeviceConnection.bulkTransfer(endpointOut,b, b.length, 0);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// }
}
}
}
@Override
public void run() {
ByteBuffer buffer = ByteBuffer.allocate(1);
UsbRequest request = new UsbRequest();
request.initialize(usbDeviceConnection, endpointIn);
while (true) {
request.queue(buffer, 1);
if (usbDeviceConnection.requestWait() == request) {
byte dataRx = buffer.get(0);
Log.d(TAG, "dataRx: " + dataRx);
if(dataRx!=IGNORE_00){
stringToRx += (char)dataRx;
runOnUiThread(new Runnable(){
@Override
public void run() {
textIn.setText(stringToRx);
}});
}
} else {
break;
}
}
}
}