Code Monkey home page Code Monkey logo

android-serialport's Introduction

Android-Serialport

移植谷歌官方串口库android-serialport-api,仅支持串口名称及波特率,该项目添加支持校验位、数据位、停止位、流控配置项

Android-Serialport

GitHub forksGitHub issuesGitHub starsSource persentJcenter2.1Maven CentralDemo apk download AppVeyor branchGitHub license

文档

使用依赖

  1. 在项目根目录的build.gradle文件中添加:
allprojects {
    repositories {
        ...
        mavenCentral()
    }
}
  1. 在项目Module下的build.gradle文件中添加:
dependencies {
    implementation 'io.github.xmaihh:serialport:2.1.1'
}

属性支持

属性 参数
波特率 BAUDRATE
数据位 5,6,7,8 ;默认值8
校验位 无奇偶校验(NONE), 奇校验(ODD), 偶校验(EVEN); 默认无奇偶校验
停止位 1,2 ;默认值1
流控 不使用流控(NONE), 硬件流控(RTS/CTS), 软件流控(XON/XOFF); 默认不使用流控

代码功能

1.列出串口列表

serialPortFinder.getAllDevicesPath();

2.串口属性设置

serialHelper.setPort(String sPort);      //设置串口
serialHelper.setBaudRate(int iBaud);     //设置波特率
serialHelper.setStopBits(int stopBits);  //设置停止位
serialHelper.setDataBits(int dataBits);  //设置数据位
serialHelper.setParity(int parity);      //设置校验位
serialHelper.setFlowCon(int flowcon);    //设置流控

串口属性设置需在执行open()函数之前才能设置生效

3.打开串口

serialHelper.open();

4.关闭串口

serialHelper.close();

5.发送

serialHelper.send(byte[] bOutArray); // 发送byte[]
serialHelper.sendHex(String sHex);  // 发送Hex
serialHelper.sendTxt(String sTxt);  // 发送ASCII

6.接收

 @Override
protected void onDataReceived(final ComBean comBean) {
       Toast.makeText(getBaseContext(), new String(comBean.bRec, "UTF-8"), Toast.LENGTH_SHORT).show();
   }

7.粘包处理

支持粘包处理,原因见issues#1,提供的粘包处理有

  1. 不处理(默认)
  2. 首尾特殊字符处理
  3. 固定长度处理
  4. 动态长度处理 支持自定义粘包处理,第一步实现AbsStickPackageHelper接口
/**
 * 接受消息,粘包处理的helper,通过inputstream,返回最终的数据,需手动处理粘包,返回的byte[]是我们预期的完整数据
 * note:这个方法会反复调用,直到解析到一条完整的数据。该方法是同步的,尽量不要做耗时操作,否则会阻塞读取数据
 */
public interface AbsStickPackageHelper {
    byte[] execute(InputStream is);
}

设置粘包处理

serialHelper.setStickPackageHelper(AbsStickPackageHelper mStickPackageHelper);
  • 其实数据粘包可参考socket通讯的粘包处理,例如此处粘包处理方法出自于XAndroidSocket

完整Demo地址

演示效果

apk下载

PC端调试工具 友善串口调试工具

更新日志

修复

新增

  • 添加支持设置接收数据粘包处理,支持设置自定义粘包处理

新增

  • 添加支持设置校验位、数据位、停止位、流控配置项

新增

  • 基础功能,串口设置串口号、波特率,发送、接收数据

FAQ

  • 此library不提供ROOT权限,请自行打开串口666权限
adb shell  chmod 666 /dev/ttyS1

android-serialport's People

Contributors

xmaihh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.