Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Bem10jfx.blogspot.com
- */
- package loockupfxsansung;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Point2D;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.CheckBox;
- import javafx.scene.control.ChoiceBox;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- import javafx.stage.WindowEvent;
- import jssc.SerialPort;
- import jssc.SerialPortEvent;
- import jssc.SerialPortEventListener;
- import jssc.SerialPortException;
- import jssc.SerialPortList;
- /**
- *
- * @author kml
- */
- public class LoockupFXSansung extends Application {
- Label lxtext = new Label("desconectado");
- ChoiceBox choiceBox = new ChoiceBox();
- Button bt = new Button("Conectar");
- TextArea ta = new TextArea();
- Button btsend = new Button("send");
- TextField tf = new TextField();
- SerialPort serialport;
- public void arranque() {
- final String[] serialPortNames = SerialPortList.getPortNames();
- for (String name : serialPortNames) {
- choiceBox.getItems().addAll(name);
- }
- }
- public void Buttons_Actions() {
- bt.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- try {
- if (choiceBox.getValue().toString().length() >= 1) {
- System.out.println(" portas " + choiceBox.getValue());
- serialport = new SerialPort("" + choiceBox.getValue());
- // lb.setText("conectado a porta: " + choiceBox.getValue());
- ta.setText("Conectando ao Dispositivo\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());
- lxtext.setText(" AΓ§ao duplicada"
- + "" + choiceBox.getValue() + "\nError de confirmaΓ§ao:" + e);
- }
- } else {
- lxtext.setText(" escolha uma porta:");
- 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) {
- Button btn = new Button();
- btn.setText("Say 'entrar'");
- btn.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- System.out.println("entrar");
- Stage stage = new Stage();
- Group gp = new Group();
- Scene scene = new Scene(gp, 400, 400, Color.SILVER);
- stage.setScene(scene);
- lxtext.setLayoutX(10);
- lxtext.setLayoutY(10);
- choiceBox.layoutXProperty().bind(lxtext.layoutXProperty().add(5));
- choiceBox.layoutYProperty().bind(lxtext.layoutYProperty().add(15));
- bt.setLayoutX(90);
- bt.setLayoutY(25);
- ta.setLayoutX(10);
- ta.layoutYProperty().bind(choiceBox.layoutYProperty().add(25));
- ta.setPrefSize(150, 120);
- gp.getChildren().addAll(lxtext, choiceBox, bt, ta, tf, btsend);
- arranque();
- bt.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- Buttons_Actions();
- }
- });
- tf.setLayoutX(10);
- tf.layoutYProperty().bind(ta.layoutYProperty().add(125));
- tf.setPromptText("CMD");
- tf.setPrefWidth(100);
- //tf.prefWidthProperty().bind(scene.widthProperty().divide(3));
- btsend.layoutXProperty().bind(tf.layoutXProperty().add(105));
- btsend.layoutYProperty().bind(tf.layoutYProperty());
- btsend.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- lxtext.setText("dados Enviando");
- try {
- serialport.writeBytes(tf.getText().getBytes());
- System.out.println("envio" + tf.getText());
- lxtext.setText("dados recebidos");
- } catch (SerialPortException ex) {
- System.out.println("Nao conectado");
- lxtext.setText("nao conectado");
- }
- }
- });
- stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
- @Override
- public void handle(WindowEvent event1) {
- if (event1.getSource() == WindowEvent.WINDOW_CLOSE_REQUEST && serialport.isOpened()) {
- System.out.println("porta aberta fechando");
- try {
- serialport.closePort();
- } catch (SerialPortException ex) {
- Logger.getLogger(LoockupFXSansung.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println(".handle()=aplicativo finalizado");
- Platform.exit();
- }
- }
- });
- stage.show();
- }
- });
- StackPane root = new StackPane();
- root.getChildren().add(btn);
- Scene scene = new Scene(root, 300, 250);
- primaryStage.setTitle("Loockup");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment