Skip to content

Commit

Permalink
接收数据不完整bug解决 增加粘包数据效验
Browse files Browse the repository at this point in the history
  • Loading branch information
chenli committed Aug 13, 2021
1 parent 24cd0e0 commit 30bbd2b
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 145 deletions.
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".MainActivity2"
android:exported="true"></activity>
<activity android:name=".SelectSerialPortActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".MainActivity">


</activity>
</application>

</manifest>
3 changes: 1 addition & 2 deletions app/src/main/java/com/cl/myapplication/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MainActivity : AppCompatActivity(), OnOpenSerialPortListener {
finish()
return
}
mSerialPortManager = SerialPortManager()


// 打开串口
val openSerialPort = mSerialPortManager!!.setOnOpenSerialPortListener(this)
Expand Down Expand Up @@ -60,7 +60,6 @@ class MainActivity : AppCompatActivity(), OnOpenSerialPortListener {
runOnUiThread { showToast(String.format("发送\n%s", String(bytes))) }
}
})
.openSerialPort(device.file, 115200)

Log.i(
TAG,
Expand Down
147 changes: 147 additions & 0 deletions app/src/main/java/com/cl/myapplication/MainActivity2.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SelectSerialPortActivity : AppCompatActivity(), OnItemClickListener {

override fun onItemClick(p0: AdapterView<*>?, p1: View?, position: Int, p3: Long) {
val device = mDeviceAdapter!!.getItem(position)
val intent = Intent(this, MainActivity::class.java)
val intent = Intent(this, MainActivity2::class.java)
intent.putExtra(DEVICE, device)
startActivity(intent)
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
android:onClick="onSend"
android:text="发送" />


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onDestroy"
android:text="注销" />

</LinearLayout>


Expand Down
39 changes: 39 additions & 0 deletions app/src/main/res/layout/activity_main2.xml
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>
1 change: 1 addition & 0 deletions serial_lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.1.0'
api 'com.github.cl-6666:xlog:V2.0.0'
testImplementation 'junit:junit:4.12'
}
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);
}
}

}
Loading

0 comments on commit 30bbd2b

Please sign in to comment.