|
@@ -9,32 +9,44 @@ import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.nio.charset.Charset;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
+import butterknife.BindView;
|
|
|
+import butterknife.ButterKnife;
|
|
|
import cn.minbb.serial.R;
|
|
|
import cn.minbb.serialport.SerialPortManager;
|
|
|
+import cn.minbb.serialport.listener.OnDataProgressListener;
|
|
|
import cn.minbb.serialport.listener.OnOpenSerialPortListener;
|
|
|
import cn.minbb.serialport.listener.OnSerialPortDataListener;
|
|
|
import cn.minbb.serialport.utils.ByteUtil;
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
|
|
+ @BindView(R.id.open_serial_port)
|
|
|
+ Button openSerial;
|
|
|
+ @BindView(R.id.close_serial_port)
|
|
|
+ Button closeSerial;
|
|
|
+ @BindView(R.id.send_string)
|
|
|
+ Button sendString;
|
|
|
+ @BindView(R.id.send_file)
|
|
|
+ Button sendFile;
|
|
|
+ @BindView(R.id.edit_text)
|
|
|
+ EditText editText;
|
|
|
+ @BindView(R.id.text_view)
|
|
|
+ TextView textView;
|
|
|
+
|
|
|
private String TAG = "MainActivity";
|
|
|
- private Button openSerial, closeSerial;
|
|
|
- private EditText editText;
|
|
|
- private TextView textView;
|
|
|
private SerialPortManager serialPortManager;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_main);
|
|
|
+ ButterKnife.bind(this);
|
|
|
|
|
|
- openSerial = findViewById(R.id.open_serial_port);
|
|
|
- closeSerial = findViewById(R.id.close_serial_port);
|
|
|
closeSerial.setEnabled(false);
|
|
|
- editText = findViewById(R.id.edit_text);
|
|
|
- textView = findViewById(R.id.text_view);
|
|
|
+ sendString.setEnabled(false);
|
|
|
+ sendFile.setEnabled(false);
|
|
|
|
|
|
serialPortManager = new SerialPortManager();
|
|
|
serialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
|
|
@@ -45,7 +57,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
|
|
|
@Override
|
|
|
public void onFail(File device, Status status) {
|
|
|
- Toast.makeText(getApplicationContext(), "串口打开失败", Toast.LENGTH_SHORT).show();
|
|
|
+ Toast.makeText(getApplicationContext(), "串口打开失败:" + status.getDescription(), Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
});
|
|
|
serialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() {
|
|
@@ -53,17 +65,18 @@ public class MainActivity extends AppCompatActivity {
|
|
|
public void onDataReceived(byte[] bytes, String data) {
|
|
|
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();
|
|
|
- }
|
|
|
+// setText(data, data.length());
|
|
|
+ setText(Arrays.toString(bytes), bytes.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();
|
|
|
+// }
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -85,6 +98,8 @@ public class MainActivity extends AppCompatActivity {
|
|
|
Toast.makeText(getApplicationContext(), "串口打开成功", Toast.LENGTH_SHORT).show();
|
|
|
openSerial.setEnabled(false);
|
|
|
closeSerial.setEnabled(true);
|
|
|
+ sendString.setEnabled(true);
|
|
|
+ sendFile.setEnabled(true);
|
|
|
} else {
|
|
|
Toast.makeText(getApplicationContext(), "串口打开失败", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
@@ -94,29 +109,30 @@ public class MainActivity extends AppCompatActivity {
|
|
|
serialPortManager.closeSerialPort();
|
|
|
openSerial.setEnabled(true);
|
|
|
closeSerial.setEnabled(false);
|
|
|
+ sendString.setEnabled(false);
|
|
|
+ sendFile.setEnabled(false);
|
|
|
break;
|
|
|
- case R.id.send:
|
|
|
- // byte[] reportID = {0x0};
|
|
|
- // byte[] data = ByteUtil.addBytes(reportID, editText.getText().toString().getBytes());
|
|
|
- boolean sendBytes = false;
|
|
|
- try {
|
|
|
- sendBytes = serialPortManager.sendString(editText.getText().toString());
|
|
|
- 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();
|
|
|
- }
|
|
|
- break;
|
|
|
- case R.id.clearSend:
|
|
|
+ case R.id.clear_send:
|
|
|
editText.setText("");
|
|
|
break;
|
|
|
- case R.id.clearText:
|
|
|
+ case R.id.clear_text:
|
|
|
textView.setText("");
|
|
|
break;
|
|
|
+ case R.id.send_string:
|
|
|
+ // byte[] reportID = {0x0};
|
|
|
+ // byte[] data = ByteUtil.addBytes(reportID, editText.getText().toString().getBytes());
|
|
|
+ String s = editText.getText().toString();
|
|
|
+ serialPortManager.sendString(s, (progress, index, total, bytes) -> log(Arrays.toString(bytes)));
|
|
|
+ log("发送字符串:" + s);
|
|
|
+ Toast.makeText(getApplicationContext(), "数据已发送", Toast.LENGTH_SHORT).show();
|
|
|
+ break;
|
|
|
+ case R.id.send_file:
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private void log(String log) {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder(textView.getText());
|
|
|
+ textView.setText(stringBuilder.append("\n").append(log));
|
|
|
+ }
|
|
|
}
|