Skip to content

Commit bca8b79

Browse files
committed
整理项目
1 parent 1cfea0e commit bca8b79

File tree

179 files changed

+17285
-506
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+17285
-506
lines changed

README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Modbus4Android
2-
Modbus的Android实现,添加对Android串口(RTU)的支持,支持RxJava操作
2+
Modbus的Android实现,添加对Android串口(RTU)的支持
33

44
[![](https://jitpack.io/v/licheedev/Modbus4Android.svg)](https://jitpack.io/#licheedev/Modbus4Android)
55

@@ -25,7 +25,7 @@ allprojects {
2525
}
2626
2727
dependencies {
28-
implementation 'com.github.licheedev:Modbus4Android:2.0.2'
28+
implementation 'com.github.licheedev:Modbus4Android:3.0.0'
2929
}
3030
3131
```
@@ -127,6 +127,7 @@ ModbusConfig.setEnableDataLog(true, true);
127127
```
128128

129129
### 功能码操作示例
130+
#### 普通用法
130131
```java
131132
// 普通写法
132133
ModbusManager.get()
@@ -172,6 +173,51 @@ ModbusManager.get()
172173

173174
其他功能码的用法,可以参考Demo的[MainActivity.java](https://github.com/licheedev/Modbus4Android/blob/master/app/src/main/java/com/licheedev/demo/MainActivity.java)
174175

176+
#### RxJava用法(2.0,其他版本按需修改)
177+
复制[rxjava](https://github.com/licheedev/Modbus4Android/tree/master/app/src/main/java/com/licheedev/impl/rxjava)相关文件到自己的项目中
178+
179+
```java
180+
// 修改实现方式
181+
//public class ModbusManager extends ModbusWorker {}
182+
public class ModbusManager extends RxModbusWorker {}
183+
184+
// Rx写法
185+
ModbusManager.get()
186+
.rxReadHoldingRegisters(mSalveId, mOffset, mAmount)
187+
.observeOn(AndroidSchedulers.mainThread())
188+
.compose(this.<ReadHoldingRegistersResponse>bindUntilEvent(ActivityEvent.DESTROY))
189+
.subscribe(new ModbusObserver<ReadHoldingRegistersResponse>() {
190+
@Override
191+
public void onSuccess(
192+
ReadHoldingRegistersResponse readHoldingRegistersResponse) {
193+
byte[] data = readHoldingRegistersResponse.getData();
194+
mTvConsole.append("F03读取:" + ByteUtil.bytes2HexStr(data) + "\n");
195+
}
196+
197+
@Override
198+
public void onFailure(Throwable tr) {
199+
appendError("F03", tr);
200+
}
201+
});
202+
```
203+
204+
#### Kotlin协程用法
205+
复制[ModbusWorkers.kt](https://github.com/licheedev/Modbus4Android/tree/master/app/src/main/java/com/licheedev/impl/kotlin/ModbusWorkers.kt)相关文件到自己的项目中
206+
```kotlin
207+
// 协程写法
208+
lifecycleScope.launch {
209+
try {
210+
val response =
211+
ModbusManager.get().awaitReadHoldingRegisters(mSalveId, mOffset, mAmount)
212+
val data: ByteArray = response.data
213+
appendText("F03读取:" + ByteUtil.bytes2HexStr(data) + "\n")
214+
} catch (e: Exception) {
215+
appendError("F03", e)
216+
}
217+
}
218+
```
219+
220+
175221

176222
## 截图
177223
![F01](https://raw.githubusercontent.com/licheedev/Modbus4Android/master/imgs/01.png)

apk_download/app-debug.apk

4.51 MB
Binary file not shown.

app/build.gradle

+41-18
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,76 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
23

34
android {
4-
compileSdkVersion 27
5+
compileSdkVersion 31
56
defaultConfig {
67
applicationId "com.licheedev.modbusdebug"
78
minSdkVersion 19
8-
targetSdkVersion 27
9+
targetSdkVersion 31
910
versionCode 1
1011
versionName "1.0"
11-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1213
}
1314
buildTypes {
1415
release {
1516
minifyEnabled false
1617
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1718
}
1819
}
20+
21+
// Butterknife requires Java 8.
22+
compileOptions {
23+
sourceCompatibility JavaVersion.VERSION_1_8
24+
targetCompatibility JavaVersion.VERSION_1_8
25+
}
26+
27+
kotlinOptions {
28+
jvmTarget = "1.8"
29+
}
1930
}
2031

2132
dependencies {
2233
implementation fileTree(dir: 'libs', include: ['*.jar'])
23-
implementation 'com.android.support:appcompat-v7:27.1.1'
24-
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
25-
testImplementation 'junit:junit:4.12'
26-
androidTestImplementation 'com.android.support.test:runner:1.0.2'
27-
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
34+
implementation 'androidx.appcompat:appcompat:1.2.0'
35+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
36+
testImplementation 'junit:junit:4.13'
37+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
38+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
2839

2940
implementation project(':modbus4android')
30-
// implementation 'com.github.licheedev:Modbus4Android:0.21'
41+
// implementation 'com.github.licheedev:Modbus4Android:0.21'
3142
// logplus
32-
implementation 'com.github.licheedev.SomeLibrary:myutils:0.02'
43+
implementation 'com.licheedev:logplus:1.0.1'
3344
// gson
34-
implementation 'com.google.code.gson:gson:2.8.2'
45+
implementation 'com.google.code.gson:gson:2.10.1'
3546
// 下拉
3647
implementation 'com.github.arcadefire:nice-spinner:1.3.5'
3748
// 增强版RadioGroup
3849
implementation 'com.github.fodroid:XRadioGroup:v1.5'
39-
// rx
40-
implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.1.0'
41-
4250
// 屏幕适配
43-
implementation 'com.github.licheedev:AdaptScreen:1.0.0'
51+
implementation 'com.github.licheedev.SmallestWidthDimens:sw_750:1.0.0'
4452

45-
4653
// 黄油刀
47-
implementation 'com.jakewharton:butterknife:8.8.1'
48-
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
54+
implementation 'com.jakewharton:butterknife:10.2.3'
55+
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
4956

5057
// 菊花对话框
5158
implementation 'com.kaopiz:kprogresshud:1.2.0'
5259

60+
// rx全家桶
61+
implementation 'io.reactivex.rxjava2:rxjava:2.2.7'
62+
implementation 'com.trello.rxlifecycle2:rxlifecycle-android:2.2.2'
63+
implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.1.0'
64+
65+
// kotlin 协程
66+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
67+
68+
// kotlin相关
69+
def lifecycle_version = "2.5.1"
70+
// LiveData
71+
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
72+
// Lifecycles only (without ViewModel or LiveData)
73+
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
74+
// alternately - if using Java8, use the following instead of lifecycle-compiler
75+
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
5376
}

app/src/androidTest/java/com/licheedev/modbus4android/ExampleInstrumentedTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.licheedev.modbus4android;
22

33
import android.content.Context;
4-
import android.support.test.InstrumentationRegistry;
5-
import android.support.test.runner.AndroidJUnit4;
4+
import androidx.test.platform.app.InstrumentationRegistry;
5+
import androidx.test.ext.junit.runners.AndroidJUnit4;
66

77
import org.junit.Test;
88
import org.junit.runner.RunWith;

app/src/main/AndroidManifest.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
android:roundIcon="@mipmap/ic_launcher_round"
1313
android:supportsRtl="true"
1414
android:theme="@style/AppTheme">
15-
<activity android:name=".ChooseModeActivity">
15+
<activity android:name=".ChooseModeActivity"
16+
android:exported="true">
1617
<intent-filter>
1718
<action android:name="android.intent.action.MAIN" />
1819

app/src/main/java/com/licheedev/demo/App.java

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.licheedev.demo;
22

33
import android.app.Application;
4-
import com.licheedev.adaptscreen.AdaptScreenEx;
54
import com.licheedev.demo.base.PrefUtil;
65
import com.serotonin.modbus4j.ModbusConfig;
76

@@ -13,8 +12,6 @@ public class App extends Application {
1312
public void onCreate() {
1413
super.onCreate();
1514
sInstance = this;
16-
// 屏幕适配
17-
AdaptScreenEx.init(this);
1815
PrefUtil.init(this);
1916

2017
configModbus();

app/src/main/java/com/licheedev/demo/ChooseModeActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.licheedev.demo;
22

33
import android.os.Bundle;
4-
import android.support.annotation.Nullable;
4+
import androidx.annotation.Nullable;
55
import android.view.View;
66
import android.widget.Button;
77
import butterknife.BindView;

app/src/main/java/com/licheedev/demo/MainActivity.java

+23-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.os.Bundle;
6-
import android.support.annotation.IntDef;
76
import android.view.View;
87
import android.widget.AdapterView;
98
import android.widget.Button;
@@ -13,15 +12,16 @@
1312
import android.widget.LinearLayout;
1413
import android.widget.RadioButton;
1514
import android.widget.TextView;
15+
import androidx.annotation.IntDef;
1616
import butterknife.BindView;
1717
import butterknife.ButterKnife;
1818
import butterknife.OnClick;
1919
import com.licheedev.demo.base.BaseActivity;
2020
import com.licheedev.demo.base.ByteUtil;
2121
import com.licheedev.demo.modbus.DeviceConfig;
2222
import com.licheedev.demo.modbus.ModbusManager;
23+
import com.licheedev.impl.rxjava.ModbusObserver;
2324
import com.licheedev.modbus4android.ModbusCallback;
24-
import com.licheedev.modbus4android.ModbusObserver;
2525
import com.licheedev.modbus4android.ModbusParam;
2626
import com.licheedev.modbus4android.param.SerialParam;
2727
import com.licheedev.modbus4android.param.TcpParam;
@@ -481,6 +481,7 @@ private void trySend() {
481481
}
482482

483483
private void appendError(String func, Throwable tr) {
484+
LogPlus.e("出现异常",tr);
484485
appendText(func + "异常:\n" + tr + "\n");
485486
}
486487

@@ -527,11 +528,13 @@ private void send02() {
527528
new ModbusCallback<ReadDiscreteInputsResponse>() {
528529
@Override
529530
public void onSuccess(
530-
ReadDiscreteInputsResponse readDiscreteInputsResponse) {
531+
ReadDiscreteInputsResponse readDiscreteInputsResponse
532+
) {
531533

532534
boolean[] sub =
533535
ArrayUtils.subarray(readDiscreteInputsResponse.getBooleanData(), 0,
534-
amount);
536+
amount
537+
);
535538
appendText("F02读取:\n" + ArrayUtils.toString(sub) + "\n");
536539
}
537540

@@ -544,7 +547,8 @@ public void onFailure(Throwable tr) {
544547
public void onFinally() {
545548

546549
}
547-
});
550+
}
551+
);
548552
}
549553
}
550554

@@ -582,7 +586,8 @@ private void send03() {
582586
.subscribe(new ModbusObserver<ReadHoldingRegistersResponse>() {
583587
@Override
584588
public void onSuccess(
585-
ReadHoldingRegistersResponse readHoldingRegistersResponse) {
589+
ReadHoldingRegistersResponse readHoldingRegistersResponse
590+
) {
586591
byte[] data = readHoldingRegistersResponse.getData();
587592
appendText("F03读取:\n" + ByteUtil.bytes2HexStr(data) + "\n");
588593
}
@@ -604,7 +609,8 @@ private void send04() {
604609
new ModbusCallback<ReadInputRegistersResponse>() {
605610
@Override
606611
public void onSuccess(
607-
ReadInputRegistersResponse readInputRegistersResponse) {
612+
ReadInputRegistersResponse readInputRegistersResponse
613+
) {
608614
byte[] data = readInputRegistersResponse.getData();
609615
appendText("F04读取:\n" + ByteUtil.bytes2HexStr(data) + "\n");
610616
}
@@ -618,7 +624,8 @@ public void onFailure(Throwable tr) {
618624
public void onFinally() {
619625

620626
}
621-
});
627+
}
628+
);
622629
}
623630
}
624631

@@ -643,7 +650,8 @@ public void onFailure(Throwable tr) {
643650
public void onFinally() {
644651

645652
}
646-
});
653+
}
654+
);
647655
}
648656
}
649657

@@ -667,7 +675,8 @@ public void onFailure(Throwable tr) {
667675
public void onFinally() {
668676

669677
}
670-
});
678+
}
679+
);
671680
}
672681
}
673682

@@ -691,7 +700,8 @@ public void onFailure(Throwable tr) {
691700
public void onFinally() {
692701

693702
}
694-
});
703+
}
704+
);
695705
}
696706
}
697707

@@ -717,7 +727,8 @@ public void onFailure(Throwable tr) {
717727
public void onFinally() {
718728

719729
}
720-
});
730+
}
731+
);
721732
}
722733
}
723734

0 commit comments

Comments
 (0)