我的代码如下:
package com.rx.rxserial;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class RxSerial extends Activity {
static {
System.loadLibrary("rxSerial");
}
String TAG = "RxSerial";
// 发送与接收的字节,暂定义为16字节
int UART_BUFFER_ZISE = 16;
// 指定定波特率
int Baud = 115200;
// 指定读取串口数据间隔时间
int TIME = 500;
// 用于保存打开的设备节点的文件描述符
int com1_fd;
int com4_fd;
// 用于保存接收到的数据
byte[] com1_recvdata = new byte[UART_BUFFER_ZISE];
byte[] com4_recvdata = new byte[UART_BUFFER_ZISE];
String com1RcvdData;
String com4RcvdData;
byte[] com1SendData = new byte[UART_BUFFER_ZISE];
byte[] com4SendData = new byte[UART_BUFFER_ZISE];
/*
* JNI接口函数
*/
static public native int com1DeviceInit(int baud);
static public native int com4DeviceInit(int baud);
static public native void com1SendData(int com1_fd, byte[] com1_buffer);
static public native void com4SendData(int com4_fd, byte[] com4_buffer);
static public native byte[] com1GetData(int com1_fd);
static public native byte[] com4GetData(int com4_fd);
TextView com1DataRcvdTV;
TextView com4DataRcvdTV;
Button com1DataSendBTN;
Button com4DataSendBTN;
Handler com1DataRcvdHandler = new Handler();
Handler com4DataRcvdHandler = new Handler();
Runnable com1DataRcvdRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
com1_recvdata = com1GetData(com1_fd);
com1RcvdData = byte2hex(com1_recvdata);
com1DataRcvdTV.setText(com1RcvdData);
} catch (Exception e) {
// TODO: handle exception
}
com1DataRcvdHandler.postDelayed(this, TIME);
}
};
Runnable com4DataRcvdRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
com4_recvdata = com4GetData(com4_fd);
com4RcvdData = byte2hex(com4_recvdata);
com4DataRcvdTV.setText(com4RcvdData);
} catch (Exception e) {
// TODO: handle exception
}
com4DataRcvdHandler.postDelayed(this, TIME);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rx_serial);
int i;
com1DataRcvdTV = (TextView) findViewById(R.id.com1RcvdDataTv);
com4DataRcvdTV = (TextView) findViewById(R.id.com4RcvdDataTv);
com1DataSendBTN = (Button) findViewById(R.id.com1SendBtn);
com4DataSendBTN = (Button) findViewById(R.id.com4SendBtn);
com1_fd = com1DeviceInit(Baud);
com4_fd = com4DeviceInit(Baud);
for(i = 0; i < UART_BUFFER_ZISE; i++) {
com1SendData[i] = 0x1F;
com4SendData[i] = 0x3C;
}
com1DataRcvdHandler.postDelayed(com1DataRcvdRunnable, TIME);
com4DataRcvdHandler.postDelayed(com4DataRcvdRunnable, TIME);
com1DataSendBTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
com1SendData(com1_fd, com1SendData);
}
});
com4DataSendBTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
com4SendData(com4_fd, com4SendData);
}
});
}
private static String byte2hex(byte[] buffer) {
String h = "";
for (int i = 0; i < buffer.length; i++) {
String temp = Integer.toHexString(buffer[i] & 0xFF);
if (temp.length() == 1) {
temp = "0" + temp;
}
h = h + " " + temp;
}
return h;
}
}
package com.rx.rxserial;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class RxSerial extends Activity {
static {
System.loadLibrary("rxSerial");
}
String TAG = "RxSerial";
// 发送与接收的字节,暂定义为16字节
int UART_BUFFER_ZISE = 16;
// 指定定波特率
int Baud = 115200;
// 指定读取串口数据间隔时间
int TIME = 500;
// 用于保存打开的设备节点的文件描述符
int com1_fd;
int com4_fd;
// 用于保存接收到的数据
byte[] com1_recvdata = new byte[UART_BUFFER_ZISE];
byte[] com4_recvdata = new byte[UART_BUFFER_ZISE];
String com1RcvdData;
String com4RcvdData;
byte[] com1SendData = new byte[UART_BUFFER_ZISE];
byte[] com4SendData = new byte[UART_BUFFER_ZISE];
/*
* JNI接口函数
*/
static public native int com1DeviceInit(int baud);
static public native int com4DeviceInit(int baud);
static public native void com1SendData(int com1_fd, byte[] com1_buffer);
static public native void com4SendData(int com4_fd, byte[] com4_buffer);
static public native byte[] com1GetData(int com1_fd);
static public native byte[] com4GetData(int com4_fd);
TextView com1DataRcvdTV;
TextView com4DataRcvdTV;
Button com1DataSendBTN;
Button com4DataSendBTN;
Handler com1DataRcvdHandler = new Handler();
Handler com4DataRcvdHandler = new Handler();
Runnable com1DataRcvdRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
com1_recvdata = com1GetData(com1_fd);
com1RcvdData = byte2hex(com1_recvdata);
com1DataRcvdTV.setText(com1RcvdData);
} catch (Exception e) {
// TODO: handle exception
}
com1DataRcvdHandler.postDelayed(this, TIME);
}
};
Runnable com4DataRcvdRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
com4_recvdata = com4GetData(com4_fd);
com4RcvdData = byte2hex(com4_recvdata);
com4DataRcvdTV.setText(com4RcvdData);
} catch (Exception e) {
// TODO: handle exception
}
com4DataRcvdHandler.postDelayed(this, TIME);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rx_serial);
int i;
com1DataRcvdTV = (TextView) findViewById(R.id.com1RcvdDataTv);
com4DataRcvdTV = (TextView) findViewById(R.id.com4RcvdDataTv);
com1DataSendBTN = (Button) findViewById(R.id.com1SendBtn);
com4DataSendBTN = (Button) findViewById(R.id.com4SendBtn);
com1_fd = com1DeviceInit(Baud);
com4_fd = com4DeviceInit(Baud);
for(i = 0; i < UART_BUFFER_ZISE; i++) {
com1SendData[i] = 0x1F;
com4SendData[i] = 0x3C;
}
com1DataRcvdHandler.postDelayed(com1DataRcvdRunnable, TIME);
com4DataRcvdHandler.postDelayed(com4DataRcvdRunnable, TIME);
com1DataSendBTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
com1SendData(com1_fd, com1SendData);
}
});
com4DataSendBTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
com4SendData(com4_fd, com4SendData);
}
});
}
private static String byte2hex(byte[] buffer) {
String h = "";
for (int i = 0; i < buffer.length; i++) {
String temp = Integer.toHexString(buffer[i] & 0xFF);
if (temp.length() == 1) {
temp = "0" + temp;
}
h = h + " " + temp;
}
return h;
}
}