Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * bem10jfx.blogspot.com
- mabson
- */
- package velocimetrogauge1;
- import java.util.Arrays;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.event.ActionEvent;
- import javafx.event.Event;
- import javafx.event.EventHandler;
- import javafx.geometry.Orientation;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.ChoiceBox;
- import javafx.scene.control.Label;
- import javafx.scene.control.Slider;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.input.ScrollEvent;
- import javafx.scene.layout.StackPane;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- import javafx.stage.WindowEvent;
- import jssc.SerialPort;
- import jssc.SerialPortEvent;
- import jssc.SerialPortException;
- import jssc.SerialPortList;
- /**
- *
- * @author mabson
- */
- public class VelocimetroGauge1 extends Application {
- public SerialPort serialportOne;
- public Label tx_porta = new Label("PORTAS:");
- public ChoiceBox cb_Port = new ChoiceBox();
- public Label tx_Baud = new Label("Velocidade:");
- public ChoiceBox cb_baud = new ChoiceBox();
- public Slider slider = new Slider(0, 260, 0);//vlminimo vlmaximo e inicio do slade_luand
- @Override
- public void start(Stage primaryStage) {
- Button btn = new Button();
- btn.setText("Conectar'");
- cb_baud.getItems().addAll(
- "300", "1200", "2400", "4800", "9600", "14400", "19200",
- "28800", "38400", "57600", "115200", "230400", "250000", "500000", "1000000");
- btn.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- System.out.println("Display!");
- try {
- N_Dispar_PRN();
- } catch (InterruptedException ex) {
- Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SerialPortException ex) {
- Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- VBox vb_Conect = new VBox();
- Group root = new Group();
- vb_Conect.setLayoutX(60);
- vb_Conect.setLayoutY(35);
- root.getChildren().addAll(vb_Conect);
- vb_Conect.getChildren().addAll(tx_porta, cb_Port, tx_Baud, cb_baud, btn);
- vb_Conect.setStyle("-fx-padding:2;"
- + "-fx-border-style: solid inside;"
- + "-fx-border-color:white;");
- Scene scene = new Scene(root, 300, 250, Color.SILVER);
- primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
- @Override
- public void handle(WindowEvent event) {
- try {
- serialportOne.closePort();
- } catch (SerialPortException ex) {
- Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
- }
- Platform.exit();
- }
- });
- primaryStage.setTitle("display");
- primaryStage.setScene(scene);
- cb_Port.setOnMouseClicked(new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent event) {
- LOL();
- }
- });
- primaryStage.show();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }//procurade portas ~~
- public void LOL() {
- cb_Port.getItems().clear();//apaga as portas lidas na entrada
- final String[] serialPortasNomes = SerialPortList.getPortNames();//armazena portas disponivel no momento
- for (String nomeportas : serialPortasNomes) {//armazena portas e incrementa +1 porta disponivei no momento do novo ciclo
- cb_Port.getItems().addAll(nomeportas); //??envia um choiseBOX
- System.out.println("portas disponiveis\n" + nomeportas);//XD
- //--{[>
- }
- }
- //arranque de conexao
- @SuppressWarnings("empty-statement")//sdc v
- public void N_Dispar_PRN() throws InterruptedException, SerialPortException {
- serialportOne = new SerialPort(cb_Port.getValue().toString());
- serialportOne.openPort();
- int baudrater1 = Integer.parseInt(cb_baud.getValue().toString());
- System.out.println("Velocidade de conexao" + baudrater1);
- serialportOne.setParams(baudrater1, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- Arranque();//ARRANQUE DE CONEXAO ...
- }//stage velecimetro LCD1
- public void disparStage() {
- Stage stageD1 = new Stage();
- Group go1 = new Group();
- Scene scened1 = new Scene(go1, 200, 400, Color.DARKORCHID);
- stageD1.setScene(scened1);
- stageD1.setOnCloseRequest((WindowEvent event) -> {
- try {
- serialportOne.closePort();
- } catch (SerialPortException ex) {
- Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
- }
- Platform.exit();
- });
- VBox vb1center = new VBox(10);
- vb1center.setStyle("-fx-padding:2;"+ "-fx-border-style: solid inside;" + "-fx-border-color:white;");
- go1.getChildren().addAll(vb1center);
- vb1center.setLayoutX(60);
- vb1center.setLayoutY(60);
- slider.setOrientation(Orientation.VERTICAL);
- Label lbvlKM = new Label("KM\\h");
- vb1center.getChildren().addAll(tx_Baud, slider, lbvlKM);//childrens
- slider.setOnKeyReleased(new EventHandler<KeyEvent>() {//;; evento de key released :D
- @Override
- public void handle(KeyEvent event) {
- System.out.println(event.getText());
- try {
- System.out.println("velocidade Do Software" + ((int) slider.getValue()));//Parseint
- serialportOne.writeString(((int) slider.getValue()) + "\n");//XD
- lbvlKM.setText(((int) slider.getValue()) + "KM\\h");
- } catch (SerialPortException ex) {
- Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- slider.setOnMouseDragged(new EventHandler<MouseEvent>() {//evento de mouse
- @Override
- public void handle(MouseEvent event) {
- try {
- System.out.println("Velocidade Do Software" + ((int) slider.getValue()));
- serialportOne.writeString(((int) slider.getValue()) + "\n");
- lbvlKM.setText(((int) slider.getValue()) + "KM\\h");
- } catch (SerialPortException ex) {
- Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- stageD1.show();
- }
- public void Arranque() throws SerialPortException {
- if (serialportOne.isOpened()) {
- System.out.println("conectado portas abertas ");
- //iniciando conexao e habilitando stage2
- disparStage();
- //criar um event listerner aqui e corrifigir depois passando para uma classe public nova
- //----------------------------------------------------->>
- serialportOne.addEventListener((SerialPortEvent spe) -> {
- //escuta de input e output
- if (spe.isRXCHAR() && spe.getEventValue() > 0) {
- try {
- byte[] byte_Input = serialportOne.readBytes(spe.getEventValue());
- String str_Byte_input = new String(byte_Input);
- int vlVeloControl = Integer.parseInt(str_Byte_input);
- int vlVeloSoft = Integer.parseInt(slider.toString());
- System.out.println("Velocidade A.U." + Arrays.toString(byte_Input));//preferir colections do jfx
- if (vlVeloControl != vlVeloSoft) { //se a resposta de retorno for diferente do envio enviar novavemte (~~)
- serialportOne.writeString(vlVeloSoft + "\n");
- } else {
- serialportOne.writeString(vlVeloSoft + "\n");
- }
- } catch (SerialPortException e) {
- }
- }
- });
- //----------------------------------------------------->>
- } else {
- System.out.println("Nao conectado Portas fechadas");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment