kalibre

virtualsoftpotenciometro

Mar 17th, 2024
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.89 KB | Software | 0 0
  1. /*
  2.  * bem10jfx.blogspot.com
  3. mabson
  4.  */
  5. package velocimetrogauge1;
  6.  
  7. import java.util.Arrays;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import javafx.application.Application;
  11. import javafx.application.Platform;
  12. import javafx.event.ActionEvent;
  13. import javafx.event.Event;
  14. import javafx.event.EventHandler;
  15. import javafx.geometry.Orientation;
  16. import javafx.scene.Group;
  17. import javafx.scene.Scene;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.ChoiceBox;
  20. import javafx.scene.control.Label;
  21. import javafx.scene.control.Slider;
  22. import javafx.scene.input.KeyEvent;
  23. import javafx.scene.input.MouseEvent;
  24. import javafx.scene.input.ScrollEvent;
  25. import javafx.scene.layout.StackPane;
  26. import javafx.scene.layout.VBox;
  27. import javafx.scene.paint.Color;
  28. import javafx.stage.Stage;
  29. import javafx.stage.WindowEvent;
  30. import jssc.SerialPort;
  31. import jssc.SerialPortEvent;
  32. import jssc.SerialPortException;
  33. import jssc.SerialPortList;
  34.  
  35. /**
  36.  *
  37.  * @author mabson
  38.  */
  39. public class VelocimetroGauge1 extends Application {
  40.  
  41.     public SerialPort serialportOne;
  42.     public Label tx_porta = new Label("PORTAS:");
  43.     public ChoiceBox cb_Port = new ChoiceBox();
  44.     public Label tx_Baud = new Label("Velocidade:");
  45.     public ChoiceBox cb_baud = new ChoiceBox();
  46.     public Slider slider = new Slider(0, 260, 0);//vlminimo vlmaximo e inicio do slade_luand
  47.  
  48.     @Override
  49.     public void start(Stage primaryStage) {
  50.         Button btn = new Button();
  51.         btn.setText("Conectar'");
  52.  
  53.         cb_baud.getItems().addAll(
  54.                 "300", "1200", "2400", "4800", "9600", "14400", "19200",
  55.                 "28800", "38400", "57600", "115200", "230400", "250000", "500000", "1000000");
  56.  
  57.         btn.setOnAction(new EventHandler<ActionEvent>() {
  58.  
  59.             @Override
  60.             public void handle(ActionEvent event) {
  61.                 System.out.println("Display!");
  62.  
  63.                 try {
  64.                     N_Dispar_PRN();
  65.                 } catch (InterruptedException ex) {
  66.                     Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
  67.                 } catch (SerialPortException ex) {
  68.                     Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
  69.                 }
  70.  
  71.             }
  72.         });
  73.  
  74.         VBox vb_Conect = new VBox();
  75.         Group root = new Group();
  76.         vb_Conect.setLayoutX(60);
  77.         vb_Conect.setLayoutY(35);
  78.         root.getChildren().addAll(vb_Conect);
  79.         vb_Conect.getChildren().addAll(tx_porta, cb_Port, tx_Baud, cb_baud, btn);
  80.  
  81.         vb_Conect.setStyle("-fx-padding:2;"
  82.                 + "-fx-border-style: solid inside;"
  83.                 + "-fx-border-color:white;");
  84.         Scene scene = new Scene(root, 300, 250, Color.SILVER);
  85.         primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
  86.             @Override
  87.             public void handle(WindowEvent event) {
  88.                 try {
  89.                     serialportOne.closePort();
  90.                 } catch (SerialPortException ex) {
  91.                     Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
  92.                 }
  93.                 Platform.exit();
  94.  
  95.             }
  96.         });
  97.         primaryStage.setTitle("display");
  98.         primaryStage.setScene(scene);
  99.  
  100.         cb_Port.setOnMouseClicked(new EventHandler<MouseEvent>() {
  101.             @Override
  102.             public void handle(MouseEvent event) {
  103.                 LOL();
  104.             }
  105.         });
  106.         primaryStage.show();
  107.     }
  108.  
  109.     /**
  110.      * @param args the command line arguments
  111.      */
  112.     public static void main(String[] args) {
  113.         launch(args);
  114.     }//procurade portas ~~
  115.  
  116.     public void LOL() {
  117.         cb_Port.getItems().clear();//apaga as portas lidas na entrada
  118.         final String[] serialPortasNomes = SerialPortList.getPortNames();//armazena portas disponivel no momento
  119.         for (String nomeportas : serialPortasNomes) {//armazena  portas e incrementa +1 porta disponivei no momento do novo ciclo
  120.             cb_Port.getItems().addAll(nomeportas); //??envia um choiseBOX
  121.             System.out.println("portas disponiveis\n" + nomeportas);//XD
  122.             //--{[>
  123.         }
  124.     }
  125.     //arranque de conexao
  126.  
  127.     @SuppressWarnings("empty-statement")//sdc v
  128.     public void N_Dispar_PRN() throws InterruptedException, SerialPortException {
  129.         serialportOne = new SerialPort(cb_Port.getValue().toString());
  130.         serialportOne.openPort();
  131.         int baudrater1 = Integer.parseInt(cb_baud.getValue().toString());
  132.         System.out.println("Velocidade de conexao" + baudrater1);
  133.         serialportOne.setParams(baudrater1, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  134.         Arranque();//ARRANQUE DE CONEXAO ...
  135.  
  136.     }//stage velecimetro LCD1
  137.  
  138.     public void disparStage() {
  139.  
  140.         Stage stageD1 = new Stage();
  141.         Group go1 = new Group();
  142.         Scene scened1 = new Scene(go1, 200, 400, Color.DARKORCHID);
  143.         stageD1.setScene(scened1);
  144.         stageD1.setOnCloseRequest((WindowEvent event) -> {
  145.             try {
  146.                 serialportOne.closePort();
  147.             } catch (SerialPortException ex) {
  148.                 Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
  149.             }
  150.             Platform.exit();
  151.         });
  152.  
  153.         VBox vb1center = new VBox(10);
  154.        
  155.         vb1center.setStyle("-fx-padding:2;"+ "-fx-border-style: solid inside;" + "-fx-border-color:white;");
  156.         go1.getChildren().addAll(vb1center);
  157.         vb1center.setLayoutX(60);
  158.         vb1center.setLayoutY(60);
  159.         slider.setOrientation(Orientation.VERTICAL);
  160.         Label lbvlKM = new Label("KM\\h");
  161.         vb1center.getChildren().addAll(tx_Baud, slider, lbvlKM);//childrens
  162.         slider.setOnKeyReleased(new EventHandler<KeyEvent>() {//;; evento  de key released :D
  163.          @Override
  164.             public void handle(KeyEvent event) {
  165.                 System.out.println(event.getText());
  166.                 try {
  167.                     System.out.println("velocidade Do Software" + ((int) slider.getValue()));//Parseint
  168.                     serialportOne.writeString(((int) slider.getValue()) + "\n");//XD
  169.                     lbvlKM.setText(((int) slider.getValue()) + "KM\\h");
  170.  
  171.                    
  172.                    
  173.                    
  174.                    
  175.                 } catch (SerialPortException ex) {
  176.                     Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
  177.                 }
  178.             }
  179.         });
  180.         slider.setOnMouseDragged(new EventHandler<MouseEvent>() {//evento de mouse
  181.             @Override
  182.             public void handle(MouseEvent event) {
  183.  
  184.                 try {
  185.                     System.out.println("Velocidade Do Software" + ((int) slider.getValue()));
  186.                     serialportOne.writeString(((int) slider.getValue()) + "\n");
  187.                     lbvlKM.setText(((int) slider.getValue()) + "KM\\h");
  188.                 } catch (SerialPortException ex) {
  189.                     Logger.getLogger(VelocimetroGauge1.class.getName()).log(Level.SEVERE, null, ex);
  190.                 }
  191.             }
  192.         });
  193.         stageD1.show();
  194.     }
  195.  
  196.     public void Arranque() throws SerialPortException {
  197.         if (serialportOne.isOpened()) {
  198.             System.out.println("conectado portas abertas ");
  199.             //iniciando conexao e habilitando stage2
  200.             disparStage();
  201.             //criar um event listerner aqui e corrifigir depois passando para uma classe public nova
  202.             //----------------------------------------------------->>
  203.             serialportOne.addEventListener((SerialPortEvent spe) -> {
  204.                 //escuta de input e output
  205.                 if (spe.isRXCHAR() && spe.getEventValue() > 0) {
  206.                     try {
  207.                         byte[] byte_Input = serialportOne.readBytes(spe.getEventValue());
  208.                         String str_Byte_input = new String(byte_Input);
  209.                         int vlVeloControl = Integer.parseInt(str_Byte_input);
  210.                         int vlVeloSoft = Integer.parseInt(slider.toString());
  211.                         System.out.println("Velocidade A.U." + Arrays.toString(byte_Input));//preferir colections do jfx
  212.  
  213.                         if (vlVeloControl != vlVeloSoft) { //se a resposta de retorno for diferente do envio enviar novavemte (~~)
  214.                             serialportOne.writeString(vlVeloSoft + "\n");
  215.                         } else {
  216.                             serialportOne.writeString(vlVeloSoft + "\n");
  217.                         }
  218.                     } catch (SerialPortException e) {
  219.                     }
  220.                 }
  221.             });
  222.             //----------------------------------------------------->>  
  223.         } else {
  224.            
  225.             System.out.println("Nao conectado Portas  fechadas");
  226.         }
  227.     }
  228. }
  229.  
Tags: Arduino JavaFX
Advertisement
Add Comment
Please, Sign In to add comment