├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── top │ │ └── maybesix │ │ └── demo │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── top │ │ │ └── maybesix │ │ │ └── demo │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── top │ └── maybesix │ └── demo │ └── ExampleUnitTest.kt ├── build.gradle ├── easyserialport ├── .gitignore ├── CMakeLists.txt ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── cpp │ ├── SerialPort.c │ └── SerialPort.h │ └── main │ ├── AndroidManifest.xml │ └── java │ ├── android_serialport_api │ ├── SerialPort.java │ └── SerialPortFinder.java │ └── top │ └── maybesix │ └── easyserialport │ ├── ComPortData.java │ ├── EasySerialPort.java │ └── util │ ├── CrcUtils.java │ └── HexStringUtils.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-EasySerialPort 2 | 非常好用、非常稳定的Android串口封装 3 | 久经两年多项目考验,一直很稳定 4 | ## 使用说明 5 | 第一步,在gradle(Project)下添加 6 | ``` 7 | allprojects { 8 | repositories { 9 | ... 10 | maven { url 'https://www.jitpack.io' } 11 | } 12 | } 13 | ``` 14 | 第二步,导入依赖 15 | ``` 16 | dependencies { 17 | implementation 'com.github.maybesix:Android-XHLibrary:v1.0.0' 18 | } 19 | ``` 20 | 在需要实现Activity或者Service中这样写: 21 | 22 | ```  23 | SerialPortHelper serialPort; 24 | String port = "/dev/ttyHSL1"; 25 | int baudRate = 9600; 26 | //串口程序初始化 27 | serialPort = new SerialPortHelper(port, baudRate, this); 28 | //打开串口 29 | serialPort.open(); 30 | ``` 31 | 串口发送: 32 | ``` 33 | //发送十六进制 34 | serialPort.sendHex("A55A0010002096"); 35 | //发送文本 36 | serialPort.sendHex("hello world"); 37 | ``` 38 | 串口接收:实现SerialPortHelper.OnSerialPortReceivedListener接口 39 | ``` 40 | public class MainActivity extends AppCompatActivity implements SerialPortHelper.OnSerialPortReceivedListener { 41 | ... 42 | @Override 43 | protected void onCreate(Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | ... 46 | } 47 | ... 48 | @Override 49 | public void onSerialPortDataReceived(ComPortData comPortData) { 50 | //处理接收的串口消息 51 | String s = HexStringUtils.byteArray2HexString(comPortData.getRecData()); 52 | Log.i(TAG, "onReceived: " + s); 53 | } 54 | } 55 | ``` 56 | 或者可以使用构造者链式调用(kotlin写法) 57 | ``` 58 | serialPort = EasySerialPort.Builder() 59 | .setBaudRate(9600) 60 | .setPort("") 61 | .setSatesListener(object : EasySerialPort.OnStatesChangeListener { 62 | /** 63 | * 打开的状态回调 64 | * 65 | * @param isSuccess 是否成功 66 | * @param reason 原因 67 | */ 68 | override fun onOpen(isSuccess: Boolean, reason: String) { 69 | Log.i("EasySerialPort", "是否开启成功:$isSuccess,原因:$reason") 70 | Toast.makeText( 71 | applicationContext, 72 | "是否开启成功:$isSuccess,原因:$reason", 73 | Toast.LENGTH_SHORT 74 | ).show() 75 | 76 | } 77 | 78 | /** 79 | * 关闭的状态回调 80 | */ 81 | override fun onClose() { 82 | Log.i("EasySerialPort", "已关闭") 83 | Toast.makeText(applicationContext, "已关闭", Toast.LENGTH_SHORT).show() 84 | } 85 | }) 86 | .setListener { 87 | //处理接收的串口消息 88 | val s: String = HexStringUtils.byteArray2HexString(it.recData) 89 | Log.i("EasySerialPort", "onReceived: $s,time:${it.recTime}") 90 | textView.text = s 91 | } 92 | .build() 93 | ``` 94 | 至此,串口的打开、发送、接收就全部完成了。 95 | ## 串口相关 96 | > 串口操作类 → [SerialPortHelper](https://github.com/maybesix/Android-XHLibrary/blob/master/XHLibrary/src/main/java/top/maybesix/xhlibrary/serialport/SerialPortHelper.java) 97 | ``` 98 | isOpen : 是否开启串口 99 | getBaudRate : 获取波特率 100 | setBaudRate : 设置波特率 101 | getPort : 获取串口名称 102 | setPort : 设置串口名称 103 | open : 打开串口 104 | close : 关闭串口 105 | sendHex : 以16进制发送 106 | sendTxtString : 以文本发送 107 | getLoopData : 获取循环发送的数据 108 | setLoopData : 设置循环发送的数据 109 | getDelay : 获取延迟 110 | setDelay : 设置延时(毫秒) 111 | startSend : 开启循环发送 112 | stopSend : 停止循环发送 113 | OnSerialPortReceivedListener : 串口数据接收回调 114 | ``` 115 | > 串口数据基类 → [ComPortData](https://github.com/maybesix/Android-XHLibrary/blob/master/XHLibrary/src/main/java/top/maybesix/xhlibrary/serialport/ComPortData.java) 116 | ``` 117 | getRecData : 获取串口数据 118 | setRecData : 设置串口数据 119 | getRecTime : 获取接收时间 120 | setRecTime : 设置接受时间 121 | getComPort : 获取串口名称 122 | setComPort : 设置串口名称 123 | ``` 124 | ## 串口数据处理相关 125 | > 十六进制转换 → [HexStringUtils](https://github.com/maybesix/Android-XHLibrary/blob/master/XHLibrary/src/main/java/top/maybesix/xhlibrary/util/HexStringUtils.java) 126 | ``` 127 | isOdd : 判断是否为奇数 128 | hexString2Int : 16进制字符串转int 129 | hexString2Byte : 16进制字符串转byte 130 | byte2HexString : byte转16进制字符串 131 | byteArray2HexString : byte数组转16进制字符串 132 | hexString2ByteArray : 16进制字符串转byte数组 133 | ``` 134 | > CRC校验 → [CrcUtils](https://github.com/maybesix/Android-XHLibrary/blob/master/XHLibrary/src/main/java/top/maybesix/xhlibrary/util/CrcUtils.java) 135 | ``` 136 | isPassCRC : 返回是否通过验证 137 | getCrcString : 获取16进制的crc字符串 138 | toHexString : int转16进制字符串 139 | getCrc : 传入bytes,计算得到CRC验证码 140 | hexToByte : 16进制字符串转byte数组 141 | ``` 142 | 143 | ## 项目更新内容: 144 | ### v1.1: 145 | 146 | 1. 升级至androidx 147 | 2. 去除不必要的依赖 148 | 3. 支持链式调用配置监听事件、设置端口号、设置波特率 149 | 4. 修改串口接收数据时格式化时间,现在改为时间戳 150 | 151 | ## 如果这个项目对你有帮助,请点个star! 152 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 29 7 | buildToolsVersion "29.0.3" 8 | 9 | defaultConfig { 10 | applicationId "top.maybesix.demo" 11 | minSdkVersion 19 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | } 27 | 28 | dependencies { 29 | implementation fileTree(dir: 'libs', include: ['*.jar']) 30 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 31 | implementation 'androidx.appcompat:appcompat:1.1.0' 32 | implementation 'androidx.core:core-ktx:1.2.0' 33 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 34 | //引入串口库 35 | implementation project(':easyserialport') 36 | 37 | } 38 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/top/maybesix/demo/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package top.maybesix.demo 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("top.maybesix.demo", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/top/maybesix/demo/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package top.maybesix.demo 2 | 3 | import android.os.Bundle 4 | import android.util.Log 5 | import android.widget.Toast 6 | import androidx.appcompat.app.AppCompatActivity 7 | import kotlinx.android.synthetic.main.activity_main.* 8 | import top.maybesix.easyserialport.EasySerialPort 9 | import top.maybesix.easyserialport.util.HexStringUtils 10 | 11 | class MainActivity : AppCompatActivity() { 12 | private lateinit var serialPort: EasySerialPort 13 | override fun onCreate(savedInstanceState: Bundle?) { 14 | super.onCreate(savedInstanceState) 15 | setContentView(R.layout.activity_main) 16 | //初始化 17 | serialPort = EasySerialPort.Builder() 18 | .setBaudRate(9600) 19 | .setPort("") 20 | .setSatesListener(object : EasySerialPort.OnStatesChangeListener { 21 | /** 22 | * 打开的状态回调 23 | * 24 | * @param isSuccess 是否成功 25 | * @param reason 原因 26 | */ 27 | override fun onOpen(isSuccess: Boolean, reason: String) { 28 | Log.i("EasySerialPort", "是否开启成功:$isSuccess,原因:$reason") 29 | Toast.makeText( 30 | applicationContext, 31 | "是否开启成功:$isSuccess,原因:$reason", 32 | Toast.LENGTH_SHORT 33 | ).show() 34 | 35 | } 36 | 37 | /** 38 | * 关闭的状态回调 39 | */ 40 | override fun onClose() { 41 | Log.i("EasySerialPort", "已关闭") 42 | Toast.makeText(applicationContext, "已关闭", Toast.LENGTH_SHORT).show() 43 | } 44 | }) 45 | .setListener { 46 | //处理接收的串口消息 47 | val s: String = HexStringUtils.byteArray2HexString(it.recData) 48 | Log.i("EasySerialPort", "onReceived: $s,time:${it.recTime}") 49 | textView.text = s 50 | } 51 | .build() 52 | 53 | btn_open.setOnClickListener { 54 | serialPort.open() 55 | } 56 | btn_close.setOnClickListener { 57 | serialPort.close() 58 | } 59 | btn_send.setOnClickListener { 60 | if (serialPort.isNotOpen) { 61 | Toast.makeText(applicationContext, "请先开启串口", Toast.LENGTH_SHORT).show() 62 | } else { 63 | Toast.makeText( 64 | applicationContext, 65 | "发送:${et_send.text.toString()}", 66 | Toast.LENGTH_SHORT 67 | ).show() 68 | serialPort.sendTxtString(et_send.text.toString()) 69 | } 70 | } 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 12 | 13 | 17 | 18 | 23 | 24 | 32 | 33 | 41 | 42 | 49 | 50 |