【小(xiǎo)編推薦】android操作(z< ✘uò)串口

2017-03-19   |&nbs≠★εp;  發布者:梁國(gu→÷®ó)芳   |  ₽<≤ 查看(kàn):3320次

android
 一(yī)些(xiē)Android 手機(jī)是(shì)有(yǒu)串口可(kě)以提供打開(kāi↑•≈λ),讀(dú)取一(yī)些(xiē)底♥β¥層的(de)數(shù)據。如(rú)果手機( ¥₩↓jī)本身(shēn)沒有(yǒu)串口的(de)話(h•↑uà),那(nà)麽打開(kāi)估計(jì)會♣α≥(huì)報(bào)錯(cuò)。串口讀(≠§dú)取數(shù)據肯定是(shì)使用(yòng)§←☆jni調用(yòng)c代碼來(lái)完成的(de)。有(yǒu)一(yī)個(gè)開(kāi)源的(de)串口庫androi‍₹₹ d-serialport-api。其主頁在這(zhè)裡(£₽≠lǐ)http://code.google.com/↔ p/android-serialport-api/ &nb≠≠•sp;,這(zhè)裡(lǐ)可(kě)以下(xià)到(dào)♣↔&APK及對(duì)源碼。谷歌(gē)的(de)代碼庫,無奈國(guó)內(nèi)無法下(x✔>€ià)載https://github.com/cepr/android-ser☆™✘ialport-api ,GITHUB的(de)地(dì)址,這(zhδ ∏è)個(gè)可(kě)以下(xià)載 但(dàn)是(shì)下(xΩ ià)載源碼之後發現(xiàn)源碼不(bù)能(néng&∑)直接使用(yòng),而且源碼結構較為(w₩• èi)複雜(zá)。

在這(zhè)裡(lǐ)我是(shì)使用(yòng)服務來(lái‌↔>)讀(dú)取串口數(shù)據,都(dōu)出來(lái¶≈₽)數(shù)據直接寫入到(dào)txt文(wén)件(jiàn)™←ε"裡(lǐ)面。

串口讀(dú)寫的(de)庫的(de)地(dì)址:https://code ✘.google.com/p/android-seriβ¥alport-api/

github地(dì)址:https://github.com/cepr/androΩ£id-serialport-api 

本篇博客的(de)下(xià)載地(dì)址:http://dow•₽®©nload.csdn.net/detail/qq★★_16064871/9731908

1,注意事(shì)項

選擇串口時(shí)候,需要(yào)加上(shàng)&₽π'dev/。如(rú)果我選擇ttyMT2,路(lù)徑就(jiù)是©"±(shì)dev/ttyMT2。波特率就(jiù)是(∏$shì)整型38400,或者其他(tā)。

打開(kāi)串口以及讀(dú)取數(shù)據的(de)串口類♣↓"₹調用(yòng)加載so庫一(yī)定放(₽¥fàng)在這(zhè)個(gè)包下(xià)面android_seriaβ♦‍lport_api。不(bù)能(néng)放(fàng)在其他(tā)包的(↕‍←de)下(xià)面。

SerialPortDevice.class

 

[java] view plain copy
 
 在CODE上(shàng)查看(kàn)代碼片派生(shēng)到(dào)我的(de)代碼片
  1.   
  2. package android_serialportδ‌_api;  
  3.   
  4. import java.io.FileDescriptor;  
  5. import java.io.FileIn✔¶πputStream; &nbs→"p;
  6. import java.io.FileOutputStream♦★γ;  
  7. import java.io.InputStream$♦±;  
  8. import java.io.OutputStream;  <∞&✔;
  9.   
  10. import android.util.L×✘‌og;  
  11.   
  12. public class SerialPortDevice{&nbsσ★p; 
  13.   
  14.     private FileDescriptor mFd;  
  15.    &nγ∞↑bsp;private InputStream mInputγ∞★πStream;  
  16.     private OutputStream mOutputStre≈πam;  
  17.       
  18.     private String path;  &✘;
  19.     $ε;private int baudrate;  
  20.    &n€&bsp;int flags=0;  
  21.   
  22.     public SerialPortDevice(String&n∏✘bsp;path, int baudrate, int flags) {  ®€≤;
  23.      &≤π₩®nbsp;  mFd&★≤ ±nbsp;= new FileDescriptor();  
  24.     &n®δbsp;   this.path=path;  ✔✔λ;
  25.       &nb♠₹ φsp; this.baudrate=baudrate;  
  26.      &₹₩nbsp;  this.flags=flags;  
  27.     }&nbs‍"p; 
  28.   
  29.       
  30.     public boolean connect()  {<←  
  31.      &nbs∏∞★↕p;  mFd π♦♦✘= open(path, b∏‌©audrate, 0);  
  32.     ≥♦≠;    if (mFd == null) {  
  33.      &nbΩ  €sp;      ₽₹×;return false;  
  34.       &n≥∏bsp; }  
  35.      &nbs≠☆©p;  else  
  36.       ∞↔Ω;  {  •‌✔×;
  37.      &n‌ λ♦bsp;     £∑γ; return true;  
  38.      &nΩ÷<₩bsp;  }  
  39.     β×¥≈}     
  40.   
  41.     α®;  
  42.    &nbs≈✔p;public InputStream getInputStream()‍↔ {  
  43.      &nbsδ™p;  // TODO Auto-ge♠≥£nerated method stub​✔&§  
  44.    &nbsλ₩$p;    return new FileInputStream(mFd);&nbs≠★>p; 
  45.     ↑‌}  
  46.   
  47.    &nbs☆÷€p;