3 Star 5 Fork 1

caicailong / Android-Serialport

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Android-Serialport

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

Android-Serialport

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

文档

使用依赖Download

  1. Gradle引用
implementation 'tp.xmaihh:serialport:2.1'
  1. Maven引用
<dependency>
  <groupId>tp.xmaihh</groupId>
  <artifactId>serialport</artifactId>
  <version>2.1</version>
  <type>pom</type>
</dependency>

属性支持

属性 参数
波特率 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.粘包处理

支持粘包处理,原因见issue,提供的粘包处理有

  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端调试工具 友善串口调试工具

更新日志

2.1

新增

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

2.0

新增

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

1.0

新增

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

FAQ

  • 此library不提供ROOT权限,请自行打开串口666权限
adb shell  chmod 666 /dev/ttyS1
MIT License Copyright (c) 2018 xmaihh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

引用大神的工程,android串口链接,支持多种参数 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/jambestwick/Android-Serialport.git
git@gitee.com:jambestwick/Android-Serialport.git
jambestwick
Android-Serialport
Android-Serialport
master

搜索帮助

14c37bed 8189591 565d56ea 8189591