27
27
28
28
package com .tlcsdm .qe .tools ;
29
29
30
- import cn .hutool .core .util .StrUtil ;
31
30
import cn .hutool .log .StaticLog ;
32
31
import com .fazecast .jSerialComm .SerialPort ;
33
32
import com .fazecast .jSerialComm .SerialPortDataListener ;
34
33
import com .fazecast .jSerialComm .SerialPortEvent ;
35
- import com .tlcsdm .core .javafx .control .FxTextInput ;
36
34
import com .tlcsdm .core .javafx .helper .LayoutHelper ;
37
35
import com .tlcsdm .core .javafx .util .Config ;
38
36
import com .tlcsdm .core .javafx .util .FxmlUtil ;
39
37
import com .tlcsdm .qe .QeSample ;
40
38
import com .tlcsdm .qe .util .I18nUtils ;
39
+ import javafx .animation .Timeline ;
41
40
import javafx .event .ActionEvent ;
42
41
import javafx .fxml .FXML ;
43
42
import javafx .fxml .FXMLLoader ;
57
56
58
57
import java .net .URL ;
59
58
import java .util .ArrayList ;
60
- import java .util .HashMap ;
61
59
import java .util .List ;
62
- import java .util .Map ;
63
60
import java .util .ResourceBundle ;
64
- import java .util .Timer ;
65
61
import java .util .stream .Collectors ;
66
62
67
63
/**
72
68
*/
73
69
public class SerialPortTool extends QeSample implements Initializable {
74
70
71
+ //串口名称选择器
75
72
@ FXML
76
73
private ComboBox <SerialPort > serPort ;
74
+ //波特率选择器
77
75
@ FXML
78
76
private ComboBox <String > serPortSpeed ;
77
+ //检验位选择器
79
78
@ FXML
80
79
private ComboBox <String > serPortCheckBit ;
80
+ //数据位选择器
81
81
@ FXML
82
82
private ComboBox <String > serPortDataBit ;
83
+ //停止位选择器
83
84
@ FXML
84
85
private ComboBox <String > serPortStopBit ;
86
+ //流控选择器
87
+ @ FXML
88
+ private ComboBox <String > serFlowControl ;
85
89
@ FXML
86
90
private Button serPortOpenBtn ;
91
+ //16进制接收显示开关
87
92
@ FXML
88
93
private CheckBox recvShowHex ;
94
+ //显示时间
89
95
@ FXML
90
96
private CheckBox recvShowTime ;
97
+ //暂停接收
91
98
@ FXML
92
99
private CheckBox recvStopShow ;
93
100
@ FXML
94
101
private Button recvClear ;
102
+ //16进制发送开关
95
103
@ FXML
96
104
private CheckBox sendHex ;
105
+ //定时发送开关
97
106
@ FXML
98
107
private CheckBox sendCycle ;
99
108
@ FXML
@@ -113,7 +122,8 @@ public class SerialPortTool extends QeSample implements Initializable {
113
122
@ FXML
114
123
private Button sendBtn ;
115
124
116
- private Timer t ;
125
+ private final Timeline circularSending = new Timeline ();
126
+ private volatile long waitTime = 1000 ;
117
127
118
128
@ Override
119
129
public Node getPanel (Stage stage ) {
@@ -152,6 +162,11 @@ public String getSampleVersion() {
152
162
return "1.0.1" ;
153
163
}
154
164
165
+ @ Override
166
+ public boolean hasControlPanel () {
167
+ return false ;
168
+ }
169
+
155
170
@ Override
156
171
public void initialize (URL location , ResourceBundle resources ) {
157
172
initializeUserDataBindings ();
@@ -161,33 +176,6 @@ public void initialize(URL location, ResourceBundle resources) {
161
176
initializeUI ();
162
177
}
163
178
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
-
191
179
/**
192
180
* 设置刷新串口信息按钮被点击时的事件处理器: 重新获取本机当前串口信息
193
181
*/
@@ -242,6 +230,12 @@ public ImageView getSampleImageIcon() {
242
230
// https://github.com/yiaoBang/SerialPortToolFX/blob/master/src/main/java/com/yiaoBang/serialPortToolFX/view/SerialPortView.java
243
231
244
232
public void initializeUI () {
233
+ //无限循环发送
234
+ circularSending .setCycleCount (Timeline .INDEFINITE );
235
+
236
+ // recvCount
237
+ // sendNumber.textProperty().bind(viewModel.getSEND_LONG_PROPERTY().asString());
238
+ // receiveNumber.textProperty().bind(viewModel.getRECEIVE_LONG_PROPERTY().asString());
245
239
246
240
// // 初始化常用波特率列表
247
241
// baudRateComboBox.getItems().addAll("9600", "4800", "2400", "1200");
@@ -308,6 +302,28 @@ public void initializeUI() {
308
302
}
309
303
serPortStopBit .setValue ("1" );
310
304
305
+ //流控
306
+ // String[] flowControl = new String[]{
307
+ // "100", "300", "600", "1200"
308
+ // };
309
+ // for (String s : speeds) {
310
+ // serPortSpeed.getItems().add(s);
311
+ // }
312
+ // serPortSpeed.setValue("9600");
313
+ //serFlowControl.getItems().addAll(SerialPort.FLOW_CONTROL_DISABLED);
314
+
315
+ //循环发送的等待时间(ms)
316
+ sendCycleRap .textProperty ().addListener ((o , oldValue , newValue ) -> {
317
+ try {
318
+ waitTime = Integer .parseInt (newValue );
319
+ if (waitTime < 1 ) {
320
+ waitTime = 1 ;
321
+ }
322
+ } catch (NumberFormatException e ) {
323
+ sendCycleRap .setText (oldValue );
324
+ }
325
+ });
326
+
311
327
serPortOpenBtn .setOnAction ((ActionEvent event ) -> {
312
328
SerialPort serialPort = serPort .getSelectionModel ().getSelectedItem ();
313
329
if (serialPort == null ) {
@@ -322,6 +338,8 @@ public void initializeUI() {
322
338
serPortDataBit .setDisable (false );
323
339
serPortStopBit .setDisable (false );
324
340
} else {
341
+ serialPortDataListener listener = new serialPortDataListener ();
342
+ serialPort .addDataListener (listener );
325
343
serialPort .openPort ();
326
344
//SerialPort.ONE_STOP_BIT
327
345
//SerialPort.NO_PARITY
@@ -333,7 +351,7 @@ public void initializeUI() {
333
351
serialPort .setFlowControl (SerialPort .FLOW_CONTROL_DISABLED );
334
352
serialPort .setComPortTimeouts (SerialPort .TIMEOUT_READ_BLOCKING | SerialPort .TIMEOUT_WRITE_BLOCKING ,
335
353
1000 , 1000 );
336
- //UsartRXEven();
354
+
337
355
serPortOpenBtn .setText ("关闭" );
338
356
serPort .setDisable (true );
339
357
serPortSpeed .setDisable (true );
@@ -342,48 +360,52 @@ public void initializeUI() {
342
360
serPortStopBit .setDisable (true );
343
361
}
344
362
});
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
- // });
363
+
364
+ sendBtn .setOnAction (event -> {
365
+ SerialPort serialPort = serPort .getSelectionModel ().getSelectedItem ();
366
+ if (null == serialPort || (!serialPort .isOpen ())) {
367
+ // new AlertBox().display("错误", "请先打开串口");
368
+ return ;
369
+ }
370
+ try {
371
+ if (sendHex .isSelected ()) {
372
+ byte [] bytes = hexStringToBytes (sendTextAear .getText ());
373
+ serialPort .writeBytes (bytes , bytes .length );
374
+ sendCount .setText (String .valueOf (
375
+ (Integer .parseInt (sendCount .getText ()) + hexStringToBytes (sendTextAear .getText ()).length )));
376
+ } else {
377
+ byte [] bytes = sendTextAear .getText ().getBytes ();
378
+ serialPort .writeBytes (bytes , bytes .length );
379
+ sendCount .setText (String .valueOf (
380
+ (Integer .parseInt (sendCount .getText ()) + sendTextAear .getText ().getBytes ().length )));
381
+ }
382
+
383
+ } catch (Exception e ) {
384
+ //new AlertBox().display("发送数据错误", e.getMessage());
385
+ }
386
+ });
387
+
388
+ recvClear .setOnAction (event -> {
389
+ recvTextAear .setText ("" );
390
+ });
391
+ sendHex .setOnAction (event -> {
392
+ if (!sendHex .isSelected ())
393
+ try {
394
+ sendTextAear .setText (new String (hexStringToBytes (sendTextAear .getText ())));
395
+ } catch (Exception e ) {
396
+ //new AlertBox().display("非法16进制字符", e.getMessage());
397
+ }
398
+ else {
399
+ sendTextAear .setText (bytesToHexString (sendTextAear .getText ().getBytes ()));
400
+ }
401
+ });
402
+ sendClear .setOnAction (event -> {
403
+ sendTextAear .setText ("" );
404
+ });
405
+ CountReset .setOnAction (event -> {
406
+ sendCount .setText ("0" );
407
+ recvCount .setText ("0" );
408
+ });
387
409
// sendCycle.setOnAction(event -> {
388
410
// if (null == serialPort || (!serialPort.isOpened())) {
389
411
// //new AlertBox().display("错误", "请先打开串口");
@@ -462,6 +484,17 @@ public void initializeUserDataBindings() {
462
484
// userData.put("txtCssLinebreakpos", txtCssLinebreakpos);
463
485
}
464
486
487
+ @ Override
488
+ public void dispose () {
489
+ SerialPort serialPort = serPort .getSelectionModel ().getSelectedItem ();
490
+ if (serialPort == null ) {
491
+ return ;
492
+ }
493
+ if (serialPort .isOpen ()) {
494
+ serialPort .closePort ();
495
+ }
496
+ }
497
+
465
498
/**
466
499
* 获得当前计算机所有的串口的名称列表
467
500
*
@@ -511,30 +544,6 @@ private SerialPort openPort(String portName, int baudRate) {
511
544
return serialPort ;
512
545
}
513
546
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
-
538
547
/**
539
548
* 从串口读取数据
540
549
*
@@ -573,8 +582,9 @@ public int getListeningEvents() {
573
582
public void serialEvent (SerialPortEvent event ) {
574
583
byte [] newData = event .getReceivedData ();
575
584
System .out .println ("Received data of size: " + newData .length );
576
- for (int i = 0 ; i < newData .length ; ++i )
585
+ for (int i = 0 ; i < newData .length ; ++i ) {
577
586
System .out .print ((char ) newData [i ]);
587
+ }
578
588
System .out .println ("\n " );
579
589
}
580
590
}
0 commit comments