1.首先下載一個libserial_port.so,建立目錄libs/armeabi,將so檔案放到該目錄下。
2.定義串口類,在類的構建函數中修改許可權,開啟裝置,建立輸入資料流和輸出資料流,通過native介面訪問串口開啟關閉函數
複製代碼 代碼如下:
public class SerialPort {
/*Do not remove or rename the field mFd: it is used by native method close();*/
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException, InvalidParameterException{
//如果串口許可權不夠,改變許可權
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
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();
/* Create a receiving thread */
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;
/* EtcView類,Etc介面管理 */
public class SerialView {
private Activity context = null;
private Serial mEtcServer = null;
/* Etc介面建構函式 */
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;
}
}