All Projects → alanqjt → SerialPortHelper

alanqjt / SerialPortHelper

Licence: other
一个低成本,能快速接入的串口通讯工具,支持处理并发,适配三种协议,同时支持监听多种不同报文头,适用于口红机、快递柜(格子柜),售货机、售餐机、咖啡机、游戏机等串口设备

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to SerialPortHelper

Libserial
Serial Port Programming in C++
Stars: ✭ 201 (+224.19%)
Mutual labels:  serialport
electron-vite-boilerplate
📚 A Electron + Vite boilerplate of the nature of learning(source-code of vite-plugin-electron) / 学习性的样板工程(vite-plugin-electron源码)
Stars: ✭ 157 (+153.23%)
Mutual labels:  serialport
pySerialTransfer
Python package to transfer data in a fast, reliable, and packetized form
Stars: ✭ 78 (+25.81%)
Mutual labels:  serialport
Common
Yet another serial port debugger.
Stars: ✭ 245 (+295.16%)
Mutual labels:  serialport
serial2mqtt
Serial to MQTT adapter serivce
Stars: ✭ 21 (-66.13%)
Mutual labels:  serialport
etherport-client
Client-side virtual serial port for Etherport. Used to implement firmata-compatible boards and relays.
Stars: ✭ 20 (-67.74%)
Mutual labels:  serialport
Android Serialport
『Android Lib』 Android平台上的usb串口调试库,支持串口号、波特率、数据位、校验位、停止位、流控等参数设置,能够控制数据的收发
Stars: ✭ 164 (+164.52%)
Mutual labels:  serialport
libserialport.dart
Serial Port for Dart
Stars: ✭ 51 (-17.74%)
Mutual labels:  serialport
ble-serial
"RFCOMM for BLE" a UART over Bluetooth low energy (4.0+) bridge for Linux, Mac and Windows
Stars: ✭ 134 (+116.13%)
Mutual labels:  serialport
serial.nim
A Nim library for accessing serial ports.
Stars: ✭ 59 (-4.84%)
Mutual labels:  serialport
SerialPundit
Serial port communication in Java - FTDI D2XX, HID API, X/Y modem
Stars: ✭ 116 (+87.1%)
Mutual labels:  serialport
libcssl
Columbo Simple Serial Library is an easy to use, event driven serial port communication library for Linux.
Stars: ✭ 38 (-38.71%)
Mutual labels:  serialport
SerialTest
Serial port test tool on Win/Linux/Android, with realtime plotting, shortcut | 跨平台串口助手,带实时绘图和快捷发送面板
Stars: ✭ 98 (+58.06%)
Mutual labels:  serialport
Serial Assistant
一款使用 C# 及 WPF 框架编写的串口调试助手,界面优雅、简洁,易于使用。
Stars: ✭ 212 (+241.94%)
Mutual labels:  serialport
serialport
PHP Serial Port
Stars: ✭ 42 (-32.26%)
Mutual labels:  serialport
Qtswissarmyknife
QSAK (Qt Swiss Army Knife) is a multi-functional, cross-platform debugging tool based on Qt.
Stars: ✭ 196 (+216.13%)
Mutual labels:  serialport
netty-transport-purejavacomm
A netty serial pipeline using JNA and PureJavaComm
Stars: ✭ 30 (-51.61%)
Mutual labels:  serialport
BaseIotUtils
🔥🔥串口工具,屏幕适配,通知工具类,多文件断点下载,xls,xlsx操作,文件处理,crash控制,音视频播放,usb设备检测,adb工具等...
Stars: ✭ 44 (-29.03%)
Mutual labels:  serialport
serialPort
Android通用的串口通信库
Stars: ✭ 39 (-37.1%)
Mutual labels:  serialport
PInvokeSerialPort
P/Invoke wrapper for Win32API serial port
Stars: ✭ 30 (-51.61%)
Mutual labels:  serialport

SerialPortHelper

API

目录

引入

  • Android Studio

serialportlib引入

dependencies {
     implementation project(path: ':serialportlib')
}

也可以直接

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
dependencies {
     implementation 'com.github.alanqjt:SerialPortHelper:v1.0.2'
}
  • Eclipse

还在使用Eclipse的你可以回家了!(把代码复制下来自己导入过去,但是还是建议赶紧把项目开发切换到Android Studio)

使用说明

串口协议一般分为两种,第一种为固定长度,第二种为可变长度,而可变长度又分为两种。 所以此库能帮助您快速适配三种类型的协议,如果还是不能满足开发需求,可以自行修改serialportlib中的ReadThread(兄弟,我也只能帮你到这了)

  • 第一种协议固定长度 PROTOCOLMODEL_FIXED



//协议模版
//请在串口调试助手发送E1 09 8A 01 01 00 01 08 EF

List<Integer> protocolHead = new ArrayList<>();
protocolHead.add(0xE1);
Parameter parameter = new Parameter(SERIALPATH, BAUDRATE, protocolHead, Parameter.PROTOCOLMODEL_FIXED, new OnSerialPortDataListener() {
    @Override
    public void onDataReceived(byte[] bytes, int length, String hexData) {
               
    }

    @Override
    public void onDataSent(byte[] bytes, int length, String hexData) {
                
    }
});
parameter.setProLenIndex(1);
parameter.setProtocolLength(9);
SerialHelper.getInstance().serialStart(parameter);
  • 第二种协议固定长度 PROTOCOLMODEL_VARIABLE

    1.data包含了所要截取的长度



//协议模版
//请在串口调试助手发送E1 09 8A 01 01 00 01 08 EF
//ProLenIndex 所在的下标是1,  值为0x09
List<Integer> protocolHead = new ArrayList<>();
protocolHead.add(0xE1);
Parameter parameter = new Parameter(SERIALPATH, BAUDRATE, protocolHead, Parameter.PROTOCOLMODEL_VARIABLE, new OnSerialPortDataListener() {
    @Override
    public void onDataReceived(byte[] bytes, int length, String hexData) {
               
    }

    @Override
    public void onDataSent(byte[] bytes, int length, String hexData) {
               
    }
});
parameter.setProLenIndex(1);//设置LEN下标
SerialHelper.getInstance().serialStart(parameter);



2.data不包含了所要截取的长度,指的是有意义的数据长度



//协议模版
//请在串口调试助手发送E1 05 8A 01 01 00 01 08 EF
//请在串口调试助手发送55 05 8A 01 01 00 01 08 EF
List<Integer> protocolHead = new ArrayList<>();
protocolHead.add(0xE1);
protocolHead.add(0x55);
Parameter parameter = new Parameter(SERIALPATH, BAUDRATE, protocolHead, Parameter.PROTOCOLMODEL_VARIABLE, new OnSerialPortDataListener() {
    @Override
    public void onDataReceived(byte[] bytes, int length, String hexData) {
               
    }

    @Override
    public void onDataSent(byte[] bytes, int length, String hexData) {
               
    }
});
parameter.setProLenIndex(1);//设置LEN下标
parameter.setUselessLength(4);//除了data有效数据外的长度
SerialHelper.getInstance().serialStart(parameter);
  • 发送数据
SerialHelper.getInstance().sendData(data);
  • 监听多种协议开头
List<Integer> protocolHead = new ArrayList<>();
protocolHead.add(0xE1);
protocolHead.add(0x55);
Parameter parameter = new Parameter(SERIALPATH, BAUDRATE, protocolHead, Parameter.PROTOCOLMODEL_VARIABLE, new OnSerialPortDataListener() {
    @Override
    public void onDataReceived(byte[] bytes, int length, String hexData) {
               
    }

    @Override
    public void onDataSent(byte[] bytes, int length, String hexData) {
               
    }
});
  • 读写线程消息队列缓存长度
//发送数据队列长度
parameter.setQueueSizeBySend(100);//默认缓存100

//监听数据队列长度
parameter.setQueueSizeByReceived(100);//默认缓存100
  • 读写线程消息处理并发处理速度
//发送数据间隔
parameter.setFrequencyBySend(200);//默认间隔为200毫秒

//监听数据间隔
parameter.setFrequencyByReceived(200);//默认间隔为200毫秒
  • 修改权限地址
parameter.setSuPath("/system/xbin/su");
  • 启动串口
SerialHelper.getInstance().serialStart(parameter);
  • 关闭串口
SerialHelper.getInstance().close();
  • 获取串口设备
SerialHelper.getInstance().getAllSerialDevices();
  • 获取串口设备地址
SerialHelper.getInstance().getAllSerialDevicesPath();
  • 查看日志
parameter.setDebug(true);

属性说明

属性 说明
debug 是否显示log
suPath 权限地址
baudrate 波特率
serialPath 串口地址
protocolHead 协议开头
protocolEnd 协议结尾
protocolLength 协议长度
proLenIndex 协议长度截取的下标 从0开始
protocolModel 协议模型
uselessLength 无用的数据长度
frequencyBySend 写线程处理速度, 只能大于0 默认为200毫秒
frequencyByReceived 读线程处理速度, 只能大于0 默认为200毫秒
queueSizeBySend 写线程队列缓存长度, 只能大于0 默认为100
queueSizeByReceived 读线程队列缓存长度, 只能大于0 默认为100
onSerialPortDataListener 数据监听
PROTOCOLMODEL_FIXED 固定长度协议, proLenIndex就没有意义了
PROTOCOLMODEL_VARIABLE 可变长度协议, proLenIndex才有意义

效果演示

  • 并发处理



项目成品

地心科技-色秀口红机

网红口红机

云际科技-游云魔盒

一种游艺设备,支持使用摇杆玩王者荣耀,和各种cocos2d,unity3d游戏。

逗买科技-逗买智能售货机

弹簧售货机

味都好-味都好智能售餐机

智能售餐机

License

Copyright 2019 屈健涛

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].