|
@@ -9,6 +9,7 @@ import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.nio.charset.Charset;
|
|
|
|
|
|
import cn.minbb.serial.R;
|
|
|
import cn.minbb.serialport.SerialPortManager;
|
|
@@ -23,7 +24,6 @@ public class MainActivity extends AppCompatActivity {
|
|
|
private EditText editText;
|
|
|
private TextView textView;
|
|
|
private SerialPortManager serialPortManager;
|
|
|
- private static String data;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
@@ -36,25 +36,6 @@ public class MainActivity extends AppCompatActivity {
|
|
|
editText = findViewById(R.id.edit_text);
|
|
|
textView = findViewById(R.id.text_view);
|
|
|
|
|
|
-// serialPortUtil = new SerialPortUtil(getApplicationContext());
|
|
|
-// serialPortUtil.setOnDataReceiveListener(new SerialPortUtil.OnDataReceiveListener() {
|
|
|
-// @Override
|
|
|
-// public void onDataReceive(final String data, final int size) {
|
|
|
-// runOnUiThread(new Runnable() {
|
|
|
-// @Override
|
|
|
-// public void run() {
|
|
|
-// setText(data, size);
|
|
|
-// try {
|
|
|
-// serialPortUtil.sendSerialPort(data.getBytes(Charset.forName("UTF-8")));
|
|
|
-// } catch (IOException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// Toast.makeText(getApplicationContext(), "发送失败 = " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// });
|
|
|
-// }
|
|
|
-// });
|
|
|
-
|
|
|
serialPortManager = new SerialPortManager();
|
|
|
serialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
|
|
|
@Override
|
|
@@ -70,20 +51,18 @@ public class MainActivity extends AppCompatActivity {
|
|
|
serialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() {
|
|
|
@Override
|
|
|
public void onDataReceived(byte[] bytes, String data) {
|
|
|
- MainActivity.data = data;
|
|
|
- runOnUiThread(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- byte[] bytes = ByteUtil.addBytes(MainActivity.data.getBytes(), new byte[64]);
|
|
|
-// for (int i = bytes.length; i < 64; i++) {
|
|
|
-// bytes[i] = 0x0;
|
|
|
-// }
|
|
|
- setText(MainActivity.data, MainActivity.data.length());
|
|
|
- if (serialPortManager.sendBytes(bytes)) {
|
|
|
- Toast.makeText(getApplicationContext(), "发送成功 = " + MainActivity.data, Toast.LENGTH_SHORT).show();
|
|
|
+ runOnUiThread(() -> {
|
|
|
+ byte[] bytes1 = ByteUtil.addBytes(data.getBytes(), new byte[64]);
|
|
|
+ setText(data, data.length());
|
|
|
+ try {
|
|
|
+ if (serialPortManager.sendBytes(bytes1)) {
|
|
|
+ Toast.makeText(getApplicationContext(), "发送成功 = " + data, Toast.LENGTH_SHORT).show();
|
|
|
} else {
|
|
|
Toast.makeText(getApplicationContext(), "发送失败", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ Toast.makeText(getApplicationContext(), "发送失败 " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -102,15 +81,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
public void onClick(View view) {
|
|
|
switch (view.getId()) {
|
|
|
case R.id.open_serial_port:
|
|
|
-// if (serialPortUtil.serialPortStatus) {
|
|
|
-// Toast.makeText(getApplicationContext(), "串口已经打开", Toast.LENGTH_SHORT).show();
|
|
|
-// } else {
|
|
|
-// if (null != serialPortUtil.openSerialPort()) {
|
|
|
-// openSerial.setEnabled(false);
|
|
|
-// closeSerial.setEnabled(true);
|
|
|
-// }
|
|
|
-// }
|
|
|
- if (serialPortManager.openSerialPort(new File("/dev/ttyS1"), 115200)) {
|
|
|
+ if (serialPortManager.openSerialPort()) {
|
|
|
Toast.makeText(getApplicationContext(), "串口打开成功", Toast.LENGTH_SHORT).show();
|
|
|
openSerial.setEnabled(false);
|
|
|
closeSerial.setEnabled(true);
|
|
@@ -119,12 +90,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
break;
|
|
|
case R.id.close_serial_port:
|
|
|
-// if (serialPortUtil.serialPortStatus) {
|
|
|
-// serialPortUtil.closeSerialPort();
|
|
|
-// openSerial.setEnabled(true);
|
|
|
-// closeSerial.setEnabled(false);
|
|
|
-// }
|
|
|
-// Toast.makeText(getApplicationContext(), "串口已关闭", Toast.LENGTH_SHORT).show();
|
|
|
+ Toast.makeText(getApplicationContext(), "串口已关闭", Toast.LENGTH_SHORT).show();
|
|
|
serialPortManager.closeSerialPort();
|
|
|
openSerial.setEnabled(true);
|
|
|
closeSerial.setEnabled(false);
|
|
@@ -132,23 +98,18 @@ public class MainActivity extends AppCompatActivity {
|
|
|
case R.id.send:
|
|
|
// byte[] reportID = {0x0};
|
|
|
// byte[] data = ByteUtil.addBytes(reportID, editText.getText().toString().getBytes());
|
|
|
- boolean sendBytes = serialPortManager.sendBytes(editText.getText().toString().getBytes());
|
|
|
- if (sendBytes) {
|
|
|
- Toast.makeText(getApplicationContext(), "发送成功", Toast.LENGTH_SHORT).show();
|
|
|
- } else {
|
|
|
- Toast.makeText(getApplicationContext(), "发送失败", Toast.LENGTH_SHORT).show();
|
|
|
+ boolean sendBytes = false;
|
|
|
+ try {
|
|
|
+ sendBytes = serialPortManager.sendBytes(editText.getText().toString().getBytes(Charset.forName("UTF-8")));
|
|
|
+ if (sendBytes) {
|
|
|
+ Toast.makeText(getApplicationContext(), "数据发送成功", Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+ Toast.makeText(getApplicationContext(), "数据发送失败", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ Toast.makeText(getApplicationContext(), "数据发送失败 " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
-// if (serialPortUtil.serialPortStatus) {
|
|
|
-// try {
|
|
|
-// serialPortUtil.sendSerialPort(editText.getText().toString());
|
|
|
-// } catch (IOException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// Log.e(TAG, "sendSerialPort: 串口数据发送失败:" + e.toString());
|
|
|
-// Toast.makeText(getApplicationContext(), "数据发送失败:" + e.toString(), Toast.LENGTH_SHORT).show();
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// Toast.makeText(getApplicationContext(), "请先打开串口", Toast.LENGTH_SHORT).show();
|
|
|
-// }
|
|
|
break;
|
|
|
case R.id.clearSend:
|
|
|
editText.setText("");
|