android 读取串口数据的服务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android 读取串口数据的服务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4334字,纯文字阅读大概需要7分钟。
内容图文
![android 读取串口数据的服务](/upload/InfoBanner/zyjiaocheng/1123/12df648ea8b24561b2194679be9677de.jpg)
2016-09-1813:10:03
继承Service,定义抽象方法onDataReceived,子类通过实现抽象方法获取接收到数据的回调。
1 package com.zrsoft.liftad.serialport; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 8 import android.app.Service; 9 import android_serialport_api.SerialPort; 10 11 import com.zrsoft.liftad.MyApp; 12 import com.zrsoft.liftad.utils.Logger; 13 14 public abstract class SerialPortService extends Service { 1517protected SerialPort mSerialPort; 18protected OutputStream mOutputStream; 19private InputStream mInputStream; 20private ReadThread mReadThread; 2122privateclass ReadThread extends Thread { 23byte[] buffer = newbyte[128]; 2425 @Override 26publicvoid run() { 27super.run(); 28while (!isInterrupted()) { 29int size; 30try { 3132if (mInputStream == null) 33return; 34 size = mInputStream.read(buffer); 35if (size > 0) { 36 onDataReceived(buffer, size); 38 } 39 } catch (IOException e) { 40 e.printStackTrace(); 41return; 42 } 43 } 44 } 45 } 4647 @Override 48publicvoid onCreate() { 50try { 52 mSerialPort = new SerialPort(new File("/dev/ttyS3"), 9600, 0); 53 mOutputStream = mSerialPort.getOutputStream(); 54 mInputStream = mSerialPort.getInputStream(); 5556 mReadThread = new ReadThread(); 57 mReadThread.start(); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 } 6263protectedabstractvoid onDataReceived(finalbyte[] buffer, finalint size); 6465 @Override 66publicvoid onDestroy() { 67if (mReadThread != null){ 68 mReadThread.interrupt(); 69 } 70if (mSerialPort != null) { 71 mSerialPort.close(); 72 mSerialPort = null; 73 } 75 mSerialPort = null; 76super.onDestroy(); 77 } 78 }
SerialPort 类:
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 android_serialport_api; 18 19 import java.io.File; 20 import java.io.FileDescriptor; 21 import java.io.FileInputStream; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.io.OutputStream; 26 27 import android.util.Log; 28 29 public class SerialPort { 30 private static final String TAG = "SerialPort"; 3132/*33 * Do not remove or rename the field mFd: it is used by native method 34 * close(); 35*/36private FileDescriptor mFd; 37private FileInputStream mFileInputStream; 38private FileOutputStream mFileOutputStream; 3940public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException { 41try { 42 System.loadLibrary("serial_port"); 43 } catch (Exception ex) { 44 Log.d("asdf", ex.getMessage()); 45 } 46/* Check access permission */47if (!device.canRead() || !device.canWrite()) { 48try { 49/* Missing read/write permission, trying to chmod the file */50 Process su; 51 su = Runtime.getRuntime().exec("/system/bin/su"); 52 String cmd = "chmod 666 " + device.getAbsolutePath() + "\n" + "exit\n"; 53 su.getOutputStream().write(cmd.getBytes()); 54if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) { 55thrownew SecurityException(); 56 } 57 } catch (Exception e) { 58 e.printStackTrace(); 59thrownew SecurityException(); 60 } 61 } 62 Log.d("port", "open ready"); 63 mFd = open(device.getAbsolutePath(), baudrate, flags); 64if (mFd == null) { 65 Log.e(TAG, "native open returns null"); 66thrownew IOException(); 67 } 68 mFileInputStream = new FileInputStream(mFd); 69 mFileOutputStream = new FileOutputStream(mFd); 70 } 7172// Getters and setters73public InputStream getInputStream() { 74return mFileInputStream; 75 } 7677public OutputStream getOutputStream() { 78return mFileOutputStream; 79 } 8081// JNI82privatenativestatic FileDescriptor open(String path, int baudrate, int flags); 8384publicnativevoid close(); 8586static { 87 System.loadLibrary("serial_port"); 88 } 89 }
原文:http://www.cnblogs.com/slz-bky/p/5881312.html
内容总结
以上是互联网集市为您收集整理的android 读取串口数据的服务全部内容,希望文章能够帮你解决android 读取串口数据的服务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。