Skip to content

Commit 4bed737

Browse files
committed
Initial commit
0 parents  commit 4bed737

File tree

63 files changed

+2053
-0
lines changed

Some content is hidden

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

63 files changed

+2053
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Gradle files
2+
.gradle/
3+
build/
4+
5+
# Local configuration file (sdk path, etc)
6+
local.properties
7+
8+
# Log/OS Files
9+
*.log
10+
11+
# Android Studio generated files and folders
12+
captures/
13+
.externalNativeBuild/
14+
.cxx/
15+
*.apk
16+
output.json
17+
18+
# IntelliJ
19+
*.iml
20+
.idea/
21+
22+
# Keystore files
23+
*.jks
24+
*.keystore
25+
26+
# Google Services (e.g. APIs or Firebase)
27+
google-services.json
28+
29+
# Android Profiling
30+
*.hprof

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SerialPort
2+
Android 串口通讯实现,附带Modbus

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
compileSdkVersion 30
7+
buildToolsVersion "30.0.3"
8+
9+
defaultConfig {
10+
applicationId "com.boylab.example"
11+
minSdkVersion 19
12+
targetSdkVersion 30
13+
versionCode 1
14+
versionName "1.0"
15+
ndk {
16+
abiFilters "armeabi"
17+
}
18+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
21+
buildTypes {
22+
release {
23+
minifyEnabled false
24+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25+
}
26+
}
27+
compileOptions {
28+
sourceCompatibility JavaVersion.VERSION_1_8
29+
targetCompatibility JavaVersion.VERSION_1_8
30+
}
31+
}
32+
33+
dependencies {
34+
implementation fileTree(dir: 'libs', include: ['*.jar'])
35+
implementation 'androidx.appcompat:appcompat:1.5.0'
36+
implementation 'com.google.android.material:material:1.6.1'
37+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
38+
testImplementation 'junit:junit:4.+'
39+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
40+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
41+
42+
// implementation project(path: ':library')
43+
implementation files('libs/serialport.aar')
44+
implementation 'com.github.infiniteautomation:modbus4j:v3.0.5'
45+
46+
}

app/libs/serialport.aar

97.8 KB
Binary file not shown.

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.boylab.example;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.boylab.example", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.boylab.example">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/Theme.ModbusDemo">
12+
<activity android:name=".MainActivity"></activity>
13+
<activity android:name=".ModbusActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.boylab.example;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import android.os.Bundle;
6+
import android.serialport.SerialPortManager;
7+
import android.serialport.PortBean;
8+
import android.view.View;
9+
import android.widget.TextView;
10+
11+
public class MainActivity extends AppCompatActivity {
12+
13+
private SerialPortManager serialPortManager = null;
14+
private TextView text_Receive;
15+
16+
@Override
17+
protected void onCreate(Bundle savedInstanceState) {
18+
super.onCreate(savedInstanceState);
19+
setContentView(R.layout.activity_main);
20+
21+
text_Receive = findViewById(R.id.text_Receive);
22+
23+
findViewById(R.id.btn_Init).setOnClickListener(new View.OnClickListener() {
24+
@Override
25+
public void onClick(View view) {
26+
PortBean portBean = new PortBean("/dev/ttyMT2", 38400, PortBean.PARITY_EVEN);
27+
SerialPortManager.DEBUG = true;
28+
serialPortManager = new SerialPortManager(portBean, true); //启动自动读数据
29+
30+
serialPortManager.setOnAutoReadListener(new SerialPortManager.OnAutoReadListener() {
31+
@Override
32+
public void onAutoRead(byte[] bytes, int j) {
33+
text_Receive.postDelayed(new Runnable() {
34+
@Override
35+
public void run() {
36+
text_Receive.append(" 数量"+j+" ");
37+
for (int i = 0; i < bytes.length; i++) {
38+
text_Receive.append(" ");
39+
text_Receive.append(String.format("%2x", bytes[i]));
40+
}
41+
}
42+
}, 0);
43+
}
44+
});
45+
}
46+
});
47+
48+
findViewById(R.id.btn_ReadWeigh).setOnClickListener(new View.OnClickListener() {
49+
@Override
50+
public void onClick(View view) {
51+
byte[] bytes = new byte[]{0x01, 0x04, 0x00, 0x00, 0x00, 0x02, 0x71, (byte) 0xCB};
52+
serialPortManager.write(bytes);
53+
}
54+
});
55+
}
56+
57+
@Override
58+
protected void onStop() {
59+
super.onStop();
60+
serialPortManager.close();
61+
}
62+
}

0 commit comments

Comments
 (0)