Guest User

potenciometro

a guest
Apr 26th, 2017
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.76 KB | None | 0 0
  1. /*
  2.  *
  3. bem10jfx.blogspot.com
  4.  */
  5. package potenciometrojfx;
  6.  
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import javafx.application.Application;
  10. import javafx.application.Platform;
  11. import javafx.beans.value.ChangeListener;
  12. import javafx.beans.value.ObservableValue;
  13. import javafx.event.ActionEvent;
  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.ComboBox;
  21. import javafx.scene.control.Label;
  22. import javafx.scene.control.Slider;
  23. import javafx.scene.control.TextArea;
  24. import javafx.scene.paint.Color;
  25. import javafx.stage.Stage;
  26. import javafx.stage.StageStyle;
  27. import javafx.stage.WindowEvent;
  28. import jssc.SerialPort;
  29. import static jssc.SerialPort.MASK_RXCHAR;
  30. import jssc.SerialPortEvent;
  31. import jssc.SerialPortEventListener;
  32. import jssc.SerialPortException;
  33. import jssc.SerialPortList;
  34.  
  35. /**
  36.  *
  37.  * @author kml javafx arduino
  38.  */
  39. public class PotenciometroJFX extends Application {
  40.  
  41.     Stage stage;
  42.     Group gp;
  43.     Scene scene;
  44.     Slider slider1;
  45.  
  46.     Label lb = new Label("PORTAS COM");
  47.     ChoiceBox choiseBox;
  48.     Button btports = new Button("Conectar");
  49.     TextArea ta = new TextArea();
  50.     SerialPort serialPort;
  51.     double vl1, vl2;
  52.  
  53.     public void arranque() {
  54.  
  55.         final String[] serialPortNames = SerialPortList.getPortNames();
  56.         for (String name : serialPortNames) {
  57.             choiseBox.getItems().addAll(name);
  58.         }
  59.  
  60.     }
  61.  
  62.     public void Buttons_Actions() {
  63.         btports.setOnAction(new EventHandler<ActionEvent>() {
  64.             @Override
  65.             public void handle(ActionEvent event) {
  66.                 try {
  67.  
  68.                     if (choiseBox.getValue().toString().length() >= 1) {
  69.  
  70.                         System.out.println("valores de portas " + choiseBox.getValue());
  71.                         serialPort = new SerialPort("" + choiseBox.getValue());
  72.                         lb.setText("conectado a porta: " + choiseBox.getValue());
  73.                         ta.setText("Conectando ao Arduino\n");
  74.                         try {
  75.  
  76.                             serialPort.openPort();
  77.                             serialPort.setParams(9600, 8, 1, 0);
  78.                             serialPort.setEventsMask(SerialPort.MASK_RXCHAR);
  79.                             serialPort.addEventListener(new SerialPortEventListener() {
  80.                                 @Override
  81.                                 public void serialEvent(SerialPortEvent serialPortEvent) {
  82.                                     if (serialPortEvent.isRXCHAR()) {
  83.                                         try {
  84.                                             ta.appendText(serialPort.readString(serialPortEvent.getEventValue()));
  85.                                             String ch = ta.toString();
  86.                                             if (ch.endsWith("\r\n")) {
  87.                                                 ta.appendText(ch.substring(0, ch.indexOf("\r\n")));
  88.                                             }
  89.                                         } catch (SerialPortException e) {
  90.                                             System.out.println("SerialEvent error:" + e.toString());
  91.                                         }
  92.                                     }
  93.                                 }
  94.                             });
  95.                         } catch (Exception e) {
  96.                             System.out.println("error nao conexao" + e.getMessage());
  97.                             lb.setText("Error critico nao \ndetectado portas:"
  98.                                     + "" + choiseBox.getValue() + "\nError de confirmaΓ§ao:" + e);
  99.                         }
  100.                     } else {
  101.  
  102.                         System.out.println("error nao tem porta escolhida");
  103.                     }
  104.  
  105.                 } catch (Exception se) {
  106.                     System.out.println("error" + se);
  107.                     lb.setText("nao conectado a nenhuma porta\nerror:" + se);
  108.                 }
  109.  
  110.             }
  111.         });
  112.  
  113.     }
  114.  
  115.     @Override
  116.     public void start(Stage primaryStage) {
  117.  
  118.         stage = new Stage();
  119.         gp = new Group();
  120.         scene = new Scene(gp, 400, 240, Color.SILVER);
  121.         stage.setScene(scene);
  122.         // lb label
  123.         choiseBox = new ChoiceBox();
  124.  
  125.         choiseBox.setLayoutY(20);
  126.         choiseBox.setLayoutX(10);
  127.         lb.setLayoutX(choiseBox.getLayoutX() + 25);
  128.         lb.setLayoutY(3);
  129.         arranque();
  130.         gp.getChildren().addAll(choiseBox, lb, btports);
  131.  
  132.         btports.setLayoutX(choiseBox.getHeight() + 95);
  133.         btports.setLayoutY(20);
  134.         Buttons_Actions();
  135.  
  136.         slider1 = new Slider(0, 255, 0);
  137.  
  138.         slider1.layoutXProperty().bind(scene.widthProperty().divide(8));
  139.         slider1.layoutYProperty().bind(scene.heightProperty().divide(4));
  140.         slider1.setShowTickMarks(true);
  141.         slider1.setShowTickLabels(true);
  142.         slider1.setOrientation(Orientation.VERTICAL);
  143.  
  144.         gp.getChildren().addAll(slider1, ta);
  145.         //ta
  146.  
  147.         ta.layoutXProperty().bind(slider1.layoutXProperty().add(75));
  148.         ta.layoutYProperty().bind(scene.heightProperty().divide(5));
  149.         ta.setMaxWidth(200);
  150.         ta.prefHeight(150);
  151.         ta.setStyle(" -fx-background-color: #000000;");
  152.  
  153.         slider1.valueProperty().addListener(new ChangeListener<Number>() {
  154.             public void changed(ObservableValue<? extends Number> ov,
  155.                     Number old_val, Number new_val) {
  156.                 // double value =Double.parseDouble(String.format("%.0f", new_val.toString()));
  157.                 // double valueX=value/51.5;
  158.  
  159.                 try {
  160.                     serialPort.writeBytes(String.format("%.0f", new_val).getBytes());
  161.                     System.out.println("getwritebytes=" + String.format("%.0f", new_val)
  162.                             + "pulso ");
  163.  
  164.                 } catch (SerialPortException ex) {
  165.                     lb.setText("\"Nao conectado\"");
  166.                     System.out.println("Nao conectado"+ex);
  167.                
  168.                 }
  169.  
  170.             }
  171.         });
  172.  
  173.         stage.show();
  174.         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
  175.             @Override
  176.             public void handle(WindowEvent event) {
  177.                 try {
  178.                     serialPort.closePort();
  179.                 } catch (SerialPortException ex) {
  180.                     Logger.getLogger(PotenciometroJFX.class.getName()).log(Level.SEVERE, null, ex);
  181.                 }
  182.             }
  183.         });
  184.     }
  185.  
  186.     /**
  187.      * @param args the command line arguments
  188.      */
  189.     public static void main(String[] args) {
  190.         launch(args);
  191.  
  192.     }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment