Guest User

terminalJFX_arduino

a guest
Apr 28th, 2017
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.80 KB | None | 0 0
  1. /*
  2.  * Bem10jfx.blogspot.com
  3.  */
  4. package loockupfxsansung;
  5.  
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javafx.application.Application;
  9. import javafx.application.Platform;
  10. import javafx.event.ActionEvent;
  11. import javafx.event.EventHandler;
  12. import javafx.geometry.Point2D;
  13. import javafx.scene.Group;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Button;
  16. import javafx.scene.control.CheckBox;
  17. import javafx.scene.control.ChoiceBox;
  18. import javafx.scene.control.Label;
  19. import javafx.scene.control.TextArea;
  20. import javafx.scene.control.TextField;
  21. import javafx.scene.layout.StackPane;
  22. import javafx.scene.paint.Color;
  23. import javafx.stage.Stage;
  24. import javafx.stage.WindowEvent;
  25. import jssc.SerialPort;
  26. import jssc.SerialPortEvent;
  27. import jssc.SerialPortEventListener;
  28. import jssc.SerialPortException;
  29. import jssc.SerialPortList;
  30.  
  31. /**
  32.  *
  33.  * @author kml
  34.  */
  35. public class LoockupFXSansung extends Application {
  36.  
  37.     Label lxtext = new Label("desconectado");
  38.     ChoiceBox choiceBox = new ChoiceBox();
  39.     Button bt = new Button("Conectar");
  40.     TextArea ta = new TextArea();
  41.     Button btsend = new Button("send");
  42.     TextField tf = new TextField();
  43.  
  44.     SerialPort serialport;
  45.  
  46.     public void arranque() {
  47.  
  48.         final String[] serialPortNames = SerialPortList.getPortNames();
  49.         for (String name : serialPortNames) {
  50.             choiceBox.getItems().addAll(name);
  51.         }
  52.  
  53.     }
  54.  
  55.     public void Buttons_Actions() {
  56.  
  57.         bt.setOnAction(new EventHandler<ActionEvent>() {
  58.             @Override
  59.             public void handle(ActionEvent event) {
  60.                 try {
  61.  
  62.                     if (choiceBox.getValue().toString().length() >= 1) {
  63.  
  64.                         System.out.println(" portas " + choiceBox.getValue());
  65.                         serialport = new SerialPort("" + choiceBox.getValue());
  66.                         // lb.setText("conectado a porta: " + choiceBox.getValue());
  67.                         ta.setText("Conectando ao Dispositivo\n");
  68.                         try {
  69.  
  70.                             serialport.openPort();
  71.                             serialport.setParams(9600, 8, 1, 0);
  72.                             serialport.setEventsMask(SerialPort.MASK_RXCHAR);
  73.                             serialport.addEventListener(new SerialPortEventListener() {
  74.                                 @Override
  75.                                 public void serialEvent(SerialPortEvent serialPortEvent) {
  76.                                     if (serialPortEvent.isRXCHAR()) {
  77.                                         try {
  78.                                             ta.appendText(serialport.readString(serialPortEvent.getEventValue()));
  79.                                             String ch = ta.toString();
  80.  
  81.                                             if (ch.endsWith("\r\n")) {
  82.                                                 ta.appendText(ch.substring(0, ch.indexOf("\r\n")));
  83.                                             }
  84.                                         } catch (SerialPortException e) {
  85.  
  86.                                             System.out.println("SerialEvent error:" + e.toString());
  87.                                         }
  88.                                     }
  89.                                 }
  90.                             });
  91.                         } catch (Exception e) {
  92.                             System.out.println("error nao conexao" + e.getMessage());
  93.                             lxtext.setText(" AΓ§ao duplicada"
  94.                                     + "" + choiceBox.getValue() + "\nError de confirmaΓ§ao:" + e);
  95.                         }
  96.                     } else {
  97.                         lxtext.setText(" escolha uma porta:");
  98.                         System.out.println("error nao tem porta escolhida");
  99.                     }
  100.  
  101.                 } catch (Exception se) {
  102.                     System.out.println("error" + se);
  103.                     //     lb.setText("nao conectado a nenhuma porta\nerror:" + se);
  104.                 }
  105.  
  106.             }
  107.         });
  108.  
  109.     }
  110.  
  111.     @Override
  112.     public void start(Stage primaryStage) {
  113.         Button btn = new Button();
  114.         btn.setText("Say 'entrar'");
  115.         btn.setOnAction(new EventHandler<ActionEvent>() {
  116.  
  117.             @Override
  118.             public void handle(ActionEvent event) {
  119.                 System.out.println("entrar");
  120.  
  121.                 Stage stage = new Stage();
  122.                 Group gp = new Group();
  123.                 Scene scene = new Scene(gp, 400, 400, Color.SILVER);
  124.                 stage.setScene(scene);
  125.                 lxtext.setLayoutX(10);
  126.                 lxtext.setLayoutY(10);
  127.                 choiceBox.layoutXProperty().bind(lxtext.layoutXProperty().add(5));
  128.                 choiceBox.layoutYProperty().bind(lxtext.layoutYProperty().add(15));
  129.  
  130.                 bt.setLayoutX(90);
  131.                 bt.setLayoutY(25);
  132.                 ta.setLayoutX(10);
  133.  
  134.                 ta.layoutYProperty().bind(choiceBox.layoutYProperty().add(25));
  135.                 ta.setPrefSize(150, 120);
  136.  
  137.                 gp.getChildren().addAll(lxtext, choiceBox, bt, ta, tf, btsend);
  138.                 arranque();
  139.  
  140.                 bt.setOnAction(new EventHandler<ActionEvent>() {
  141.                     @Override
  142.                     public void handle(ActionEvent event) {
  143.  
  144.                         Buttons_Actions();
  145.  
  146.                     }
  147.                 });
  148.  
  149.                 tf.setLayoutX(10);
  150.                 tf.layoutYProperty().bind(ta.layoutYProperty().add(125));
  151.                 tf.setPromptText("CMD");
  152.                 tf.setPrefWidth(100);
  153.                 //tf.prefWidthProperty().bind(scene.widthProperty().divide(3));
  154.                 btsend.layoutXProperty().bind(tf.layoutXProperty().add(105));
  155.                 btsend.layoutYProperty().bind(tf.layoutYProperty());
  156.  
  157.                 btsend.setOnAction(new EventHandler<ActionEvent>() {
  158.                     @Override
  159.                     public void handle(ActionEvent event) {
  160.  
  161.                         lxtext.setText("dados Enviando");
  162.                         try {
  163.                             serialport.writeBytes(tf.getText().getBytes());
  164.                             System.out.println("envio" + tf.getText());
  165.                             lxtext.setText("dados recebidos");
  166.                         } catch (SerialPortException ex) {
  167.                             System.out.println("Nao conectado");
  168.                             lxtext.setText("nao conectado");
  169.  
  170.                         }
  171.  
  172.                     }
  173.                 });
  174.  
  175.                 stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
  176.                     @Override
  177.                     public void handle(WindowEvent event1) {
  178.                         if (event1.getSource() == WindowEvent.WINDOW_CLOSE_REQUEST && serialport.isOpened()) {
  179.                             System.out.println("porta aberta fechando");
  180.                             try {
  181.                                 serialport.closePort();
  182.                             } catch (SerialPortException ex) {
  183.                                 Logger.getLogger(LoockupFXSansung.class.getName()).log(Level.SEVERE, null, ex);
  184.                             }
  185.                             System.out.println(".handle()=aplicativo finalizado");
  186.                             Platform.exit();
  187.  
  188.                         }
  189.                     }
  190.                 });
  191.                 stage.show();
  192.             }
  193.         });
  194.  
  195.         StackPane root = new StackPane();
  196.         root.getChildren().add(btn);
  197.  
  198.         Scene scene = new Scene(root, 300, 250);
  199.  
  200.         primaryStage.setTitle("Loockup");
  201.         primaryStage.setScene(scene);
  202.         primaryStage.show();
  203.     }
  204.  
  205.     /**
  206.      * @param args the command line arguments
  207.      */
  208.     public static void main(String[] args) {
  209.         launch(args);
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment