-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chenli
committed
Aug 13, 2021
1 parent
24cd0e0
commit 30bbd2b
Showing
10 changed files
with
445 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
app/src/main/java/com/cl/myapplication/MainActivity2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.cl.myapplication; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import android.os.Bundle; | ||
import android.text.TextUtils; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.EditText; | ||
import android.widget.Toast; | ||
|
||
import com.kongqw.serialportlibrary.ConfigurationSdk; | ||
import com.kongqw.serialportlibrary.Device; | ||
import com.kongqw.serialportlibrary.SerialPortManager; | ||
import com.kongqw.serialportlibrary.listener.OnOpenSerialPortListener; | ||
import com.kongqw.serialportlibrary.listener.OnSerialPortDataListener; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
|
||
public class MainActivity2 extends AppCompatActivity implements OnOpenSerialPortListener { | ||
|
||
|
||
private static final String TAG = MainActivity2.class.getSimpleName(); | ||
public static final String DEVICE = "device"; | ||
private SerialPortManager mSerialPortManager; | ||
private Toast mToast; | ||
|
||
private byte[] b1 = {(byte) 33, (byte) -3}; | ||
|
||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main2); | ||
Device device = (Device) getIntent().getSerializableExtra(DEVICE); | ||
Log.i(TAG, "onCreate: device = " + device); | ||
if (null == device) { | ||
finish(); | ||
return; | ||
} | ||
|
||
//构建初始化参数 | ||
ConfigurationSdk sdk = new ConfigurationSdk.ConfigurationBuilder(device.getFile(), 115200) | ||
.log("TAG", true, false) | ||
.msgHead(b1) | ||
.build(); | ||
SerialPortManager.getInstance().init(sdk,this); | ||
|
||
// 打开串口 | ||
SerialPortManager.getInstance().setOnOpenSerialPortListener(this) | ||
.setOnSerialPortDataListener(new OnSerialPortDataListener() { | ||
@Override | ||
public void onDataReceived(byte[] bytes) { | ||
Log.i(TAG, "onDataReceived [ byte[] ]: " + Arrays.toString(bytes)); | ||
Log.i(TAG, "onDataReceived [ String ]: " + new String(bytes)); | ||
final byte[] finalBytes = bytes; | ||
runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
showToast(String.format("接收\n%s", new String(finalBytes))); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onDataSent(byte[] bytes) { | ||
Log.i(TAG, "onDataSent [ byte[] ]: " + Arrays.toString(bytes)); | ||
Log.i(TAG, "onDataSent [ String ]: " + new String(bytes)); | ||
final byte[] finalBytes = bytes; | ||
runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
showToast(String.format("发送\n%s", new String(finalBytes))); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
} | ||
|
||
|
||
@Override | ||
public void onSuccess(File device) { | ||
showToast("串口打开成功"); | ||
} | ||
|
||
@Override | ||
public void onFail(File device, Status status) { | ||
switch (status) { | ||
case NO_READ_WRITE_PERMISSION: | ||
showToast("没有读写权限"); | ||
break; | ||
case OPEN_FAIL: | ||
default: | ||
showToast("串口打开失败"); | ||
break; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* 发送数据 | ||
* | ||
* @param view view | ||
*/ | ||
public void onSend(View view) { | ||
EditText editTextSendContent = (EditText) findViewById(R.id.et_send_content); | ||
if (null == editTextSendContent) { | ||
return; | ||
} | ||
String sendContent = editTextSendContent.getText().toString().trim(); | ||
if (TextUtils.isEmpty(sendContent)) { | ||
Log.i(TAG, "onSend: 发送内容为 null"); | ||
return; | ||
} | ||
|
||
byte[] sendContentBytes = sendContent.getBytes(); | ||
|
||
boolean sendBytes = mSerialPortManager.sendBytes(sendContentBytes); | ||
Log.i(TAG, "onSend: sendBytes = " + sendBytes); | ||
showToast(sendBytes ? "发送成功" : "发送失败"); | ||
} | ||
|
||
|
||
/** | ||
* Toast | ||
* | ||
* @param content content | ||
*/ | ||
private void showToast(String content) { | ||
if (null == mToast) { | ||
mToast = Toast.makeText(getApplicationContext(), null, Toast.LENGTH_SHORT); | ||
} | ||
mToast.setText(content); | ||
mToast.show(); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
if (null != mSerialPortManager) { | ||
mSerialPortManager.closeSerialPort(); | ||
mSerialPortManager = null; | ||
} | ||
super.onDestroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<LinearLayout | ||
android:id="@+id/linearLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
|
||
<EditText | ||
android:id="@+id/et_send_content" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:ems="10" | ||
android:gravity="top" | ||
android:hint="请输入发送内容,内容转 byte[] 发送" | ||
android:minLines="5" /> | ||
|
||
<Button | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:onClick="onSend" | ||
android:text="发送" /> | ||
|
||
|
||
<Button | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:onClick="onDestroy" | ||
android:text="注销" /> | ||
|
||
</LinearLayout> | ||
|
||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
serial_lib/src/main/java/com/kongqw/serialportlibrary/ConfigurationSdk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.kongqw.serialportlibrary; | ||
|
||
import androidx.annotation.IntRange; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* 项目:inspection | ||
* 作者:Arry | ||
* 创建日期:2021/8/9 | ||
* 描述: sdk参数配置 | ||
* 修订历史: | ||
*/ | ||
public class ConfigurationSdk { | ||
|
||
private final File device; | ||
private final int baudRate; | ||
|
||
private final byte[] msgHead; | ||
private final byte[] msgTail; | ||
//默认配置 | ||
private final boolean sDebug; | ||
private final boolean sIncludeThread; | ||
private final String sLogType; | ||
|
||
|
||
public ConfigurationSdk(ConfigurationBuilder configurationBuilder) { | ||
this.device = configurationBuilder.device; | ||
this.baudRate = configurationBuilder.baudRate; | ||
this.msgHead = configurationBuilder.msgHead; | ||
this.msgTail = configurationBuilder.msgTail; | ||
this.sDebug = configurationBuilder.sDebug; | ||
this.sIncludeThread = configurationBuilder.sIncludeThread; | ||
this.sLogType = configurationBuilder.sLogType; | ||
} | ||
|
||
public File getDevice() { | ||
return device; | ||
} | ||
|
||
public int getBaudRate() { | ||
return baudRate; | ||
} | ||
|
||
public boolean issDebug() { | ||
return sDebug; | ||
} | ||
|
||
public boolean issIncludeThread() { | ||
return sIncludeThread; | ||
} | ||
|
||
public String getsLogType() { | ||
return sLogType; | ||
} | ||
|
||
public byte[] getMsgHead() { | ||
return msgHead; | ||
} | ||
|
||
public byte[] getMsgTail() { | ||
return msgTail; | ||
} | ||
|
||
public static class ConfigurationBuilder { | ||
private final File device; | ||
private final int baudRate; | ||
private byte[] msgHead; | ||
private byte[] msgTail; | ||
private boolean sDebug = false; | ||
private boolean sIncludeThread = false; | ||
private String sLogType = "inspection"; | ||
|
||
public ConfigurationBuilder(File device, int baudRate) { | ||
this.device = device; | ||
this.baudRate = baudRate; | ||
} | ||
|
||
public ConfigurationBuilder log(String sLogType, boolean sDebug, boolean sIncludeThread) { | ||
this.sLogType = sLogType; | ||
this.sDebug = sDebug; | ||
this.sIncludeThread = sIncludeThread; | ||
return this; | ||
} | ||
public ConfigurationBuilder msgHead(byte[] msgHead) { | ||
this.msgHead = msgHead; | ||
return this; | ||
} | ||
|
||
public ConfigurationBuilder msgTail(byte[] msgTail) { | ||
this.msgTail = msgTail; | ||
return this; | ||
} | ||
|
||
public ConfigurationSdk build() { | ||
return new ConfigurationSdk(this); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.