Skip to content

Commit 4b74c59

Browse files
author
frey tse
committed
Initial commit
0 parents  commit 4b74c59

Some content is hidden

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

58 files changed

+3071
-0
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 28
5+
defaultConfig {
6+
applicationId "top.keepempty"
7+
minSdkVersion 19
8+
targetSdkVersion 28
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(include: ['*.jar'], dir: 'libs')
23+
implementation 'com.android.support:appcompat-v7:28.0.0'
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'
28+
//implementation project(':library')
29+
implementation project(':library')
30+
}

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 top.keepempty;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("aidesudi.sph", 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="top.keepempty">
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/AppTheme">
12+
<activity android:name=".MainActivity"
13+
android:screenOrientation="landscape">
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: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package top.keepempty;
2+
3+
import android.os.Bundle;
4+
import android.support.v4.content.ContextCompat;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.text.TextUtils;
7+
import android.util.Log;
8+
import android.view.View;
9+
import android.widget.AdapterView;
10+
import android.widget.ArrayAdapter;
11+
import android.widget.Button;
12+
import android.widget.EditText;
13+
import android.widget.Spinner;
14+
import android.widget.TextView;
15+
import android.widget.Toast;
16+
17+
import top.keepempty.sph.library.SerialPortConfig;
18+
import top.keepempty.sph.library.SerialPortFinder;
19+
import top.keepempty.sph.library.SerialPortHelper;
20+
import top.keepempty.sph.library.SphCmdEntity;
21+
import top.keepempty.sph.library.SphResultCallback;
22+
23+
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
24+
25+
26+
private static final String TAG = "SerialPortHelper";
27+
28+
private SerialPortHelper serialPortHelper;
29+
30+
private TextView mShowReceiveTxt;
31+
32+
private Button mSendBtn;
33+
private Button mOpenBtn;
34+
35+
private EditText mSendDataEt;
36+
37+
private Spinner mPathSpinner;
38+
private Spinner mBaudRateSpinner;
39+
private Spinner mDataSpinner;
40+
private Spinner mCheckSpinner;
41+
private Spinner mStopSpinner;
42+
43+
private int baudRate = 9600;
44+
private int dataBits = 8;
45+
private char checkBits = 'N';
46+
private int stopBits = 1;
47+
private String path;
48+
49+
private SerialPortFinder mSerialPortFinder;
50+
private String[] entryValues;
51+
private boolean isOpen;
52+
private StringBuilder receiveTxt = new StringBuilder();
53+
54+
@Override
55+
protected void onCreate(Bundle savedInstanceState) {
56+
super.onCreate(savedInstanceState);
57+
setContentView(R.layout.activity_main);
58+
59+
init();
60+
}
61+
62+
private void init() {
63+
mPathSpinner = findViewById(R.id.sph_path);
64+
mBaudRateSpinner = findViewById(R.id.sph_baudRate);
65+
mDataSpinner = findViewById(R.id.sph_data);
66+
mCheckSpinner = findViewById(R.id.sph_check);
67+
mStopSpinner = findViewById(R.id.sph_stop);
68+
mOpenBtn = findViewById(R.id.sph_openBtn);
69+
mSendBtn = findViewById(R.id.sph_sendBtn);
70+
mShowReceiveTxt = findViewById(R.id.sph_showReceiveTxt);
71+
mSendDataEt = findViewById(R.id.sph_sendDataEt);
72+
73+
mBaudRateSpinner.setSelection(13);
74+
mDataSpinner.setSelection(3);
75+
mCheckSpinner.setSelection(0);
76+
mStopSpinner.setSelection(0);
77+
78+
mPathSpinner.setOnItemSelectedListener(this);
79+
mBaudRateSpinner.setOnItemSelectedListener(this);
80+
mDataSpinner.setOnItemSelectedListener(this);
81+
mCheckSpinner.setOnItemSelectedListener(this);
82+
mStopSpinner.setOnItemSelectedListener(this);
83+
84+
mSerialPortFinder = new SerialPortFinder();
85+
entryValues = mSerialPortFinder.getAllDevicesPath();
86+
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
87+
this, android.R.layout.simple_spinner_item,
88+
entryValues);
89+
mPathSpinner.setAdapter(adapter);
90+
91+
mSendBtn = findViewById(R.id.sph_sendBtn);
92+
mSendBtn.setOnClickListener(new View.OnClickListener() {
93+
@Override
94+
public void onClick(View v) {
95+
String sendTxt = mSendDataEt.getText().toString().trim();
96+
if(TextUtils.isEmpty(sendTxt)){
97+
Toast.makeText(MainActivity.this,"请输入发送命令!",Toast.LENGTH_LONG).show();
98+
return;
99+
}
100+
if (sendTxt.length() % 2 == 1) {
101+
Toast.makeText(MainActivity.this,"命令错误!",Toast.LENGTH_LONG).show();
102+
return;
103+
}
104+
serialPortHelper.addCommands(sendTxt);
105+
}
106+
});
107+
108+
mOpenBtn.setOnClickListener(new View.OnClickListener() {
109+
@Override
110+
public void onClick(View v) {
111+
if(isOpen){
112+
serialPortHelper.closeDevice();
113+
isOpen = false;
114+
}else{
115+
openSerialPort();
116+
}
117+
showState();
118+
}
119+
});
120+
}
121+
122+
/**
123+
* 打开串口
124+
*/
125+
private void openSerialPort(){
126+
127+
/**
128+
* 串口参数
129+
*/
130+
SerialPortConfig serialPortConfig = new SerialPortConfig();
131+
serialPortConfig.mode = 0;
132+
serialPortConfig.path = path;
133+
serialPortConfig.baudRate = baudRate;
134+
serialPortConfig.dataBits = dataBits;
135+
serialPortConfig.parity = checkBits;
136+
serialPortConfig.stopBits = stopBits;
137+
138+
139+
// 初始化串口
140+
serialPortHelper = new SerialPortHelper(16);
141+
// 设置串口参数
142+
serialPortHelper.setConfigInfo(serialPortConfig);
143+
// 开启串口
144+
isOpen = serialPortHelper.openDevice();
145+
if(!isOpen){
146+
Toast.makeText(this,"串口打开失败!",Toast.LENGTH_LONG).show();
147+
}
148+
serialPortHelper.setSphResultCallback(new SphResultCallback() {
149+
@Override
150+
public void onSendData(SphCmdEntity sendCom) {
151+
Log.d(TAG, "发送命令:" + sendCom.commandsHex);
152+
}
153+
154+
@Override
155+
public void onReceiveData(SphCmdEntity data) {
156+
Log.d(TAG, "收到命令:" + data.commandsHex);
157+
receiveTxt.append(data.commandsHex).append("\n");
158+
mShowReceiveTxt.setText(receiveTxt.toString());
159+
}
160+
161+
@Override
162+
public void onComplete() {
163+
Log.d(TAG, "完成");
164+
}
165+
});
166+
}
167+
168+
@Override
169+
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
170+
switch (parent.getId()) {
171+
case R.id.sph_path:
172+
path = entryValues[position];
173+
break;
174+
case R.id.sph_baudRate:
175+
String[] baud_rates = getResources().getStringArray(R.array.baud_rate_arr);
176+
baudRate = Integer.parseInt(baud_rates[position]);
177+
break;
178+
case R.id.sph_data:
179+
String[] data_rates = getResources().getStringArray(R.array.data_bits_arr);
180+
dataBits = Integer.parseInt(data_rates[position]);
181+
break;
182+
case R.id.sph_check:
183+
String[] check_rates = getResources().getStringArray(R.array.check_digit_arr);
184+
checkBits = check_rates[position].charAt(0);
185+
break;
186+
case R.id.sph_stop:
187+
String[] stop_rates = getResources().getStringArray(R.array.stop_bits_arr);
188+
stopBits = Integer.parseInt(stop_rates[position]);
189+
break;
190+
default:
191+
break;
192+
}
193+
}
194+
195+
@Override
196+
public void onNothingSelected(AdapterView<?> parent) {
197+
198+
}
199+
200+
private void showState(){
201+
if(isOpen){
202+
Toast.makeText(this,"串口打开成功!",Toast.LENGTH_LONG).show();
203+
mOpenBtn.setText("关闭串口");
204+
mOpenBtn.setTextColor(ContextCompat.getColor(this,R.color.org));
205+
mOpenBtn.setBackgroundResource(R.drawable.button_style_stroke);
206+
}else {
207+
mOpenBtn.setText("打开串口");
208+
mOpenBtn.setTextColor(ContextCompat.getColor(this,R.color.white));
209+
mOpenBtn.setBackgroundResource(R.drawable.button_style_org);
210+
}
211+
}
212+
213+
214+
public void clearSend(View view) {
215+
mSendDataEt.setText("");
216+
}
217+
218+
public void clearReceive(View view) {
219+
receiveTxt = new StringBuilder();
220+
mShowReceiveTxt.setText("");
221+
}
222+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeWidth="1"
11+
android:strokeColor="#00000000">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeWidth="1"
33+
android:strokeColor="#00000000" />
34+
</vector>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<corners android:radius="8dp" />
4+
<solid android:color="#e17808" />
5+
<stroke android:color="#e17808" android:width="1dp"/>
6+
</shape>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<corners android:radius="8dp" />
4+
<solid android:color="#fffef9" />
5+
<stroke android:color="#e17808" android:width="1dp"/>
6+
</shape>

0 commit comments

Comments
 (0)