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

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

[日期:2012-05-27] 来源:Linux社区  作者:hc260164797 [字体:大 中 小]
Android.mk
  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4.   
  5. LOCAL_MODULE    := serial_port  
  6. LOCAL_SRC_FILES := SerialPort.c  
  7.   
  8. LOCAL_LDLIBS    := -llog  
  9. include $(BUILD_SHARED_LIBRARY)  
然后,直接在目录下ndk-build一下,便可得到我们需要的lib库文件。看看怎么调用吧,首先在SerialPort.Java中实现打开和关闭串口,引用了lib 库
  1. package com.huangcheng.serial;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileDescriptor;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10.   
  11. import android.util.Log;  
  12.   
  13. public class SerialPort {  
  14.     private static final String TAG = "SerialPort";  
  15.   
  16.     /* 
  17.      * Do not remove or rename the field mFd: it is used by native method close(); 
  18.      */  
  19.     private FileDescriptor mFd;  
  20.     private FileInputStream mFileInputStream;  
  21.     private FileOutputStream mFileOutputStream;  
  22.   
  23.     public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {  
  24.   
  25.         /* Check access permission */  
  26.         if (!device.canRead() || !device.canWrite()) {  
  27.             try {  
  28.                 /* Missing read/write permission, trying to chmod the file */  
  29.                 Process su;  
  30.                 su = Runtime.getRuntime().exec("/system/bin/su");  
  31.                 String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"  
  32.                         + "exit\n";  
  33.                 su.getOutputStream().write(cmd.getBytes());  
  34.                 if ((su.waitFor() != 0) || !device.canRead()  
  35.                         || !device.canWrite()) {  
  36.                     throw new SecurityException();  
  37.                 }  
  38.             } catch (Exception e) {  
  39.                 e.printStackTrace();  
  40.                 throw new SecurityException();  
  41.             }  
  42.         }  
  43.   
  44.         mFd = open(device.getAbsolutePath(), baudrate, flags);  
  45.         if (mFd == null) {  
  46.             Log.e(TAG, "native open returns null");  
  47.             throw new IOException();  
  48.         }  
  49.         mFileInputStream = new FileInputStream(mFd);  
  50.         mFileOutputStream = new FileOutputStream(mFd);  
  51.     }  
  52.   
  53.     // Getters and setters   
  54.     public InputStream getInputStream() {  
  55.         return mFileInputStream;  
  56.     }  
  57.   
  58.     public OutputStream getOutputStream() {  
  59.         return mFileOutputStream;  
  60.     }  
  61.   
  62.     // JNI   
  63.     private native static FileDescriptor open(String path, int baudrate, int flags);  
  64.     public native void close();  
  65.     static {  
  66.         System.loadLibrary("serial_port");  
  67.     }  
  68. }  
然后他做了一个很聪明的控制串口打开和关闭的类Application.java,继承自android.app.Application 这样,他便可以在所有的Activity类中调用实现串口打开和关闭。
  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.File;  
  20. import java.io.IOException;  
  21. import java.security.InvalidParameterException;  
  22.   
  23. public class Application extends android.app.Application {  
  24.   
  25.     private SerialPort mSerialPort = null;  
  26.   
  27.     public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException {  
  28.         if (mSerialPort == null) {  
  29.             /* Open the serial port */  
  30.             mSerialPort = new SerialPort(new File("/dev/ttyUSB0"),19200, 0);  
  31.         }  
  32.         return mSerialPort;  
  33.     }  
  34.   
  35.     public void closeSerialPort() {  
  36.         if (mSerialPort != null) {  
  37.             mSerialPort.close();  
  38.             mSerialPort = null;  
  39.         }  
  40.     }  
  41. }  
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
根本没法用,瞎扯