您尝试将接收到的数据从串口发送到java fx controller
中的标签package application;
import jssc.*;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class MainController {
@FXML
private Label lab;
public static SerialPort serialPort;
public void test(){
serialPort = new SerialPort("COM13");
try {
serialPort.openPort();
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN |
SerialPort.FLOWCONTROL_RTSCTS_OUT);
serialPort.addEventListener(new SerialPortEventListener(){
@Override
public void serialEvent(SerialPortEvent event) {
if(event.isRXCHAR() && event.getEventValue() > 0) {
try {
String s = serialPort.readString(event.getEventValue());
System.out.println("Received response: " + s);
lab.setText(s);
}
catch (SerialPortException ex) {
System.out.println("Error in receiving string from COM-port: " + ex);
}
}
}
}, SerialPort.MASK_RXCHAR);
serialPort.writeString("AT+CUSD=1,\"*222#\",15\r");
}
catch (SerialPortException ex) {
System.out.println("There are an error on writing string to port т: " + ex);
}
}
}
我的fxml文件上有一个按钮,它有onaction test()方法和一个带有lab id的标签 当我按下按钮时,我需要接收到的数据不在控制台上的标签上 我拥有的那个人
Exception in thread "EventThread COM13" Received response:
+CSQ: 20,99
OK
java.lang.IllegalStateException: Not on FX application thread; currentThread = EventThread COM13
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source)
at javafx.scene.Parent$2.onProposedChange(Unknown Source)
at com.sun.javafx.collections.VetoableListDecorator.setAll(Unknown Source)
at com.sun.javafx.collections.VetoableListDecorator.setAll(Unknown Source)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(Unknown Source)
at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(Unknown Source)
at com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(Unknown Source)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(Unknown Source)
at javafx.beans.value.WeakChangeListener.changed(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(Unknown Source)
at javafx.beans.property.StringPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.StringPropertyBase.set(Unknown Source)
at javafx.beans.property.StringPropertyBase.set(Unknown Source)
at javafx.beans.property.StringProperty.setValue(Unknown Source)
at javafx.scene.control.Labeled.setText(Unknown Source)
at application.MainController$1.serialEvent(MainController.java:32)
at jssc.SerialPort$EventThread.run(SerialPort.java:1096)