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. 【转】MySql数据库--mysql_real_escape_string()函数

    MySql数据库--mysql_real_escape_string()函数 unsigned long mysql_real_escape_string(MYSQL *mysql, char *to ...

  2. Codeforces 380 简要题解

    ABC见上一篇. 感觉这场比赛很有数学气息. D: 显然必须要贴着之前的人坐下. 首先考虑没有限制的方案数.就是2n - 1(我们把1固定,其他的都只有两种方案,放完后长度为n) 我们发现对于一个限制 ...

  3. HDU-4725 The Shortest Path in Nya Graph 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...

  4. cloudera hbase集群简单思路

    文章copy link:http://cloudera.iteye.com/blog/889468 链接所有者保留所有权! http://www.csdn.net/article/2013-05-10 ...

  5. 第二百三十七天 how can I 坚持

    最近好像迷上看小说了,<灵域>,而且也感觉会看小说了. 话说,今天好冷啊,真怕在路上冻着就冻萌了,寒风赤骨啊. 好想买个帽子.好想让送个帽子. 睡觉.

  6. GUI、模块化与结对编程(homework-03)

    摘要: 在本次作业博客里,我将主要阐述作业3的收获.作业3表面是将之前的程序转换为图形界面(之前程序见http://www.cnblogs.com/shone/p/3348372.html),然而本质 ...

  7. HDU 5676 ztr loves lucky numbers (模拟)

    ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...

  8. HTTP协议状态码详解

    HTTP状态码,我都是现查现用. 我以前记得几个常用的状态码,比如200,302,304,404, 503. 一般来说我也只需要了解这些常用的状态码就可以了.  如果是做AJAX,REST,网络爬虫, ...

  9. 用 JavaScript 修改样式元素

    利用 <style> 元素,我们可以在网页中嵌入样式表.如果需要动态增加 <style> 元素,似乎可以用如下的 JavaScript 代码: var style = docu ...

  10. Connection对象连接加密2

    一般情况下,大多数人习惯于将数据库连接写在web.config上里面,理论上讲,将明文存放在该文件里面是安全的,因为web.config文件是不允许被客户端下载,但一旦该文件泄漏出去,哪怕是很短的时间 ...