Created
February 28, 2017 13:44
-
-
Save TalusL/5baceb6249d6564183e70b5398428d1c to your computer and use it in GitHub Desktop.
串口助手
描述:对开源库JSSC的GUI封装
作者:
ZiYaoTech
支持平台:
Windows
Linux(x86,64,armhf)
Mac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component name="ArtifactManager"> | |
<artifact type="jar" name="serialPortHelper:jar"> | |
<output-path>$PROJECT_DIR$/out/artifacts/serialPortHelper_jar</output-path> | |
<root id="archive" name="serialPortHelper.jar"> | |
<element id="module-output" name="serialPortHelper" /> | |
<element id="extracted-dir" path="$PROJECT_DIR$/lib/jssc.jar" path-in-jar="/" /> | |
</root> | |
</artifact> | |
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="CompilerConfiguration"> | |
<option name="DEFAULT_COMPILER" value="Javac" /> | |
<resourceExtensions /> | |
<wildcardResourcePatterns> | |
<entry name="!?*.java" /> | |
<entry name="!?*.form" /> | |
<entry name="!?*.class" /> | |
<entry name="!?*.groovy" /> | |
<entry name="!?*.scala" /> | |
<entry name="!?*.flex" /> | |
<entry name="!?*.kt" /> | |
<entry name="!?*.clj" /> | |
</wildcardResourcePatterns> | |
<annotationProcessing> | |
<profile default="true" name="Default" enabled="false"> | |
<processorPath useClasspath="true" /> | |
</profile> | |
</annotationProcessing> | |
</component> | |
</project> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html>Simple <b>JavaFX 2.0</b> application that includes simple .fxml file with attached controller and Main class to quick start. Artifact to build JavaFX application is provided. | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" /> | |
</project> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="GradleSettings"> | |
<option name="gradleHome" value="C:/Program Files (x86)/Gradle/gradle-1.0-rc-3" /> | |
</component> | |
</project> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component name="libraryTable"> | |
<library name="jssc"> | |
<CLASSES> | |
<root url="jar://$PROJECT_DIR$/lib/jssc.jar!/" /> | |
</CLASSES> | |
<JAVADOC /> | |
<SOURCES /> | |
</library> | |
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="EntryPointsManager"> | |
<entry_points version="2.0" /> | |
</component> | |
<component name="ProjectKey"> | |
<option name="state" value="project://e79810c8-c5c8-43b1-b19c-90c1f4095425" /> | |
</component> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/serialPortHelper.iml" filepath="$PROJECT_DIR$/serialPortHelper.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Palette2"> | |
<group name="Swing"> | |
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> | |
</item> | |
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> | |
</item> | |
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> | |
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> | |
<initial-values> | |
<property name="text" value="Button" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="RadioButton" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="CheckBox" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="Label" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> | |
<preferred-size width="200" height="200" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> | |
<preferred-size width="200" height="200" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> | |
</item> | |
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> | |
<preferred-size width="-1" height="20" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> | |
</item> | |
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> | |
</item> | |
</group> | |
</component> | |
</project> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="VcsDirectoryMappings"> | |
<mapping directory="" vcs="" /> | |
</component> | |
</project> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
<orderEntry type="library" name="jssc" level="project" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ziyaotech.serialPortHelper; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Modality; | |
import javafx.stage.Stage; | |
public class AlertBox { | |
public void display(String title , String message){ | |
Stage window = new Stage(); | |
window.setTitle(title); | |
//modality要使用Modality.APPLICATION_MODEL | |
window.initModality(Modality.APPLICATION_MODAL); | |
window.setMinWidth(300); | |
window.setMinHeight(200); | |
Button button = new Button("Close"); | |
button.setOnAction(e -> window.close()); | |
Label label = new Label(message); | |
VBox layout = new VBox(10); | |
layout.getChildren().addAll(label , button); | |
layout.setAlignment(Pos.CENTER); | |
Scene scene = new Scene(layout); | |
window.setScene(scene); | |
//使用showAndWait()先处理这个窗口,而如果不处理,main中的那个窗口不能响应 | |
window.showAndWait(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ziyaotech.serialPortHelper; | |
import javafx.application.Platform; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.*; | |
import jssc.SerialPort; | |
import jssc.SerialPortException; | |
import jssc.SerialPortList; | |
import java.net.URL; | |
import java.util.Date; | |
import java.util.ResourceBundle; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
public class Controller implements Initializable { | |
@FXML ChoiceBox serPort; | |
@FXML ChoiceBox serPortSpeed; | |
@FXML ChoiceBox serPortCheckBit; | |
@FXML ChoiceBox serPortDataBit; | |
@FXML ChoiceBox serPortStopBit; | |
@FXML Button serPortOpenBtn; | |
@FXML CheckBox recvShowHex; | |
@FXML CheckBox recvShowTime; | |
@FXML CheckBox recvStopShow; | |
@FXML Button recvClear; | |
@FXML CheckBox sendHex; | |
@FXML CheckBox sendCycle; | |
@FXML TextField sendCycleRap; | |
@FXML Button sendClear; | |
@FXML Label sendCount; | |
@FXML Label recvCount; | |
@FXML Button CountReset; | |
@FXML TextArea sendTextAear; | |
@FXML TextArea recvTextAear; | |
@FXML Button sendBtn; | |
private static SerialPort serialPort = null; | |
Timer t ; | |
public void about(){ | |
new AlertBox().display("关于","描述:对开源库JSSC的GUI封装\n作者:\nZiYaoTech\n支持平台:\nWindows\nLinux(x86,64,armhf)\nMac"); | |
} | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
//串口号 | |
String[] ports = SerialPortList.getPortNames(); | |
if(ports.length!=0) { | |
for (String s : ports) { | |
serPort.getItems().add(s); | |
} | |
serPort.setValue(ports[0]); | |
} | |
//串口波特率 | |
String[] speeds = new String[]{ | |
"100","300","600","1200","2400","4800","9600","14400","19200","38400","56000","57600","115200","128000","256000" | |
}; | |
for (String s:speeds) { | |
serPortSpeed.getItems().add(s); | |
} | |
serPortSpeed.setValue("9600"); | |
//串口检验位设置 | |
String[] checks = new String[]{ | |
"NONE","ODD","EVEN","MARK","SPACE" | |
}; | |
for (String s:checks) { | |
serPortCheckBit.getItems().add(s); | |
} | |
serPortCheckBit.setValue("NONE"); | |
//数据位设置 | |
String[] databits = new String[]{ | |
"5","6","7","8" | |
}; | |
for (String s:databits) { | |
serPortDataBit.getItems().add(s); | |
} | |
serPortDataBit.setValue("8"); | |
//停止位设置 | |
String[] stopbits = new String[]{ | |
"1","2" | |
}; | |
for (String s:stopbits) { | |
serPortStopBit.getItems().add(s); | |
} | |
serPortStopBit.setValue("1"); | |
serPortOpenBtn.setOnAction((ActionEvent event) -> { | |
if( serialPort!=null&&serialPort.isOpened()) try { | |
serialPort.closePort(); | |
serPortOpenBtn.setText("打开"); | |
serPort.setDisable(false); | |
serPortSpeed.setDisable(false); | |
serPortCheckBit.setDisable(false); | |
serPortDataBit.setDisable(false); | |
serPortStopBit.setDisable(false); | |
return; | |
} catch (SerialPortException e) { | |
new AlertBox().display("关闭串口错误", e.getMessage()); | |
} | |
serialPort = new SerialPort((String) serPort.getValue()); | |
try { | |
serialPort.openPort(); | |
serialPort.setParams( | |
new Integer((String)serPortSpeed.getValue()) , | |
new Integer((String)serPortDataBit.getValue()), | |
new Integer((String)serPortStopBit.getValue()), | |
serPortSpeed.getValue().equals("NONE")? 0: serPortSpeed.getValue().equals("ODD")?1 : | |
serPortSpeed.getValue().equals("EVEN")?2: serPortSpeed.getValue().equals("SPACE")?3: 0); | |
serialPort.purgePort(SerialPort.PURGE_RXCLEAR); | |
serialPort.purgePort(SerialPort.PURGE_TXCLEAR); | |
serialPort.setEventsMask(SerialPort.MASK_RXCHAR); | |
UsartRXEven(); | |
serPortOpenBtn.setText("关闭"); | |
serPort.setDisable(true); | |
serPortSpeed.setDisable(true); | |
serPortCheckBit.setDisable(true); | |
serPortDataBit.setDisable(true); | |
serPortStopBit.setDisable(true); | |
} catch (SerialPortException e) { | |
new AlertBox().display("打开串口错误", e.getMessage()); | |
} | |
}); | |
sendBtn.setOnAction(event -> { | |
if(null==serialPort||(!serialPort.isOpened())){ new AlertBox().display("错误","请先打开串口");return;} | |
try { | |
if(sendHex.isSelected()){ | |
serialPort.writeBytes(hexStringToBytes(sendTextAear.getText())); | |
sendCount.setText(String.valueOf((Integer.parseInt(sendCount.getText())+hexStringToBytes(sendTextAear.getText()).length))); | |
}else{ | |
serialPort.writeBytes(sendTextAear.getText().getBytes()); | |
sendCount.setText(String.valueOf((Integer.parseInt(sendCount.getText())+sendTextAear.getText().getBytes().length))); | |
} | |
} catch (Exception e) { | |
new AlertBox().display("发送数据错误",e.getMessage()); | |
} | |
}); | |
recvClear.setOnAction(event -> { | |
recvTextAear.setText(""); | |
}); | |
sendHex.setOnAction(event -> { | |
if (!sendHex.isSelected()) | |
try { | |
sendTextAear.setText(new String(hexStringToBytes(sendTextAear.getText()))); | |
} catch (Exception e) { | |
new AlertBox().display("非法16进制字符",e.getMessage()); | |
} | |
else | |
sendTextAear.setText(bytesToHexString(sendTextAear.getText().getBytes())); | |
}); | |
sendClear.setOnAction(event -> { | |
sendTextAear.setText(""); | |
}); | |
CountReset.setOnAction(event -> { | |
sendCount.setText("0"); | |
recvCount.setText("0"); | |
}); | |
sendCycle.setOnAction(event -> { | |
if(null==serialPort||(!serialPort.isOpened())){ | |
new AlertBox().display("错误","请先打开串口");sendCycle.setSelected(false);return;} | |
try { | |
if(sendCycle.isSelected()) { | |
sendBtn.setDisable(true); | |
sendCycleRap.setDisable(true); | |
t = new Timer(); | |
byte[] sendData = sendHex.isSelected()?hexStringToBytes(sendTextAear.getText()):sendTextAear.getText().getBytes(); | |
TimerTask task = new TimerTask() { | |
public void run() { | |
// task to run goes here | |
//System.out.println("Hello !!!"); | |
try { | |
serialPort.writeBytes(sendData); | |
Platform.runLater(()-> { | |
sendCount.setText(String.valueOf((Integer.parseInt(sendCount.getText()) + sendData.length))); | |
}); | |
} catch (SerialPortException e) { | |
new AlertBox().display("循环发送错误",e.getMessage()); | |
} | |
} | |
}; | |
t.schedule(task, 0, new Long(sendCycleRap.getText())); | |
}else{ | |
t.cancel(); | |
sendBtn.setDisable(false); | |
sendCycleRap.setDisable(false); | |
} | |
} catch (Exception e) { | |
new AlertBox().display("循环发送错误",e.getMessage()); | |
} | |
}); | |
} | |
public void UsartRXEven(){ | |
try { | |
serialPort.addEventListener(serialPortEvent -> { | |
try { | |
if(recvStopShow.isSelected()) {serialPort.readHexString();return;} | |
byte[] bytes=serialPort.readBytes(); | |
if(bytes!=null) { | |
String str=recvShowHex.isSelected()? bytesToHexString(bytes):(new String((bytes))); | |
if(recvShowTime.isSelected()) str=new Date().toString()+": "+str; | |
String newStr = recvTextAear.getText().isEmpty() ? ("" + str) : (recvTextAear.getText() + "\n" + str); | |
recvTextAear.setText(newStr); | |
recvTextAear.setScrollTop(recvTextAear.getMaxHeight()); | |
Platform.runLater(()->{ | |
recvCount.setText(String.valueOf((Integer.parseInt(recvCount.getText())+bytes.length))); | |
}); | |
} | |
} catch (SerialPortException e) { | |
e.printStackTrace(); | |
} | |
}); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static byte[] hexStringToBytes(String hexString) throws Exception { | |
if (hexString == null || hexString.equals("")) { | |
return null; | |
} | |
hexString=hexString.replace(" ",""); | |
if(hexString.length()%2!=0)hexString="0"+hexString; | |
hexString = hexString.toUpperCase(); | |
int length = hexString.length() / 2; | |
char[] hexChars = hexString.toCharArray(); | |
byte[] d = new byte[length]; | |
for (int i = 0; i < length; i++) { | |
int pos = i * 2; | |
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); | |
} | |
return d; | |
} | |
/** | |
* Convert char to byte | |
* @param c char | |
* @return byte | |
*/ | |
private static byte charToByte(char c) throws Exception { | |
int i = "0123456789ABCDEF".indexOf(c); | |
if(i==-1) throw new Exception("非法的十六进制值"); | |
return (byte) i; | |
} | |
public static String bytesToHexString(byte[] bArray) { | |
if(bArray==null) return null; | |
StringBuffer sb = new StringBuffer(bArray.length); | |
String sTemp; | |
for (int i = 0; i < bArray.length; i++) { | |
sTemp = Integer.toHexString(0xFF & bArray[i]); | |
if (sTemp.length() < 2) | |
sb.append(0); | |
sb.append(sTemp.toUpperCase()); | |
} | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ziyaotech.serialPortHelper; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
import javafx.stage.StageStyle; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("serialPortHelper.fxml")); | |
primaryStage.setTitle("串口助手(跨平台版)"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.initStyle(StageStyle.UTILITY);//设定窗口边框 | |
primaryStage.setResizable(false); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.CheckBox?> | |
<?import javafx.scene.control.ChoiceBox?> | |
<?import javafx.scene.control.Label?> | |
<?import javafx.scene.control.Menu?> | |
<?import javafx.scene.control.MenuBar?> | |
<?import javafx.scene.control.MenuItem?> | |
<?import javafx.scene.control.TextArea?> | |
<?import javafx.scene.control.TextField?> | |
<?import javafx.scene.layout.BorderPane?> | |
<?import javafx.scene.layout.ColumnConstraints?> | |
<?import javafx.scene.layout.GridPane?> | |
<?import javafx.scene.layout.RowConstraints?> | |
<?import javafx.scene.layout.VBox?> | |
<BorderPane prefHeight="505.0" prefWidth="621.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ziyaotech.serialPortHelper.Controller"> | |
<top> | |
<MenuBar BorderPane.alignment="CENTER"> | |
<menus> | |
<Menu mnemonicParsing="false" text="帮助"> | |
<items> | |
<MenuItem mnemonicParsing="false" text="关于" onAction="#about"/> | |
</items> | |
</Menu> | |
</menus> | |
</MenuBar> | |
</top> | |
<left> | |
<VBox prefHeight="516.0" prefWidth="137.0" style="-fx-padding: 5px"> | |
<children> | |
<Label text="串口设置" /> | |
<GridPane style="-fx-padding: 5px;-fx-border-color: darkgrey"> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="80.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
<children> | |
<Label text="串口号:" GridPane.columnIndex="0" /> | |
<ChoiceBox fx:id="serPort" prefWidth="150.0" GridPane.columnIndex="1" /> | |
<Label text="波特率:" GridPane.rowIndex="1" /> | |
<ChoiceBox fx:id="serPortSpeed" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="1" /> | |
<Label text="检验位:" GridPane.rowIndex="2" /> | |
<ChoiceBox fx:id="serPortCheckBit" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="2" /> | |
<Label text="数据位:" GridPane.rowIndex="3" /> | |
<ChoiceBox fx:id="serPortDataBit" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="3" /> | |
<Label text="停止位:" GridPane.rowIndex="4" /> | |
<ChoiceBox fx:id="serPortStopBit" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="4" /> | |
</children> | |
</GridPane> | |
<Button fx:id="serPortOpenBtn" minWidth="130" mnemonicParsing="false" style="-fx-top-margin: 5px" text="打开" /> | |
<Label text="接收区设置" /> | |
<VBox style="-fx-padding: 5px;-fx-border-color: darkgrey"> | |
<children> | |
<CheckBox fx:id="recvShowHex" minHeight="20" mnemonicParsing="false" text="十六进制显示" /> | |
<CheckBox fx:id="recvShowTime" minHeight="20" mnemonicParsing="false" text="显示时间" /> | |
<CheckBox fx:id="recvStopShow" minHeight="20" mnemonicParsing="false" text="暂停接收" /> | |
<GridPane> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
<children> | |
<Button fx:id="recvClear" prefWidth="120" mnemonicParsing="false" text="清除" GridPane.columnIndex="0" /> | |
</children> | |
</GridPane> | |
</children> | |
</VBox> | |
<Label text="发送区设置" /> | |
<VBox prefHeight="118.0" prefWidth="130.0" style="-fx-padding: 5px;-fx-border-color: darkgrey"> | |
<children> | |
<CheckBox fx:id="sendHex" minHeight="20" mnemonicParsing="false" text="十六进制发送" /> | |
<CheckBox fx:id="sendCycle" minHeight="20" mnemonicParsing="false" text="循环发送" /> | |
<GridPane> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="30" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="50.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="50.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
<children> | |
<Label text="间隔" /> | |
<TextField fx:id="sendCycleRap" text="1000" GridPane.columnIndex="1" /> | |
<Label text="毫秒" GridPane.columnIndex="2" /> | |
</children> | |
</GridPane> | |
<GridPane> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
<children> | |
<Button fx:id="sendClear" prefWidth="120" mnemonicParsing="false" text="清除" GridPane.columnIndex="0" /> | |
</children> | |
</GridPane> | |
</children> | |
</VBox> | |
</children> | |
</VBox> | |
</left> | |
<bottom> | |
<GridPane style="-fx-padding: 5px;-fx-border-color: darkgrey;-fx-padding: 5px" BorderPane.alignment="CENTER"> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="20.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="20.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="15.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
<children> | |
<Label text="发送统计:" /> | |
<Label fx:id="sendCount" text="0" GridPane.columnIndex="1" /> | |
<Label text="接收统计" GridPane.columnIndex="2" /> | |
<Label fx:id="recvCount" text="0" GridPane.columnIndex="3" /> | |
<Button fx:id="CountReset" mnemonicParsing="false" text="重置统计" GridPane.columnIndex="4" /> | |
</children> | |
</GridPane> | |
</bottom> | |
<center> | |
<VBox prefHeight="453.0" prefWidth="468.0" style="-fx-padding: 5px" BorderPane.alignment="CENTER"> | |
<children> | |
<Label text="接收区" /> | |
<TextArea fx:id="recvTextAear" editable="false" prefHeight="320.0" prefWidth="200.0" /> | |
<Label text="发送区" /> | |
<GridPane> | |
<columnConstraints> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="350.0" /> | |
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="20.0" /> | |
</columnConstraints> | |
<rowConstraints> | |
<RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" /> | |
</rowConstraints> | |
<children> | |
<TextArea fx:id="sendTextAear" prefHeight="63.0" prefWidth="401.0" /> | |
<Button fx:id="sendBtn" maxWidth="70" mnemonicParsing="false" prefHeight="100" text="发送" GridPane.columnIndex="1" /> | |
</children> | |
</GridPane> | |
</children> | |
</VBox> | |
</center> | |
</BorderPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Manifest-Version: 1.0 | |
Main-Class: com.ziyaotech.serialPortHelper.Main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment