2016-09-1813:10:03

继承Service,定义抽象方法onDataReceived,子类通过实现抽象方法获取接收到数据的回调。

 package com.zrsoft.liftad.serialport;

 import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import android.app.Service;
import android_serialport_api.SerialPort; import com.zrsoft.liftad.MyApp;
import com.zrsoft.liftad.utils.Logger; public abstract class SerialPortService extends Service { protected SerialPort mSerialPort;
protected OutputStream mOutputStream;
private InputStream mInputStream;
private ReadThread mReadThread; private class ReadThread extends Thread {
byte[] buffer = new byte[128]; @Override
public void run() {
super.run();
while (!isInterrupted()) {
int size;
try { if (mInputStream == null)
return;
size = mInputStream.read(buffer);
if (size > 0) {
onDataReceived(buffer, size);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
} @Override
public void onCreate() {
try {
mSerialPort = new SerialPort(new File("/dev/ttyS3"), 9600, 0);
mOutputStream = mSerialPort.getOutputStream();
mInputStream = mSerialPort.getInputStream(); mReadThread = new ReadThread();
mReadThread.start();
} catch (Exception e) {
e.printStackTrace();
}
} protected abstract void onDataReceived(final byte[] buffer, final int size); @Override
public void onDestroy() {
if (mReadThread != null){
mReadThread.interrupt();
}
if (mSerialPort != null) {
mSerialPort.close();
mSerialPort = null;
}
mSerialPort = null;
super.onDestroy();
}
}
SerialPort 类:
 /*
* Copyright 2009 Cedric Priscal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package android_serialport_api; import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import android.util.Log; public class SerialPort {
private static final String TAG = "SerialPort"; /*
* Do not remove or rename the field mFd: it is used by native method
* close();
*/
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream; public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
try {
System.loadLibrary("serial_port");
} catch (Exception ex) {
Log.d("asdf", ex.getMessage());
}
/* 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();
}
}
Log.d("port", "open ready");
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); public native void close(); static {
System.loadLibrary("serial_port");
}
}
 

android 读取串口数据的服务的更多相关文章

  1. python3 读取串口数据

    python3 读取串口数据 demo import serial import time ser = serial.Serial("COM3",115200,timeout = ...

  2. C#SerialPort如何读取串口数据并显示在TextBox上

    SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现. 由于线程实时读串口的效率不是十分高 ...

  3. Qt监控Arduino开关状态(读取串口数据)

    setup.ini配置文件内容 [General] #游戏所在主机IP GameIp1=192.168.1.151 GameIp2=192.168.1.152 GameIp3=192.168.1.15 ...

  4. Android之提交数据到服务端方法简单封装

    在Android应用中,除了单机版的应用,其余的应用免不了需要频繁地与服务端进行数据交互,如果每一种方法都独立写一段代码,那会造成代码大量重复,冗余,这不是我们所希望的,所以我们可以对其进行一些封装, ...

  5. (5)air202读取串口数据并上传到阿里云显示

    一.首先进行云端设置 根据串口助手显示的信息,以及模块文档说明我们可以知道 其中red和ir是红光LED的原始数据, HR表示心率值, HRvalid是心率是否有效标识, SP02是血氧数值,,SPO ...

  6. 一种非常巧妙的读取串口数据的方法--C#

    读取不完就一直等待,读完了就立刻走,之前都是设置一个溢出时间,不管是不是早就读取完了都要在这等着,有一定的时间浪费. 注意,用之前要设置好SerialPort类的TimeOut属性:

  7. Android 读取后台数据并显示。模拟小区车辆管理系统

    帮别人做的演示系统,只具有基本的增删查改功能. 核心是android端和后台通过http传输数据 后台是asp.net,数据库是ms sql 2008 android端 private void ge ...

  8. SerialPort如何读取串口数据并显示在TextBox上,多线程委托

    namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...

  9. android 读取json数据(遍历JSONObject和JSONArray)(转)

    public String getJson(){ String jsonString = "{\"FLAG\":\"flag\",\"MES ...

随机推荐

  1. [C语言 - 7] 结构体struct

    A. 基本知识   与数组的对比 数组: 构造类型 只能有多个相同类型的数据构成   结构体: 结构体类型 可以由多个不同类型的数据构成   1. 定义类型 struct Student { int ...

  2. linux系统中如何查看日志 (常用命令)

    cat tail -f 日 志 文 件 说 明 /var/log/message 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一 /var/log/secure 与安全相关 ...

  3. Windows Server 2003 R2 64位简体中文版下载

    32位版 CD1: SHA1值:d0dd2782e9387328ebfa45d8804b6850acabf520 ed2k://|file|cn_win_srv_2003_r2_enterprise_ ...

  4. ssi整合报错org.apache.struts2.convention.ConventionsServiceImpl.determineResultPath(ConventionsServiceImpl.java:100)

    java.lang.RuntimeException: Invalid action class configuration that references an unknown class name ...

  5. DotNetZip封装类

      DotnetZip是一个开源类库,支持.NET的任何语言,可很方便的创建,读取,和更新zip文件.而且还可以使用在.NETCompact Framework中. 下载地址在这里: http://d ...

  6. SQLite内存数据库

    [转]SQLite内存数据库 http://www.cnblogs.com/liuyong/archive/2010/09/14/1826152.html SQLite 介绍 一. SQLite 是实 ...

  7. 推荐一个CodeProject上的SlideForm控件

    CodeProject有一篇文章介绍了怎么实现一个SlideForm,非常不错,收藏在此. http://www.codeproject.com/KB/dialog/csslideform.aspx ...

  8. Intel® RealSense™ SDK Architecture

    In this article, we highlight some of the key changes that you can expect to see in the Intel RealSe ...

  9. java foreach编辑讲解

    foreach语句使用总结 foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简化版本,但是foreac ...

  10. NFA与DFA

    正则表达式匹配,包含两个东西,一个是表达式,一个文本. NFA(Nondeterministic Finite Automaton),不确定有穷自动机,表达式主导,NFA去吃文本,贪婪算法吃下去,如果 ...