Git Product home page Git Product logo

serialportutil's Introduction

SerialPortUtil

Android串口通信简单封装,可以用于和连接串口的硬件通信或者进行硬件调试

集成方法:

Step 1. Add the JitPack repository to your build file

//Add it in your root build.gradle at the end of repositories:
allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Step 2. Add the dependency

//Add the dependency
dependencies {
	       implementation 'com.github.tyhjh:SerialPortUtil:1.0.0' 
	}

调用方法

读取文件权限应该是需要的

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

获取所有串口地址

String[] devicesPath = new SerialPortFinder().getDevicesPaths();

打开串口,设置读取返回数据超时时间

SerialPortService serialPortService = new SerialPortBuilder()
                        .setTimeOut(100L)
                        .setBaudrate(9600)
                        .setDevicePath("dev/ttyS4")
                        .createService();

发送指令

//发送byte数组数据
byte[] receiveData = serialPortService.sendData(new byte[2]);

//发送16进制的字符串
byte[] receiveData = serialPortService.sendData("55AA0101010002");
Log.e("MainActivity:", ByteStringUtil.byteArrayToHexStr(receiveData));

打开或者关闭日志,默认关闭

serialPortService.isOutputLog(true);

//关闭串口

serialPortService.close();

项目源码:https://github.com/tyhjh/SerialPortUtil

serialportutil's People

Contributors

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