kalibre

JAVAFXSERIALPORTSaRDUINOuno

Sep 10th, 2015
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.66 KB | None | 0 0
  1. bem10jfx.blogspot.com.br
  2. knbjfx.blogspot.com.br
  3.  
  4. package arduinoblogfinal;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import javafx.application.Application;
  8. import javafx.beans.Observable;
  9. import javafx.beans.value.ObservableValue;
  10. import javafx.collections.FXCollections;
  11. import javafx.collections.ObservableList;
  12. import javafx.event.ActionEvent;
  13. import javafx.event.EventHandler;
  14. import javafx.scene.Group;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.CheckBox;
  18. import javafx.scene.control.ListView;
  19. import javafx.scene.input.MouseEvent;
  20. import javafx.scene.layout.HBox;
  21. import javafx.scene.layout.VBox;
  22. import javafx.scene.paint.Color;
  23. import javafx.scene.shape.Circle;
  24. import javafx.scene.shape.StrokeLineCap;
  25. import javafx.scene.text.Text;
  26. import javafx.stage.Stage;
  27. import jssc.SerialPort;
  28. import jssc.SerialPortException;
  29. import jssc.SerialPortList;
  30.  
  31. /**
  32.  *
  33.  * @author kno
  34.  */
  35. public class ArduinoBlogFinal extends Application {
  36.   //semaforo arduino e jfx
  37.     Circle cl_1, cl_2, cl_3;
  38.     ObservableList<String> nomes = FXCollections.observableArrayList();
  39.     ListView<String> listview = new ListView<String>(nomes);
  40.     Button btOne = new Button("connectar");
  41.     String nw_vlPorta;
  42.     SerialPort serialport;
  43.     String vl_circles;
  44.     Text tx1PortaOn = new Text("porta :" + nw_vlPorta);
  45.     Text tx2PortaStatus = new Text("Status :");
  46.     Text tx3Cor = new Text("Cor :");
  47.     Text tx4Command = new Text("Comando :" + vl_circles);
  48.        
  49.     @Override
  50.     public void start(Stage stage) {
  51.  
  52.         Group go = new Group();
  53.         Scene scene = new Scene(go, 260, 250, Color.SILVER);
  54.         stage.setScene(scene);
  55.  
  56.         VBox vb1 = new VBox();
  57.         go.getChildren().add(vb1);
  58.  
  59.         cl_1 = new Circle(25);
  60.         cl_2 = new Circle(25);
  61.         cl_3 = new Circle(25);
  62.                  
  63.          //Property().bind(cl_1.layoutYProperty());
  64.          //vb1.prefHeightProperty().bind(cl_1.layoutXProperty());
  65.        
  66.         cl_1.setFill(Color.GREEN);
  67.         cl_2.setFill(Color.YELLOW);
  68.         cl_3.setFill(Color.RED);
  69.        cl_1.setStrokeDashOffset(10);
  70.        cl_2.setStrokeDashOffset(10);
  71.        cl_3.setStrokeDashOffset(10);
  72.        
  73.         vb1.getChildren().addAll(cl_1, cl_2, cl_3);
  74.         vb1.setLayoutY(30);
  75.         vb1.setLayoutX(20);
  76.         VBox vb2 = new VBox(.40);
  77.         go.getChildren().add(vb2);
  78.         vb2.layoutXProperty().bind(vb1.layoutXProperty().add(cl_1.getRadius()).multiply(2));
  79.         vb2.layoutYProperty().bind(vb1.layoutYProperty());
  80.         HBox hb1 = new HBox(.5);
  81.         vb2.getChildren().add(hb1);
  82.         CheckBox cb1 = new CheckBox("Auto");
  83.         CheckBox cb2 = new CheckBox("Manu");
  84.         hb1.getChildren().addAll(cb1, cb2);
  85.         vb2.getChildren().add(listview);
  86.         listview.setPrefSize(55, 105);
  87.         btOne.setPrefWidth(105);
  88.         vb2.getChildren().add(btOne);
  89.         veryfiK_Port();
  90.         VBox vb3 = new VBox(0.5);
  91.         go.getChildren().add(vb3);
  92.         HBox hb1x = new HBox(0.5);
  93.         HBox hb2x = new HBox(0.5);
  94.         vb3.getChildren().addAll(hb1x, hb2x);
  95.         hb1x.getChildren().addAll(tx1PortaOn, tx2PortaStatus);
  96.         hb2x.getChildren().addAll(tx3Cor, tx4Command);
  97.         vb3.layoutXProperty().bind(vb1.layoutXProperty());
  98.         vb3.layoutYProperty().bind(stage.heightProperty().subtract(105));
  99.         listview.getSelectionModel().selectedItemProperty().addListener(
  100.                 (ObservableValue<? extends String> ov, String old_val,
  101.                         String new_val) -> {
  102.                     System.out.println(new_val);
  103.                     nw_vlPorta = new_val;
  104.                     tx1PortaOn.setText("porta :" + nw_vlPorta);
  105.                     serialport = new SerialPort("" + nw_vlPorta);
  106.                 });
  107.         cl_1.setOnMouseClicked(new EventHandler<MouseEvent>() {
  108.             @Override
  109.             public void handle(MouseEvent event) {
  110.                 cl_1.setStroke(Color.BLUEVIOLET);
  111.      
  112.                 vl_circles = "q";
  113.                 tx3Cor.setText("Cor :VERDE");
  114.                 tx4Command.setText("Comando :" + vl_circles);
  115.        
  116.                 try {
  117.                      arranque();
  118.                 } catch (Exception e) {
  119.             System.out.println("cl1 error"+e.getMessage());
  120.                 }
  121.             cl_1.setStroke(Color.BLACK);
  122.             }
  123.         });
  124.     cl_2.setOnMouseClicked(new EventHandler<MouseEvent>() {
  125.             @Override
  126.             public void handle(MouseEvent event) {
  127.                 cl_2.setStroke(Color.BLUEVIOLET);
  128.                 vl_circles = "w";
  129.                 tx3Cor.setText("Cor :AMARELO");
  130.                 tx4Command.setText("Comando :" + vl_circles);
  131.                 try {
  132.                      arranque();
  133.                 } catch (Exception e) {
  134.             System.out.println("cl2 error"+e.getMessage());
  135.                 }
  136.                
  137.         cl_2.setStroke(Color.BLACK);
  138.              }
  139.         });
  140.     cl_3.setOnMouseClicked(new EventHandler<MouseEvent>() {
  141.             @Override
  142.             public void handle(MouseEvent event) {
  143.                cl_3.setStroke(Color.BLUEVIOLET);
  144.                 vl_circles = "e";
  145.                 tx3Cor.setText("Cor :VERMELHO");
  146.                 tx4Command.setText("Comando :" + vl_circles);
  147.                 try {
  148.                      arranque();
  149.                 } catch (Exception e) {
  150.             System.out.println("cl3 error"+e.getMessage());
  151.                 }
  152.         cl_3.setStroke(Color.BLACK);
  153.  
  154.             }
  155.         });
  156. //x)
  157.     btOne.setOnAction((ActionEvent event) ->{
  158.         if (cb1.isSelected()) {
  159.             vl_circles = "a";
  160.             tx3Cor.setText("Cor: THIS ");
  161.             tx4Command.setText("Commando: a");
  162.             arranque();
  163.             btOne.setText("Desconectar");
  164.         } else if (!cb1.isSelected()) {
  165.             try {
  166.                 serialport.openPort();
  167.                 serialport.closePort();
  168.                 tx2PortaStatus.setText("Status:OFF");
  169.                 btOne.setText("connectar");
  170.             } catch (SerialPortException ex) {
  171.                 Logger.getLogger(ArduinoBlogFinal.class.getName())
  172.                         .log(Level.SEVERE, null, ex);
  173.             }
  174.         }
  175.         });
  176.         stage.show();
  177.      }
  178.  
  179.     @SuppressWarnings("empty-statement")
  180.     public void arranque() {
  181.         try {
  182.          if( serialport.openPort()){
  183.              tx2PortaStatus.setText("Status :ON");
  184.              System.out.println("Sistema pronto para "
  185.                + "INPUT E OUTPUT de dados");
  186.                serialport.setParams(9600, 8, 1, 0);
  187.                serialport.writeBytes(vl_circles.getBytes());
  188.                serialport.closePort();
  189.                System.out.println("enviando dados para porta serial\n"
  190.                + "porta-" + listview.getSelectionModel().getSelectedItems() + ""
  191.                + "\ndados de comando " + vl_circles);
  192.           }else{
  193.              tx2PortaStatus.setText("Status :OFF");
  194.              System.out.println("Sistema COMPORTAS TRANCADAS"
  195.                   + " PARA SAIDA EE ENTRADAD DE DADOS");
  196.             };
  197.            
  198.         } catch (SerialPortException ex) {
  199.             Logger.getLogger(ArduinoBlogFinal.class
  200.                     .getName()).log(Level.SEVERE, null, ex);
  201.         }
  202.     }
  203.     public void veryfiK_Port() {
  204.     String[] Str_lista = SerialPortList.getPortNames();
  205.        for (int pot = 0 + 1; pot < Str_lista.length; pot++) {
  206.            System.out.println("porta com " + Str_lista[pot]);
  207.              nomes.add(Str_lista[pot]);
  208.         }
  209.  };
  210.    
  211.     public static void main(String[] args) {
  212.         launch(args);
  213.     }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment