Giter VIP home page Giter VIP logo

ez-serialport's Introduction

EZ-SerialPort

EZ-SerialPort是基于Google android-serialport-api 修改与扩展的Android串口操作库。

EZ-SerialPort简化了串口的配置与读写操作,支持了多串口通道的操作,支持了多页面串口数据的同时读取功能。

Demo效果如下:

screenshot

Gradle 依赖

在app的build.gradle 下添加:

添加依赖如下:

dependencies {
    compile 'cn.shorr:ez-serialport:0.1.0'
}

添加适合的CPU平台ABI,如:

defaultConfig {
	...

	ndk {
		abiFilters "armeabi", "armeabi-v7a", "x86"
	}
}

不配置将默认为所有平台的ABI。

使用

//1.开始串口连接
private void startSerialPortConnect() {
	//配置串口参数
    serialPortUtil = new SerialPortUtil(this,
            new SerialPortConfig("/dev/ttyMT1", 115200), //串口0
            new SerialPortConfig("/dev/ttyMT2", 38400) //串口1

    );
    //设置为调试模式,打印收发数据
    serialPortUtil.setDebug(true);
    //绑定串口服务
    serialPortUtil.bindService();

    //串口0数据读取监听(可在不同Activity中同时设置监听)
    serial0Read = new SerialRead(this);
    serial0Read.registerListener(0/*默认为0,此参数可省略*/, new Serial0ReadListener());
    //串口1数据读取监听(可在不同Activity中同时设置监听)
    serial1Read = new SerialRead(this);
    serial1Read.registerListener(1, new Serial1ReadListener());
}

//2.设置串口读取监听
private class Serial0ReadListener implements SerialRead.ReadDataListener {
    @Override
    public void onReadData(byte[] data) {
       
    }
}

//3.串口发送数据
SerialWrite.sendData(Context context, int port, byte[] data);
//如:串口0发送数据
SerialWrite.sendData(this, 0/*默认为0,此参数可省略*/, content.getBytes("GBK"));
//如:串口1发送数据
SerialWrite.sendData(this, 1, content.getBytes("GBK"));

//4.停止串口连接
private void stopSerialPortConnect() {
    serial0Read.unRegisterListener();
    serial1Read.unRegisterListener();
    serialPortUtil.unBindService();
}

//5.获取设备所有的串口信息
SerialPortFinder serialPortFinder = new SerialPortFinder();
String[] devices = serialPortFinder.getAllDevicesPath();

开发建议

由于Android各硬件平台与驱动的差异性,在接收串口数据,即在通过监听获取byte[]字节数组时,一条完整的指令数据有可能被无规律分隔为多条byte[]字节数组。所以此时,需要将多条byte[]字节数组拼接为一条完整的指令,以便于指令的解析工作。

所以,在串口数据格式制定的初期一定要做好相关工作。比如,可以在数据格式中指定开始标记数据长度结束标记等,这样可以方便数据的拼接以及校验等工作。

ez-serialport's People

Contributors

shorr 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.