Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- bem10jfx.blogspot.com
- */
- package potenciometrojfx;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.beans.value.ChangeListener;
- import javafx.beans.value.ObservableValue;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Orientation;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.ChoiceBox;
- import javafx.scene.control.ComboBox;
- import javafx.scene.control.Label;
- import javafx.scene.control.Slider;
- import javafx.scene.control.TextArea;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- import javafx.stage.StageStyle;
- import javafx.stage.WindowEvent;
- import jssc.SerialPort;
- import static jssc.SerialPort.MASK_RXCHAR;
- import jssc.SerialPortEvent;
- import jssc.SerialPortEventListener;
- import jssc.SerialPortException;
- import jssc.SerialPortList;
- /**
- *
- * @author kml javafx arduino
- */
- public class PotenciometroJFX extends Application {
- Stage stage;
- Group gp;
- Scene scene;
- Slider slider1;
- Label lb = new Label("PORTAS COM");
- ChoiceBox choiseBox;
- Button btports = new Button("Conectar");
- TextArea ta = new TextArea();
- SerialPort serialPort;
- double vl1, vl2;
- public void arranque() {
- final String[] serialPortNames = SerialPortList.getPortNames();
- for (String name : serialPortNames) {
- choiseBox.getItems().addAll(name);
- }
- }
- public void Buttons_Actions() {
- btports.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- try {
- if (choiseBox.getValue().toString().length() >= 1) {
- System.out.println("valores de portas " + choiseBox.getValue());
- serialPort = new SerialPort("" + choiseBox.getValue());
- lb.setText("conectado a porta: " + choiseBox.getValue());
- ta.setText("Conectando ao Arduino\n");
- try {
- serialPort.openPort();
- serialPort.setParams(9600, 8, 1, 0);
- serialPort.setEventsMask(SerialPort.MASK_RXCHAR);
- serialPort.addEventListener(new SerialPortEventListener() {
- @Override
- public void serialEvent(SerialPortEvent serialPortEvent) {
- if (serialPortEvent.isRXCHAR()) {
- try {
- ta.appendText(serialPort.readString(serialPortEvent.getEventValue()));
- String ch = ta.toString();
- if (ch.endsWith("\r\n")) {
- ta.appendText(ch.substring(0, ch.indexOf("\r\n")));
- }
- } catch (SerialPortException e) {
- System.out.println("SerialEvent error:" + e.toString());
- }
- }
- }
- });
- } catch (Exception e) {
- System.out.println("error nao conexao" + e.getMessage());
- lb.setText("Error critico nao \ndetectado portas:"
- + "" + choiseBox.getValue() + "\nError de confirmaΓ§ao:" + e);
- }
- } else {
- System.out.println("error nao tem porta escolhida");
- }
- } catch (Exception se) {
- System.out.println("error" + se);
- lb.setText("nao conectado a nenhuma porta\nerror:" + se);
- }
- }
- });
- }
- @Override
- public void start(Stage primaryStage) {
- stage = new Stage();
- gp = new Group();
- scene = new Scene(gp, 400, 240, Color.SILVER);
- stage.setScene(scene);
- // lb label
- choiseBox = new ChoiceBox();
- choiseBox.setLayoutY(20);
- choiseBox.setLayoutX(10);
- lb.setLayoutX(choiseBox.getLayoutX() + 25);
- lb.setLayoutY(3);
- arranque();
- gp.getChildren().addAll(choiseBox, lb, btports);
- btports.setLayoutX(choiseBox.getHeight() + 95);
- btports.setLayoutY(20);
- Buttons_Actions();
- slider1 = new Slider(0, 255, 0);
- slider1.layoutXProperty().bind(scene.widthProperty().divide(8));
- slider1.layoutYProperty().bind(scene.heightProperty().divide(4));
- slider1.setShowTickMarks(true);
- slider1.setShowTickLabels(true);
- slider1.setOrientation(Orientation.VERTICAL);
- gp.getChildren().addAll(slider1, ta);
- //ta
- ta.layoutXProperty().bind(slider1.layoutXProperty().add(75));
- ta.layoutYProperty().bind(scene.heightProperty().divide(5));
- ta.setMaxWidth(200);
- ta.prefHeight(150);
- ta.setStyle(" -fx-background-color: #000000;");
- slider1.valueProperty().addListener(new ChangeListener<Number>() {
- public void changed(ObservableValue<? extends Number> ov,
- Number old_val, Number new_val) {
- // double value =Double.parseDouble(String.format("%.0f", new_val.toString()));
- // double valueX=value/51.5;
- try {
- serialPort.writeBytes(String.format("%.0f", new_val).getBytes());
- System.out.println("getwritebytes=" + String.format("%.0f", new_val)
- + "pulso ");
- } catch (SerialPortException ex) {
- lb.setText("\"Nao conectado\"");
- System.out.println("Nao conectado"+ex);
- }
- }
- });
- stage.show();
- stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
- @Override
- public void handle(WindowEvent event) {
- try {
- serialPort.closePort();
- } catch (SerialPortException ex) {
- Logger.getLogger(PotenciometroJFX.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment