Android串口通信工具

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();

项目源码:GitHub - tyhjh/SerialPortUtil: Android串口通信工具



作者:Tyhj
链接:https://www.jianshu.com/p/4861f377330b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值