此应用程序的串行端口上唯一需要更改的是端口名称/编号。JSSC能够像这样更新组合框
private void jComboBoxCommPortFocusGained(java.awt.event.FocusEvent evt) {
SetPortNames();
}
private void SetPortNames() {
jComboBoxCommPort.removeAllItems();
String[] portNames = SerialPortList.getPortNames();
for (String portName : portNames) {
jComboBoxCommPort.addItem(portName);
System.out.println(portName);
}
}
但是行动事件
private void jComboBoxCommPortActionPerformed(java.awt.event.ActionEvent evt) {
InitializeSerialPort(jComboBoxCommPort.getSelectedItem().toString());
}
private void InitializeSerialPort(String port) {
try {
if (serialPort.getPortName().contentEquals(port)) return;
if (serialPort.isOpened()) serialPort.closePort();
serialPort = new SerialPort(port);
serialPort.openPort();
serialPort.setParams(115200, 8, 1, 0);
serialPort.setEventsMask(SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS);
serialPort.addEventListener(new SerialPortReader());
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
破坏组合框并导致此问题
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at stb.serialization.JFrameSerialization.jComboBoxCommPortActionPerformed(JFrameSerialization.java:157)
at stb.serialization.JFrameSerialization.access$300(JFrameSerialization.java:19)
...
更改JSSC串行端口的正确方法是什么?
NullPointerException 是由 null jssc 引起的。串行端口。通过更换修复
static SerialPort serialPort;
跟
static SerialPort serialPort = new SerialPort("");