1.首先下载一个libserial_port.so,新建目录libs/armeabi,将so文件放到该目录下。
2.定义串口类,在类的构建函数中修改权限,打开设备,创建输入流和输出流,通过native接口访问串口打开关闭函数
代码如下:
public class SerialPort {
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException, InvalidParameterException{
//如果串口权限不够,改变权限
if (!device.canRead() || !device.canWrite()) {
try {
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
mFd = open(device.getAbsolutePath(), baudrate, flags);//打开串口
if (mFd == null) {
Log.e(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);//串口输入流
mFileOutputStream = new FileOutputStream(mFd);//串口输出流
}
// Getters and setters
public InputStream getInputStream() {
return mFileInputStream;
}
public OutputStream getOutputStream() {
return mFileOutputStream;
}
// JNI
private native static FileDescriptor open(String path, int baudrate, int flags);//c文件中的串口open()函数
public native void close();//c文件中的串口close()函数
static {
System.loadLibrary("serial_port");//加载串口库
}
}
}
3.定义抽象类ServerData
代码如下:
public abstract class ServerData {
protected SerialPort mSerialPort;
protected OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread;
private class ReadThread extends Thread {
@Override
//在线程中读取数据并处理数据
public void run() {
super.run();
byte[] buffer = new byte[128];
int size;
while(true) {
try {
if (mInputStream == null) return;
size = mInputStream.read(buffer);//读取数据
if (size > 0) {
onDataReceived(buffer, size);//处理数据
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
4.实例化串口类,输出流和输入流,实例化读取线程并开始执行该线程
[code]
public ServerData(String path, int baudrate){
try {
mSerialPort = new SerialPort(new File(path), baudrate, 0);
mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream();
mReadThread = new ReadThread();
mReadThread.start();
} catch (SecurityException e) {
} catch (IOException e) {
} catch (InvalidParameterException e) {
}
}
protected abstract void onDataReceived(final byte[] buffer, final int size);
}
[/code]
5.然后再新建一个类,在新建的类中实现上面的抽象函数,并写一个函数返回读取到的数据。
代码如下:
package View;
//导入R类,所在包不同,不能直接饮用,需要导入才可以使用
import android_serialport_api.sample.R;
public class SerialView {
private Activity context = null;
private Serial mEtcServer = null;
public SerialView(Activity context) {
this.context = context;
}
public void EtcInitView() {
//这样才可以找到android_serialport_api.sample包下的id
TextView mytext=(TextView)context.findViewById(R.id.mytext);
mEtcServer = new Serial("/dev/s3c2410_serial3", 9600);
}
public void EtcRefresh() {
//返回串口线程读取的数据
byte[] buffer = mEtcServer.getdata();
String recString=new String(buffer);//将byte[]的数组转换成字符串string
mytext.setText(recString);//设置字符文本
buffer = null;
}
}