国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android串口操作方法實例

2020-04-11 11:36:12
字體:
供稿:網(wǎng)友

1.首先下載一個libserial_port.so,新建目錄libs/armeabi,將so文件放到該目錄下。
2.定義串口類,在類的構(gòu)建函數(shù)中修改權(quán)限,打開設(shè)備,創(chuàng)建輸入流和輸出流,通過native接口訪問串口打開關(guān)閉函數(shù)

復制代碼 代碼如下:

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{
//如果串口權(quán)限不夠,改變權(quán)限
  /* 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()函數(shù)
 public native void close();//c文件中的串口close()函數(shù)
 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
  //在線程中讀取數(shù)據(jù)并處理數(shù)據(jù)
  public void run() {
   super.run();
   byte[] buffer = new byte[128];
   int size;
   while(true) {
    try {
     if (mInputStream == null) return;
     size = mInputStream.read(buffer);//讀取數(shù)據(jù)
     if (size > 0) {
      onDataReceived(buffer, size);//處理數(shù)據(jù)
     }
    } catch (IOException e) {
     e.printStackTrace();
     return;
    }
   }
  }
 }
4.實例化串口類,輸出流和輸入流,實例化讀取線程并開始執(zhí)行該線程
[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.然后再新建一個類,在新建的類中實現(xiàn)上面的抽象函數(shù),并寫一個函數(shù)返回讀取到的數(shù)據(jù)。
復制代碼 代碼如下:

package View;
//導入R類,所在包不同,不能直接飲用,需要導入才可以使用
import android_serialport_api.sample.R;
/* EtcView類,Etc界面管理 */
public class SerialView {
 private Activity context = null;
 private Serial mEtcServer = null;
 /* Etc界面構(gòu)造函數(shù) */
 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() {
  //返回串口線程讀取的數(shù)據(jù)
  byte[] buffer = mEtcServer.getdata();
  String recString=new String(buffer);//將byte[]的數(shù)組轉(zhuǎn)換成字符串string
  mytext.setText(recString);//設(shè)置字符文本
  buffer = null;
 }
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉禾县| 时尚| 西华县| 罗山县| 尖扎县| 洛浦县| 绥宁县| 华蓥市| 望江县| 九寨沟县| 依安县| 新营市| 分宜县| 平塘县| 巴楚县| 康马县| 昂仁县| 灌阳县| 营口市| 景谷| 剑河县| 南丰县| 康平县| 西宁市| 邹城市| 静海县| 陆川县| 洛浦县| 西乌| 称多县| 扎囊县| 灵石县| 诏安县| 芦溪县| 建宁县| 清河县| 乌苏市| 宁武县| 安达市| 新沂市| 建始县|