Guest User

javafx_arduino_LCD

a guest
Jun 13th, 2017
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.33 KB | None | 0 0
  1. /*
  2.  *bem10jfx.blogspot.com
  3.  */
  4. package lcdsend;
  5.  
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javafx.application.Application;
  9. import javafx.event.ActionEvent;
  10. import javafx.event.EventHandler;
  11. import javafx.scene.Group;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.ChoiceBox;
  15. import javafx.scene.control.Label;
  16. import javafx.scene.control.TextArea;
  17. import javafx.scene.control.TextField;
  18. import javafx.scene.input.KeyEvent;
  19. import javafx.scene.input.MouseEvent;
  20. import javafx.scene.layout.HBox;
  21. import javafx.scene.paint.Color;
  22. import javafx.stage.Stage;
  23. import javafx.stage.StageStyle;
  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 kmls
  34.  */
  35. public class Lcdsend extends Application {
  36.  
  37.     String n1 = "0";
  38.     String n2 = "0";
  39.     ChoiceBox cbPorta = new ChoiceBox();
  40.     SerialPort serialport;
  41.     TextArea taSend = new TextArea();
  42.     TextArea taRecevOff = new TextArea();
  43.     TextField tf1 = new TextField();
  44.     TextField tf2 = new TextField();
  45.     Label lbPortsStatu = new Label("Status de Porta");
  46.  
  47.      public void arranquedeconexao() {
  48.  
  49.         final String[] serialPortasNomes = SerialPortList.getPortNames();
  50.  
  51.         for (String nomeportas : serialPortasNomes) {
  52.             cbPorta.getItems().addAll(nomeportas);
  53.         }
  54.  
  55.     }
  56.  
  57.     @Override
  58.     public void start(Stage stagex) {
  59.         Stage stage = new Stage();
  60.         Group gp = new Group();
  61.         Scene scene = new Scene(gp, 400, 450, Color.SILVER);
  62.         stage.setScene(scene);
  63.  
  64.         try {
  65.             arranquedeconexao();
  66.         } catch (Exception e) {
  67.             System.out.println("falha ao fazer o arranque\n" + e.getMessage());
  68.         }
  69.  
  70.         tf1.setPromptText("Max.16 digitos");
  71.         tf2.setPromptText("Max.16 digitos");
  72.         //bt1 send        
  73.         Button btSend = new Button("Send");
  74.         Button btReset = new Button("Reset");
  75.  
  76.         Label tx_tf1 = new Label("N1:" + n1);
  77.         Label tx_tf2 = new Label("N2:" + n2);
  78.         Label tx_al1 = new Label("Linha1:"),
  79.                 tx_al2 = new Label("Linha2:");
  80.  
  81.         tf1.layoutXProperty().bind(scene.widthProperty().divide(2.5));
  82.         tf1.layoutYProperty().bind(scene.heightProperty().divide(3));
  83.  
  84.         tf1.prefWidthProperty().bind(scene.widthProperty().divide(3.2));
  85.         tf2.prefWidthProperty().bind(scene.widthProperty().divide(3.2));
  86.  
  87.         tf2.layoutXProperty().bind(tf1.layoutXProperty());
  88.         tf2.layoutYProperty().bind(tf1.layoutYProperty().add(25));
  89.  
  90.         btSend.layoutXProperty().bind(tf1.layoutXProperty().add(tf1.prefWidthProperty().add(20)));
  91.         btSend.layoutYProperty().bind(tf1.layoutYProperty());
  92.  
  93.         btReset.layoutXProperty().bind(tf1.layoutXProperty().add(tf1.prefWidthProperty().add(20)));
  94.         btReset.layoutYProperty().bind(tf2.layoutYProperty());
  95.  
  96.         tx_tf1.layoutXProperty().bind(tf1.layoutXProperty().subtract(30));
  97.         tx_tf2.layoutXProperty().bind(tf2.layoutXProperty().subtract(30));
  98.  
  99.         tx_tf1.layoutYProperty().bind(tf1.layoutYProperty());
  100.         tx_tf2.layoutYProperty().bind(tf1.layoutYProperty().add(25));
  101.         //
  102.         tx_al1.layoutXProperty().bind(tx_tf1.layoutXProperty().subtract(40));
  103.         tx_al2.layoutXProperty().bind(tx_tf2.layoutXProperty().subtract(40));
  104.  
  105.         tx_al1.layoutYProperty().bind(tx_tf1.layoutYProperty());
  106.         tx_al2.layoutYProperty().bind(tx_tf2.layoutYProperty());
  107.  
  108.         taSend.setPrefSize(125, 170);
  109.  
  110.         taRecevOff.setPrefSize(125, 170);
  111.  
  112.         Label lbSendx = new Label("send");
  113.         Label lbReceivx = new Label("received");
  114.  
  115.         lbSendx.layoutXProperty().bind(tx_al1.layoutXProperty().subtract(40));
  116.         lbReceivx.layoutXProperty().bind(tf2.layoutXProperty().add(35));
  117.  
  118.         lbSendx.layoutYProperty().bind(tx_tf2.layoutYProperty().add(40));
  119.         lbReceivx.layoutYProperty().bind(tx_tf2.layoutYProperty().add(40));
  120. //
  121.         taSend.layoutXProperty().bind(lbSendx.layoutXProperty());
  122.         taRecevOff.layoutXProperty().bind(lbReceivx.layoutXProperty());
  123.  
  124.         taSend.layoutYProperty().bind(lbSendx.layoutYProperty().add(20));
  125.         taRecevOff.layoutYProperty().bind(lbReceivx.layoutYProperty().add(20));
  126.  
  127.         Button btatual = new Button("Atualizar");
  128.         Button btatualReset = new Button("Reset");
  129.  
  130.         btatual.layoutXProperty().bind(taRecevOff.layoutXProperty().add(taRecevOff.prefWidthProperty().add(10)));
  131.         btatualReset.layoutXProperty().bind(taRecevOff.layoutXProperty().add(taRecevOff.prefWidthProperty().add(10)));
  132.  
  133.         btatual.layoutYProperty().bind(lbSendx.layoutYProperty().add(20));
  134.         btatualReset.layoutYProperty().bind(btatual.layoutYProperty().add(30));
  135.         HBox hb1 = new HBox(10);
  136.  
  137.         Label lbPorts = new Label("Portas");
  138.         Button btConectPort = new Button("conectar");
  139.  
  140.         hb1.getChildren().addAll(cbPorta, lbPorts, btConectPort, lbPortsStatu);
  141.  
  142.         gp.getChildren().addAll(
  143.                 tx_al1, tx_al2, tf1, tf2,
  144.                 btSend, btReset, tx_tf1, tx_tf2,
  145.                 lbSendx, lbReceivx, taSend, taRecevOff,
  146.                 btatual, btatualReset, hb1
  147.         );
  148.  
  149.         tf1.setOnKeyPressed(new EventHandler<KeyEvent>() {
  150.             @Override
  151.             public void handle(KeyEvent event) {
  152.  
  153.                 n1 = "" + (tf1.getText().length() + 1);
  154.                 tx_tf1.setText("N1:" + n1.toString());
  155.             }
  156.         });
  157.         tf2.setOnKeyPressed(new EventHandler<KeyEvent>() {
  158.             @Override
  159.             public void handle(KeyEvent event) {
  160.                 n2 = "" + (tf2.getText().length() + 1);
  161.                 tx_tf2.setText("N2:" + n2.toString());
  162.             }
  163.         });
  164.         btReset.setOnMouseClicked(new EventHandler<MouseEvent>() {
  165.             @Override
  166.             public void handle(MouseEvent event) {
  167.  
  168.                 tf1.setText("");
  169.                 tf2.setText("");
  170.                 n1 = "0";
  171.                 n2 = "0";
  172.                 tx_tf1.setText("N1:" + n1);
  173.                 tx_tf2.setText("N2:" + n2);
  174.                 tx_al1.setText("Linha1:");
  175.                 tx_al2.setText("Linha2:");
  176.  
  177.             }
  178.         }
  179.         );
  180.  
  181.         btatualReset.setOnMouseClicked(new EventHandler<MouseEvent>() {
  182.             @Override
  183.             public void handle(MouseEvent event) {
  184.                 try {
  185.                     serialport.writeBytes("*".getBytes());
  186.                 } catch (SerialPortException ex) {
  187.                     Logger.getLogger(Lcdsend.class.getName()).log(Level.SEVERE, null, ex);
  188.                 }
  189.                 taSend.setText("");
  190.                 taRecevOff.setText("");
  191.  
  192.             }
  193.         }
  194.         );
  195.         btSend.setOnAction(new EventHandler<ActionEvent>() {
  196.             @Override
  197.             public void handle(ActionEvent event) {
  198.                 if (tf1.getText().length() > 0 && tf1.getText().length() > 0) {
  199.  
  200.                     try {
  201.                         serialport.writeBytes(tf1.getText().getBytes());
  202.                         serialport.writeBytes(tf2.getText().getBytes());
  203.                         taSend.appendText("\r\n" + tf1.getText() + ""
  204.                                 + "\r\n" + tf2.getText());
  205.  
  206.                         serialport.addEventListener(new SerialPortEventListener() {
  207.                             @Override
  208.                             public void serialEvent(SerialPortEvent serialPortEvent) {
  209.                                 if (serialPortEvent.isRXCHAR()) {
  210.                                     try {
  211.                                         taRecevOff.appendText(serialport.readString(serialPortEvent.getEventValue()));
  212.                                         String ch = taRecevOff.toString();
  213.  
  214.                                         if (ch.endsWith("\r\n")) {
  215.                                             taRecevOff.appendText(ch.substring(0, ch.indexOf("\r\n")));
  216.                                         }
  217.                                     } catch (SerialPortException e) {
  218.  
  219.                                         System.out.println("SerialEvent error:" + e.toString());
  220.                                     }
  221.                                 }
  222.                             }
  223.                         });
  224.  
  225.                         System.out.println("envio" + tf1.getText() + tf2.getText());
  226.  
  227.                     } catch (SerialPortException ex) {
  228.                         System.out.println("gerou error+" + ex.getMessage());
  229.  
  230.                     }
  231.                 } else {
  232.                     System.out.println("Nenhum dados nos campos");
  233.                     Stage stagealerta = new Stage(StageStyle.UTILITY);
  234.                     Group gpalerta = new Group();
  235.                     Scene sceneAlerta = new Scene(gpalerta, 400, 40, Color.SILVER);
  236.                     stagealerta.setScene(sceneAlerta);
  237.  
  238.                     Label lbsair = new Label("X");
  239.                     Label lbmsg = new Label("Falta de Texto no Campo");
  240.                     gpalerta.getChildren().addAll(lbmsg, lbsair);
  241.                     lbmsg.setLayoutX(10);
  242.                     lbmsg.setLayoutY(10);
  243.                     lbmsg.setTextFill(Color.BLUEVIOLET);
  244.                     lbsair.setLayoutX(sceneAlerta.getWidth() - 25);
  245.                     lbsair.setLayoutY(15);
  246.                     lbsair.setTextFill(Color.RED);
  247.  
  248.                     lbsair.setOnMouseClicked(new EventHandler<MouseEvent>() {
  249.                         @Override
  250.                         public void handle(MouseEvent event) {
  251.                             stagealerta.close();
  252.                         }
  253.                     });
  254.  
  255.                     stagealerta.show();
  256.  
  257.                 }
  258.             }
  259.         });
  260.  
  261.         btConectPort.setOnAction(new EventHandler<ActionEvent>() {
  262.             @Override
  263.             public void handle(ActionEvent event) {
  264.                 try {
  265.                     System.out.println("Conectando a porta jssc\n"
  266.                             + "porta " + cbPorta.getValue().toString());
  267.                     lbPortsStatu.setText("Conectando a porta jssc\n" + "porta " + cbPorta.getValue().toString());
  268.                     serialport = new SerialPort(cbPorta.getValue().toString());
  269.                     serialport.openPort();
  270.                     serialport.setParams(9600, 8, 1, 0);
  271.                     serialport.setEventsMask(SerialPort.MASK_RXCHAR);
  272.  
  273.                     serialport.addEventListener(new SerialPortEventListener() {
  274.                         @Override
  275.                         public void serialEvent(SerialPortEvent serialPortEvent) {
  276.                             if (serialPortEvent.isRXCHAR()) {
  277.                                 try {
  278.                                     taRecevOff.appendText(serialport.readString(serialPortEvent.getEventValue()));
  279.                                     String ch = taRecevOff.toString();
  280.  
  281.                                     if (ch.endsWith("\r\n")) {
  282.                                         taRecevOff.appendText(ch.substring(0, ch.indexOf("\r\n")));
  283.                                     }
  284.                                 } catch (SerialPortException e) {
  285.  
  286.                                     System.out.println("SerialEvent error: nao conectado" + e.toString());
  287.                                 }
  288.                             }
  289.                         }
  290.                     });
  291.  
  292.                 } catch (SerialPortException ex) {
  293.                     Logger.getLogger(Lcdsend.class.getName()).log(Level.SEVERE, null, ex);
  294.                     System.err.println("error : Sem porta de conexao escolhida");
  295.                 }
  296.             }
  297.         }
  298.         );
  299.  
  300.         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
  301.             @Override
  302.             public void handle(WindowEvent event) {
  303.  
  304.                 try {
  305.                     if (serialport.isOpened()) {
  306.                         serialport.closePort();
  307.                     }
  308.                 } catch (SerialPortException ex) {
  309.                     Logger.getLogger(Lcdsend.class.getName()).log(Level.SEVERE, null, ex);
  310.                 }
  311.             }
  312.         });
  313.  
  314.         stage.show();
  315. }
  316.      
  317.     public static void main(String[] args) {
  318.         launch(args);
  319.     }
  320. }
  321. //bem10jfx.blogspot.com
Add Comment
Please, Sign In to add comment