android 读取串口数据的服务
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 读取串口数据的服务的更多相关文章
- python3 读取串口数据
python3 读取串口数据 demo import serial import time ser = serial.Serial("COM3",115200,timeout = ...
- C#SerialPort如何读取串口数据并显示在TextBox上
SerialPort中串口数据的读取与写入有较大的不同.由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取.一.线程实时读串口:二.事件触发方式实现. 由于线程实时读串口的效率不是十分高 ...
- Qt监控Arduino开关状态(读取串口数据)
setup.ini配置文件内容 [General] #游戏所在主机IP GameIp1=192.168.1.151 GameIp2=192.168.1.152 GameIp3=192.168.1.15 ...
- Android之提交数据到服务端方法简单封装
在Android应用中,除了单机版的应用,其余的应用免不了需要频繁地与服务端进行数据交互,如果每一种方法都独立写一段代码,那会造成代码大量重复,冗余,这不是我们所希望的,所以我们可以对其进行一些封装, ...
- (5)air202读取串口数据并上传到阿里云显示
一.首先进行云端设置 根据串口助手显示的信息,以及模块文档说明我们可以知道 其中red和ir是红光LED的原始数据, HR表示心率值, HRvalid是心率是否有效标识, SP02是血氧数值,,SPO ...
- 一种非常巧妙的读取串口数据的方法--C#
读取不完就一直等待,读完了就立刻走,之前都是设置一个溢出时间,不管是不是早就读取完了都要在这等着,有一定的时间浪费. 注意,用之前要设置好SerialPort类的TimeOut属性:
- Android 读取后台数据并显示。模拟小区车辆管理系统
帮别人做的演示系统,只具有基本的增删查改功能. 核心是android端和后台通过http传输数据 后台是asp.net,数据库是ms sql 2008 android端 private void ge ...
- SerialPort如何读取串口数据并显示在TextBox上,多线程委托
namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...
- android 读取json数据(遍历JSONObject和JSONArray)(转)
public String getJson(){ String jsonString = "{\"FLAG\":\"flag\",\"MES ...
随机推荐
- hdfs 数据块重分布 sbin/start-balancer.sh -threshold
数据块重分布sbin/start-balancer.sh -threshold <percentage of disk capacity>percentage of disk capa ...
- PLSQL 连接Oracle11g (64位)
1.用plsql连不上oracle 11g(64位),先去下载一个oracle 11g(32位客户端) http://www.oracle.com/technetwork/database/featu ...
- [Hive - LanguageManual] Import/Export
LanguageManual ImportExport Skip to end of metadata Added by Carl Steinbach, last edited by Le ...
- SSH与SSL
1. SSL SSH 即Secure Shell,它主要由三部分组成: 第一部分:连接协议 [SSH-CONNECT] 将多个加密隧道分成逻辑通道.它运行在用户认证协议上.它提供了交互式登录话路.远程 ...
- linux下开发c++第二弹--helloworld与makefile
一:hello world sudo vim hello.cpp 输入 g++ -o hello hello.cpp 获得hello 输入 ./hello 运行,显示结果hello world! ...
- Spring Auto proxy creator example
In last Spring AOP examples – advice, pointcut and advisor, you have to manually create a proxy bean ...
- jdk的wsimport方法实现webservice客户端调用服务
1.配置好jdk环境,打开命令行,输入wsimport回车能看到很多该命令的参数, -s:要生成客户端代码的存储路径 -p:对生成的代码从新打包 这两个最常用. 在打开的命令行中输入:wsimport ...
- exit和_exit的区别
参考 http://www.cnblogs.com/hnrainll/archive/2011/08/17/2142001.html p.p1 { margin: 0.0px 0.0px 0.0px ...
- Ecshop 学习之路一 2016年6月30日
以前下载ecshop 都是在ecshop官网上下载,前后台模板都很难看.功能也不太齐全,这次在模板堂下载了ecshop 模板 仿小米的.做一个简单的电商网站. 页面结构还是挺简单的.功能也齐全.用ec ...
- 山东理工大学ACM平台题答案关于C语言 1137 C/C++经典程序训练7---求某个范围内的所有素数
C/C++经典程序训练7---求某个范围内的所有素数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 求小于n的所有素数,按照每行 ...