Skip to content

Commit a6739e1

Browse files
feat(qe): Improve SerialPortTool (#2138)
* feat(qe): Improve SerialPortTool Close #1900 Signed-off-by: IfGuestInDream <ifGuestInDream@163.com> * feat: Improve SerialPortTool visible condition Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * build: Prepare for qe 1.0.1 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * build: Prepare for qe 1.0.1 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> --------- Signed-off-by: IfGuestInDream <ifGuestInDream@163.com> Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> Co-authored-by: unknowIfGuestInDream <liang.tang.cx@gmail.com>
1 parent 175b710 commit a6739e1

File tree

7 files changed

+250
-222
lines changed

7 files changed

+250
-222
lines changed

.idea/artifacts/javafxTool_qe_mac_jar.xml

Lines changed: 57 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/artifacts/javafxTool_qe_win_jar.xml

Lines changed: 57 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/main/java/com/tlcsdm/core/util/DependencyInfo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ private static class SingletonInstance {
232232
"https://javaluator.fathzer.com", "Apache License, Version 2.0",
233233
"https://www.apache.org/licenses/LICENSE-2.0"),
234234

235+
new Dependency("com.fazecast", "jSerialComm", "2.11.0", false,
236+
"https://fazecast.github.io/jSerialComm", "GNU LGPL",
237+
"https://www.gnu.org/licenses/lgpl-3.0.html"),
238+
235239
new Dependency("com.github.gino0631", "icns-core", "1.2", false,
236240
"https://github.com/gino0631/icns", "GNU LGPL",
237241
"https://opensource.org/license/lgpl-3-0"));

qe/src/main/java/com/tlcsdm/qe/tools/SerialPortTool.java

Lines changed: 119 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727

2828
package com.tlcsdm.qe.tools;
2929

30-
import cn.hutool.core.util.StrUtil;
3130
import cn.hutool.log.StaticLog;
3231
import com.fazecast.jSerialComm.SerialPort;
3332
import com.fazecast.jSerialComm.SerialPortDataListener;
3433
import com.fazecast.jSerialComm.SerialPortEvent;
35-
import com.tlcsdm.core.javafx.control.FxTextInput;
3634
import com.tlcsdm.core.javafx.helper.LayoutHelper;
3735
import com.tlcsdm.core.javafx.util.Config;
3836
import com.tlcsdm.core.javafx.util.FxmlUtil;
37+
import com.tlcsdm.core.util.CoreConstant;
3938
import com.tlcsdm.qe.QeSample;
4039
import com.tlcsdm.qe.util.I18nUtils;
40+
import javafx.animation.Timeline;
4141
import javafx.event.ActionEvent;
4242
import javafx.fxml.FXML;
4343
import javafx.fxml.FXMLLoader;
@@ -57,11 +57,8 @@
5757

5858
import java.net.URL;
5959
import java.util.ArrayList;
60-
import java.util.HashMap;
6160
import java.util.List;
62-
import java.util.Map;
6361
import java.util.ResourceBundle;
64-
import java.util.Timer;
6562
import java.util.stream.Collectors;
6663

6764
/**
@@ -72,28 +69,41 @@
7269
*/
7370
public class SerialPortTool extends QeSample implements Initializable {
7471

72+
//串口名称选择器
7573
@FXML
7674
private ComboBox<SerialPort> serPort;
75+
//波特率选择器
7776
@FXML
7877
private ComboBox<String> serPortSpeed;
78+
//检验位选择器
7979
@FXML
8080
private ComboBox<String> serPortCheckBit;
81+
//数据位选择器
8182
@FXML
8283
private ComboBox<String> serPortDataBit;
84+
//停止位选择器
8385
@FXML
8486
private ComboBox<String> serPortStopBit;
87+
//流控选择器
88+
@FXML
89+
private ComboBox<String> serFlowControl;
8590
@FXML
8691
private Button serPortOpenBtn;
92+
//16进制接收显示开关
8793
@FXML
8894
private CheckBox recvShowHex;
95+
//显示时间
8996
@FXML
9097
private CheckBox recvShowTime;
98+
//暂停接收
9199
@FXML
92100
private CheckBox recvStopShow;
93101
@FXML
94102
private Button recvClear;
103+
//16进制发送开关
95104
@FXML
96105
private CheckBox sendHex;
106+
//定时发送开关
97107
@FXML
98108
private CheckBox sendCycle;
99109
@FXML
@@ -113,7 +123,8 @@ public class SerialPortTool extends QeSample implements Initializable {
113123
@FXML
114124
private Button sendBtn;
115125

116-
private Timer t;
126+
private final Timeline circularSending = new Timeline();
127+
private volatile long waitTime = 1000;
117128

118129
@Override
119130
public Node getPanel(Stage stage) {
@@ -149,7 +160,18 @@ public String getSampleDescription() {
149160

150161
@Override
151162
public String getSampleVersion() {
152-
return "1.0.1";
163+
return "1.0.1-Beta";
164+
}
165+
166+
@Override
167+
public boolean hasControlPanel() {
168+
return false;
169+
}
170+
171+
@Override
172+
public boolean isVisible() {
173+
String value = System.getProperty(CoreConstant.JVM_WORKENV);
174+
return CoreConstant.JVM_WORKENV_DEV.equals(value) || CoreConstant.JVM_WORKENV_TEST.equals(value);
153175
}
154176

155177
@Override
@@ -161,33 +183,6 @@ public void initialize(URL location, ResourceBundle resources) {
161183
initializeUI();
162184
}
163185

164-
@Override
165-
public Node getControlPanel() {
166-
String content = """
167-
{note}
168-
{enableMunge}: {enableMungeDesc}
169-
{enableVerbose}: {enableVerboseDesc}
170-
{enableOptimizations}: {enableOptimizationsDesc}
171-
{enablePreserveAllSemiColons}: {enablePreserveAllSemiColonsDesc}
172-
{enableLinebreakpos}: {enableLinebreakposDesc}
173-
""";
174-
175-
Map<String, String> map = new HashMap<>(32);
176-
map.put("note", I18nUtils.get("qe.tool.compress.description.note"));
177-
map.put("enableMunge", I18nUtils.get("qe.tool.compress.check.enableMunge"));
178-
map.put("enableVerbose", I18nUtils.get("qe.tool.compress.check.enableVerbose"));
179-
map.put("enableOptimizations", I18nUtils.get("qe.tool.compress.check.enableOptimizations"));
180-
map.put("enablePreserveAllSemiColons", I18nUtils.get("qe.tool.compress.check.enablePreserveAllSemiColons"));
181-
map.put("enableLinebreakpos", I18nUtils.get("qe.tool.compress.check.enableLinebreakpos"));
182-
map.put("enableMungeDesc", I18nUtils.get("qe.tool.compress.check.enableMunge.description"));
183-
map.put("enableVerboseDesc", I18nUtils.get("qe.tool.compress.check.enableVerbose.description"));
184-
map.put("enableOptimizationsDesc", I18nUtils.get("qe.tool.compress.check.enableOptimizations.description"));
185-
map.put("enablePreserveAllSemiColonsDesc",
186-
I18nUtils.get("qe.tool.compress.check.enablePreserveAllSemiColons.description"));
187-
map.put("enableLinebreakposDesc", I18nUtils.get("qe.tool.compress.check.enableLinebreakpos.description"));
188-
return FxTextInput.textArea(StrUtil.format(content, map));
189-
}
190-
191186
/**
192187
* 设置刷新串口信息按钮被点击时的事件处理器: 重新获取本机当前串口信息
193188
*/
@@ -242,6 +237,12 @@ public ImageView getSampleImageIcon() {
242237
// https://github.com/yiaoBang/SerialPortToolFX/blob/master/src/main/java/com/yiaoBang/serialPortToolFX/view/SerialPortView.java
243238

244239
public void initializeUI() {
240+
//无限循环发送
241+
circularSending.setCycleCount(Timeline.INDEFINITE);
242+
243+
// recvCount
244+
// sendNumber.textProperty().bind(viewModel.getSEND_LONG_PROPERTY().asString());
245+
// receiveNumber.textProperty().bind(viewModel.getRECEIVE_LONG_PROPERTY().asString());
245246

246247
// // 初始化常用波特率列表
247248
// baudRateComboBox.getItems().addAll("9600", "4800", "2400", "1200");
@@ -308,6 +309,28 @@ public void initializeUI() {
308309
}
309310
serPortStopBit.setValue("1");
310311

312+
//流控
313+
// String[] flowControl = new String[]{
314+
// "100", "300", "600", "1200"
315+
// };
316+
// for (String s : speeds) {
317+
// serPortSpeed.getItems().add(s);
318+
// }
319+
// serPortSpeed.setValue("9600");
320+
//serFlowControl.getItems().addAll(SerialPort.FLOW_CONTROL_DISABLED);
321+
322+
//循环发送的等待时间(ms)
323+
sendCycleRap.textProperty().addListener((o, oldValue, newValue) -> {
324+
try {
325+
waitTime = Integer.parseInt(newValue);
326+
if (waitTime < 1) {
327+
waitTime = 1;
328+
}
329+
} catch (NumberFormatException e) {
330+
sendCycleRap.setText(oldValue);
331+
}
332+
});
333+
311334
serPortOpenBtn.setOnAction((ActionEvent event) -> {
312335
SerialPort serialPort = serPort.getSelectionModel().getSelectedItem();
313336
if (serialPort == null) {
@@ -322,6 +345,8 @@ public void initializeUI() {
322345
serPortDataBit.setDisable(false);
323346
serPortStopBit.setDisable(false);
324347
} else {
348+
serialPortDataListener listener = new serialPortDataListener();
349+
serialPort.addDataListener(listener);
325350
serialPort.openPort();
326351
//SerialPort.ONE_STOP_BIT
327352
//SerialPort.NO_PARITY
@@ -333,7 +358,7 @@ public void initializeUI() {
333358
serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
334359
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING | SerialPort.TIMEOUT_WRITE_BLOCKING,
335360
1000, 1000);
336-
//UsartRXEven();
361+
337362
serPortOpenBtn.setText("关闭");
338363
serPort.setDisable(true);
339364
serPortSpeed.setDisable(true);
@@ -342,48 +367,52 @@ public void initializeUI() {
342367
serPortStopBit.setDisable(true);
343368
}
344369
});
345-
//
346-
// sendBtn.setOnAction(event -> {
347-
// if (null == serialPort || (!serialPort.isOpened())) {
348-
// // new AlertBox().display("错误", "请先打开串口");
349-
// return;
350-
// }
351-
// try {
352-
// if (sendHex.isSelected()) {
353-
// serialPort.writeBytes(hexStringToBytes(sendTextAear.getText()));
354-
// sendCount.setText(String.valueOf(
355-
// (Integer.parseInt(sendCount.getText()) + hexStringToBytes(sendTextAear.getText()).length)));
356-
// } else {
357-
// serialPort.writeBytes(sendTextAear.getText().getBytes());
358-
// sendCount.setText(String.valueOf(
359-
// (Integer.parseInt(sendCount.getText()) + sendTextAear.getText().getBytes().length)));
360-
// }
361-
//
362-
// } catch (Exception e) {
363-
// //new AlertBox().display("发送数据错误", e.getMessage());
364-
// }
365-
// });
366-
//
367-
// recvClear.setOnAction(event -> {
368-
// recvTextAear.setText("");
369-
// });
370-
// sendHex.setOnAction(event -> {
371-
// if (!sendHex.isSelected())
372-
// try {
373-
// sendTextAear.setText(new String(hexStringToBytes(sendTextAear.getText())));
374-
// } catch (Exception e) {
375-
// //new AlertBox().display("非法16进制字符", e.getMessage());
376-
// }
377-
// else
378-
// sendTextAear.setText(bytesToHexString(sendTextAear.getText().getBytes()));
379-
// });
380-
// sendClear.setOnAction(event -> {
381-
// sendTextAear.setText("");
382-
// });
383-
// CountReset.setOnAction(event -> {
384-
// sendCount.setText("0");
385-
// recvCount.setText("0");
386-
// });
370+
371+
sendBtn.setOnAction(event -> {
372+
SerialPort serialPort = serPort.getSelectionModel().getSelectedItem();
373+
if (null == serialPort || (!serialPort.isOpen())) {
374+
// new AlertBox().display("错误", "请先打开串口");
375+
return;
376+
}
377+
try {
378+
if (sendHex.isSelected()) {
379+
byte[] bytes = hexStringToBytes(sendTextAear.getText());
380+
serialPort.writeBytes(bytes, bytes.length);
381+
sendCount.setText(String.valueOf(
382+
(Integer.parseInt(sendCount.getText()) + hexStringToBytes(sendTextAear.getText()).length)));
383+
} else {
384+
byte[] bytes = sendTextAear.getText().getBytes();
385+
serialPort.writeBytes(bytes, bytes.length);
386+
sendCount.setText(String.valueOf(
387+
(Integer.parseInt(sendCount.getText()) + sendTextAear.getText().getBytes().length)));
388+
}
389+
390+
} catch (Exception e) {
391+
//new AlertBox().display("发送数据错误", e.getMessage());
392+
}
393+
});
394+
395+
recvClear.setOnAction(event -> {
396+
recvTextAear.setText("");
397+
});
398+
sendHex.setOnAction(event -> {
399+
if (!sendHex.isSelected())
400+
try {
401+
sendTextAear.setText(new String(hexStringToBytes(sendTextAear.getText())));
402+
} catch (Exception e) {
403+
//new AlertBox().display("非法16进制字符", e.getMessage());
404+
}
405+
else {
406+
sendTextAear.setText(bytesToHexString(sendTextAear.getText().getBytes()));
407+
}
408+
});
409+
sendClear.setOnAction(event -> {
410+
sendTextAear.setText("");
411+
});
412+
CountReset.setOnAction(event -> {
413+
sendCount.setText("0");
414+
recvCount.setText("0");
415+
});
387416
// sendCycle.setOnAction(event -> {
388417
// if (null == serialPort || (!serialPort.isOpened())) {
389418
// //new AlertBox().display("错误", "请先打开串口");
@@ -462,6 +491,17 @@ public void initializeUserDataBindings() {
462491
// userData.put("txtCssLinebreakpos", txtCssLinebreakpos);
463492
}
464493

494+
@Override
495+
public void dispose() {
496+
SerialPort serialPort = serPort.getSelectionModel().getSelectedItem();
497+
if (serialPort == null) {
498+
return;
499+
}
500+
if (serialPort.isOpen()) {
501+
serialPort.closePort();
502+
}
503+
}
504+
465505
/**
466506
* 获得当前计算机所有的串口的名称列表
467507
*
@@ -511,30 +551,6 @@ private SerialPort openPort(String portName, int baudRate) {
511551
return serialPort;
512552
}
513553

514-
/**
515-
* 关闭串口
516-
*
517-
* @param serialPort 待关闭的串口对象
518-
*/
519-
private void closePort(SerialPort serialPort) {
520-
if (serialPort != null && serialPort.isOpen()) {
521-
serialPort.closePort();
522-
}
523-
}
524-
525-
/**
526-
* 往串口发送数据
527-
*
528-
* @param serialPort 串口对象
529-
* @param content 待发送数据
530-
*/
531-
private void sendToPort(SerialPort serialPort, byte[] content) {
532-
if (!serialPort.isOpen()) {
533-
return;
534-
}
535-
serialPort.writeBytes(content, content.length);
536-
}
537-
538554
/**
539555
* 从串口读取数据
540556
*
@@ -573,8 +589,9 @@ public int getListeningEvents() {
573589
public void serialEvent(SerialPortEvent event) {
574590
byte[] newData = event.getReceivedData();
575591
System.out.println("Received data of size: " + newData.length);
576-
for (int i = 0; i < newData.length; ++i)
592+
for (int i = 0; i < newData.length; ++i) {
577593
System.out.print((char) newData[i]);
594+
}
578595
System.out.println("\n");
579596
}
580597
}

qe/src/main/java/com/tlcsdm/qe/util/QeConstant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class QeConstant {
8181
/**
8282
* 项目构建日期
8383
*/
84-
public static final String PROJECT_BUILD_DAY = "2023-10-02";
84+
public static final String PROJECT_BUILD_DAY = "2025-05-11";
8585
/**
8686
* 检查更新所用的api
8787
*/
@@ -96,7 +96,7 @@ public class QeConstant {
9696
*/
9797
public static final List<String> DEPENDENCY_LIST = List.of("poi", "freemarker", "dom4j", "java-diff-utils",
9898
"richtextfx", "thumbnailator", "jackson", "yuicompressor", "teenyhttpd", "image4j", "commons-imaging",
99-
"icns-core", "preferencesfx");
99+
"icns-core", "preferencesfx", "jSerialComm");
100100

101101
private QeConstant() {
102102
}

qe/src/main/resources/META-INF/MANIFEST.MF

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Implementation-Version: 1.0.1
44
Built-By: unknowIfGuestInDream
55
Build-Os: Windows 11
66
Build-Jdk-Spec: 17
7-
Build-Jdk: Eclipse Temurin 17.0.9
8-
Created-By: IntelliJ IDEA 2023.3.1
9-
Build-Day: 2023-10-02
7+
Build-Jdk: Eclipse Temurin 17.0.15
8+
Created-By: IntelliJ IDEA 2023.3.2.2
9+
Build-Day: 2025-05-11

0 commit comments

Comments
 (0)