Git Product home page Git Product logo

serialportplus's Introduction

SerialPortPlus

Android串口开发,基于谷歌官方的android-serialport-api增加支持设置奇偶校验、数据位和停止位

使用步骤

第一步:app/build.gradle进行依赖

implementation 'com.azhon:serialport:1.0.1'

第二步:打开串口

  • 1.打开串口
try {
    SerialPortPlus serialPortPlus = new SerialPortPlus("/dev/ttyMT2", 9600);
    //如果你需要设置奇偶校验、数据位和停止位
    //SerialPortPlus serialPortPlus = new SerialPortPlus("/dev/ttyMT2", 9600, 0, 1, 8, 1);
} catch (Exception e) {
    e.printStackTrace();
}
  • 2.设置串口数据回调
serialPortPlus.setReceiveDataListener(this);

第三步:发送数据

serialPortPlus.writeAndFlush(data);

第四步:处理数据

@Override
public void receiveData(ByteBuf byteBuf) {
    //将数据转成十六进制
    String hex = ByteBufUtil.hexDump(byteBuf).toUpperCase();
    //解析数据
    byte[] data = byteBuf.array();
}

这里引入了Netty中的ByteBuf来处理串口数据,如果不懂的小伙伴可以先学习下ByteBuf;使用ByteBuf处理字节数据超乎你想象的舒服

第五步:关闭串口

serialPortPlus.close();

serialportplus's People

Contributors

azhon avatar

Watchers

 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.