使用JavaFX控制器类+ FXML + jssc(java简单串行连接器)

时间:2016-01-11 09:04:25

标签: javafx arduino fxml jssc

我正在使用带控制器类的JavaFX和FXML和Jssc来控制arduino的简单应用程序。 arduino连接到sg90伺服电机和LED。

我有滑块控制伺服电机的问题。我想使用滑块来控制伺服电机的角度,每当我拖动滑块时,伺服器会不断更新它的角度。

这里是代码工作的控制器类,但问题是我不知道如何实现滑块和伺服的控制。

public class ServoCtrl实现Initializable {

@FXML
private Button IncrBtn = new Button();
@FXML
private Button DecrBtn = new Button();
@FXML
private ToggleButton toggleConnectSerial = new ToggleButton();
@FXML
private ToggleButton lightSwitcH = new ToggleButton();
@FXML
private Slider AngleSlider = new Slider(0, 180, 90);
@FXML
private TextField tfAngle = new TextField();
@FXML
private TextField tfSteps = new TextField();

SerialPort ServoSerialPort;

private int tempstr = 0;

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub
    tfAngle.textProperty().bindBidirectional(AngleSlider.valueProperty(), NumberFormat.getIntegerInstance());

    //disable all port when starting the application
    AngleSlider.setDisable(true);
    lightSwitcH.setDisable(true);
    IncrBtn.setDisable(true);
    DecrBtn.setDisable(true);
    tfAngle.setDisable(true);
    tfSteps.setDisable(true);

}

/*--------- increase or decrease slider value using buttons---------------------------------*/

public void IncrBtnPress(ActionEvent btnIncr) {

    tempstr = Integer.parseInt(tfSteps.getText());
    AngleSlider.setValue(Integer.parseInt(tfAngle.getText()) + tempstr);
    tfAngle.setText(Integer.toString((int) AngleSlider.getValue()));
}

public void DecrBtnPress(ActionEvent btnDecr) {
    tempstr = Integer.parseInt(tfSteps.getText());
    AngleSlider.setValue(Integer.parseInt(tfAngle.getText()) - tempstr);
    tfAngle.setText(Integer.toString((int) AngleSlider.getValue()));

}

/*-----------toggle Switch on light-----------------------------------*/

public void lightSwitch(ActionEvent eve) {
    try {
        if (lightSwitcH.isSelected() == true) {
            lightSwitcH.setText("OFF");

            /*
             * send the following string commands 3 = led number 1 = On, 0 =
             * Off / = seperator - = wait for next command
             * 
             */
            // turn on led
            ServoSerialPort.writeString("3/1-");
        } else {
            // turn off led
            lightSwitcH.setText("ON");
            ServoSerialPort.writeString("3/0-");
        }
    }
     catch (SerialPortException e) {
        e.printStackTrace();
    }
}

/*-------------------- connect or disconnect port-------------------------*/
public void toggleConnect(ActionEvent tgle) {

    if (toggleConnectSerial.isSelected() == true) {

        // enable control when serial port is connected 
        AngleSlider.setDisable(false);
        lightSwitcH.setDisable(false);
        IncrBtn.setDisable(false);
        DecrBtn.setDisable(false);
        tfAngle.setDisable(false);
        tfSteps.setDisable(false);

        // set the port name
        ServoSerialPort = new SerialPort("COM8");
        try {
            // set the toggle button text to disconnect
            toggleConnectSerial.setText("Disconnect");
            // open the serial port
            ServoSerialPort.openPort();
            // set the port parameters
            ServoSerialPort.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);


        } catch (SerialPortException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    } else {
        // disable all control when port is disconnected
        AngleSlider.setDisable(true);
        lightSwitcH.setDisable(true);
        IncrBtn.setDisable(true);
        DecrBtn.setDisable(true);
        tfAngle.setDisable(true);
        tfSteps.setDisable(true);
        toggleConnectSerial.setText("Connect");
        try {
            ServoSerialPort.closePort();
        } catch (SerialPortException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

}

1 个答案:

答案 0 :(得分:0)

您绑定了双向值:

tfAngle.textProperty().bindBidirectional(AngleSlider.valueProperty(), NumberFormat.getIntegerInstance());

表示如果textfieldslider更改,则属性已更新 - 另一个组件会自动更新。因此,IncrBtnPress等方法不应更新这两个值,只需更新一个:slidertextField

如果您想要将新脚踝更新为您的arduino:选择一个属性(例如您的文本域的textProperty())并添加ChangeListener,您可以在其中为您的arduino实施新的脚踝设置器