1

hi i try to to put the received data from serial port to a label in 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);
}
    }
    }

i have a button on my fxml file that have onaction the test() method and a label that have lab id i need when i press the button the received data apear on the label not in the console
the erer that i have

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)
A. Ilyes
  • 17
  • 1
  • 4

0 Answers0