commit 9621c9d1e9d39cdee873c5dba11d69d0ae9585f8 Author: 云海梦尘 <2523208472@qq.com> Date: Tue Jun 7 14:35:18 2022 +0800 init diff --git a/down/RXTXcomm.jar b/down/RXTXcomm.jar new file mode 100644 index 0000000..e1e7503 Binary files /dev/null and b/down/RXTXcomm.jar differ diff --git a/down/rxtxParallel.dll b/down/rxtxParallel.dll new file mode 100644 index 0000000..92666dd Binary files /dev/null and b/down/rxtxParallel.dll differ diff --git a/down/rxtxSerial.dll b/down/rxtxSerial.dll new file mode 100644 index 0000000..211e006 Binary files /dev/null and b/down/rxtxSerial.dll differ diff --git a/lib/RXTXcomm.jar b/lib/RXTXcomm.jar new file mode 100644 index 0000000..e1e7503 Binary files /dev/null and b/lib/RXTXcomm.jar differ diff --git a/read.md b/read.md new file mode 100644 index 0000000..54f4f7a --- /dev/null +++ b/read.md @@ -0,0 +1,2 @@ +### Java读取串口数据测试 +![](https://www.lshserver.com:9006/super-lamps/20220607143441.png) diff --git a/src/sample/Controller.java b/src/sample/Controller.java new file mode 100644 index 0000000..d1d9d6d --- /dev/null +++ b/src/sample/Controller.java @@ -0,0 +1,98 @@ +package sample; + +import gnu.io.SerialPort; +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; +import sample.utils.*; + +import java.util.ArrayList; + + +public class Controller { + + @FXML + Button writeButton; + @FXML + TextArea readTextarea; + @FXML + TextArea writeTextarea; + @FXML + TextField comName; + // 串口 + SerialPort port = null; + + /** 开启串口 + * + */ + @FXML + public void startCom() { + if(port != null){ + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "串口已经打开:" + port.getName()); + alert.show(); + return; + } + ArrayList findPorts = SerialTool.findPort(); + if(findPorts.isEmpty()){ + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "未找到串口信息!"); + alert.show(); + return; + } + String portName = findPorts.get(0);//默认打开第一个串口 + // 设置使用的串口 + comName.setText(portName); + try { + port = SerialTool.openPort(portName, 9600);//打开串口 + } catch (RuntimeException e){ + Alert alert = new Alert(Alert.AlertType.ERROR, e.getMessage()); + alert.show(); + return; + } + + SerialTool.addListener(port, () -> { + byte[] data = null; + try { + if (port == null) { + System.out.println("串口对象为空,监听失败!"); + } else { + // 读取串口数据 + data = SerialTool.readFromPort(port); + String resultHEX = SerialTool.byteArrayToHexString(data); + readTextarea.setText(resultHEX); + } + } catch (Exception e) { + Alert alert = new Alert(Alert.AlertType.ERROR, "发生了一个异常:".concat(e.getMessage())); + alert.show(); + } + }); + + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "串口开启成功!"); + alert.show(); + } + + /** 写入串口数据 + * + */ + @FXML + public void writeCom() { + if(port == null){ + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "请先开启串口!"); + alert.show(); + return; + } + + String content = writeTextarea.getText(); + if(content == null || content.trim().length() <=0){ + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "请填写写入的数据!"); + alert.show(); + return; + } + //设定发送字符串 + byte[] bs = SerialTool.hex2Bytes(content); + SerialTool.sendToPort(port, bs);//写入,写入应该在监听器打开之后而不是之前 + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "写入成功!"); + alert.show(); + } +} diff --git a/src/sample/Main.java b/src/sample/Main.java new file mode 100644 index 0000000..ca74bdc --- /dev/null +++ b/src/sample/Main.java @@ -0,0 +1,23 @@ +package sample; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class Main extends Application { + + @Override + public void start(Stage primaryStage) throws Exception{ + Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); + primaryStage.setTitle("串口测试工具"); + primaryStage.setScene(new Scene(root, 550, 360)); + primaryStage.show(); + } + + + public static void main(String[] args) { + launch(args); + } +} diff --git a/src/sample/sample.fxml b/src/sample/sample.fxml new file mode 100644 index 0000000..4817905 --- /dev/null +++ b/src/sample/sample.fxml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + +