手机版
你好,游客 登录 注册
背景:
阅读新闻

Android串口通信实例分析【附源码】

[日期:2012-05-27] 来源:Linux社区  作者:hc260164797 [字体:大 中 小]
最后写了一个类SerialPortActivity.Java实现串口打开和关闭,串口数据通过Input/OutputStrean流来读取和发送,所有继承这个类的Activiy就可以实现串口读取或者发送。
  1. /* 
  2.  * Copyright 2009 Cedric Priscal 
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  *  
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License.  
  15.  */  
  16.   
  17. package com.huangcheng.serial;  
  18.   
  19. import java.io.IOException;  
  20. import java.io.InputStream;  
  21. import java.io.OutputStream;  
  22. import java.security.InvalidParameterException;  
  23.   
  24. import com.huangcheng.zigbeeview.R;  
  25.   
  26. import Android.app.Activity;  
  27. import android.app.AlertDialog;  
  28. import android.content.DialogInterface;  
  29. import android.content.DialogInterface.OnClickListener;  
  30. import android.os.Bundle;  
  31. public abstract class SerialPortActivity extends Activity {  
  32.   
  33.     protected Application mApplication;  
  34.     protected SerialPort mSerialPort;  
  35.     protected OutputStream mOutputStream;  
  36.     private InputStream mInputStream;  
  37.     private ReadThread mReadThread;  
  38.   
  39.     private class ReadThread extends Thread {  
  40.   
  41.         @Override  
  42.         public void run() {  
  43.             super.run();  
  44.             while(!isInterrupted()) {  
  45.                 int size;  
  46.                 try {  
  47.                     byte[] buffer = new byte[64];  
  48.                     if (mInputStream == null) return;  
  49.                     size = mInputStream.read(buffer);  
  50.                     if (size > 0) {  
  51.                         onDataReceived(buffer, size);  
  52.                     }  
  53.                 } catch (IOException e) {  
  54.                     e.printStackTrace();  
  55.                     return;  
  56.                 }  
  57.             }  
  58.         }  
  59.     }  
  60.   
  61.     private void DisplayError(int resourceId) {  
  62.         AlertDialog.Builder b = new AlertDialog.Builder(this);  
  63.         b.setTitle("Error");  
  64.         b.setMessage(resourceId);  
  65.         b.setPositiveButton("OK", new OnClickListener() {  
  66.             public void onClick(DialogInterface dialog, int which) {  
  67.                 SerialPortActivity.this.finish();  
  68.             }  
  69.         });  
  70.         b.show();  
  71.     }  
  72.   
  73.     @Override  
  74.     protected void onCreate(Bundle savedInstanceState) {  
  75.         super.onCreate(savedInstanceState);  
  76.         mApplication = (Application) getApplication();  
  77.         try {  
  78.             mSerialPort = mApplication.getSerialPort();  
  79.             mOutputStream = mSerialPort.getOutputStream();  
  80.             mInputStream = mSerialPort.getInputStream();  
  81.   
  82.             /* Create a receiving thread */  
  83.             mReadThread = new ReadThread();  
  84.             mReadThread.start();  
  85.         } catch (SecurityException e) {  
  86.             DisplayError(R.string.error_security);  
  87.         } catch (IOException e) {  
  88.             DisplayError(R.string.error_unknown);  
  89.         } catch (InvalidParameterException e) {  
  90.             DisplayError(R.string.error_configuration);  
  91.         }  
  92.     }  
  93.   
  94.     protected abstract void onDataReceived(final byte[] buffer, final int size);  
  95.   
  96.     @Override  
  97.     protected void onDestroy() {  
  98.         if (mReadThread != null)  
  99.             mReadThread.interrupt();  
  100.         mApplication.closeSerialPort();  
  101.         mSerialPort = null;  
  102.         super.onDestroy();  
  103.     }  
  104. }  
linux
相关资讯       Android编程入门 
本文评论   查看全部评论 (6)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 6 楼
* 匿名 发表于 2017/1/22 16:34:13
请你们写点有用的。
第 5 楼
* 匿名 发表于 2013/7/15 15:30:14
从未见过如此垃圾的代码
第 4 楼
* 匿名 发表于 2013/4/27 17:32:14
看不懂
第 3 楼
* 匿名 发表于 2012/10/24 10:36:04
这个android-serial-api,纯粹太烂了,很多人浪费了很多时间在上面,但是没法用起,有这个时间去看这个代码,还不如自己去编呢,效率反而高
第 2 楼
* 匿名 发表于 2012/10/24 10:33:07
根本没法用,瞎扯