Advertisement
stirante

Untitled

Dec 19th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package com.stirante.Everything.arduino;
  2.  
  3. import gnu.io.CommPortIdentifier;
  4. import gnu.io.SerialPort;
  5. import javafx.application.Application;
  6. import javafx.event.EventHandler;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.input.MouseEvent;
  10. import javafx.scene.layout.HBox;
  11. import javafx.stage.Stage;
  12.  
  13. import java.io.IOException;
  14. import java.io.OutputStream;
  15. import java.util.Enumeration;
  16.  
  17. /**
  18.  * Created by Artia
  19.  */
  20. public class Main extends Application {
  21.  
  22.     private static CommPortIdentifier cpi;
  23.     private static SerialPort port;
  24.     private static boolean isOn = false;
  25.  
  26.     public static void main(String[] args) {
  27.         Enumeration enums = CommPortIdentifier.getPortIdentifiers();
  28.         while (enums.hasMoreElements()) {
  29.             cpi = (CommPortIdentifier) enums.nextElement();
  30.             if ("COM27".equals(cpi.getName())) {
  31.                 break;
  32.             }
  33.             System.out.println(cpi.getName());
  34.         }
  35.  
  36.         if (cpi != null) {
  37.             try {
  38.                 port = cpi.open("ArduinoJavaBridge", 1000);
  39.                 if (port != null) {
  40.                     port.setSerialPortParams(9600,
  41.                             SerialPort.DATABITS_8,
  42.                             SerialPort.STOPBITS_1,
  43.                             SerialPort.PARITY_NONE);
  44.                 }
  45.  
  46.                 System.out.println("Ready!");
  47.                 launch(args);
  48.  
  49.             } catch (Exception e) {
  50.                 e.printStackTrace();
  51.             }
  52.         }
  53.     }
  54.  
  55.     @Override
  56.     public void start(Stage stage) throws Exception {
  57.         HBox root = new HBox();
  58.         Scene scene = new Scene(root, 800, 600, true);
  59.         Button click = new Button("Click");
  60.         click.setOnMouseClicked(new EventHandler<MouseEvent>() {
  61.             @Override
  62.             public void handle(MouseEvent mouseEvent) {
  63.                 try {
  64.                     OutputStream outputStream = port.getOutputStream();
  65.                     outputStream.write(0xFF);
  66.                     if (isOn) outputStream.write(0xFF);
  67.                     else outputStream.write(0x00);
  68.                     outputStream.flush();
  69.                     isOn = !isOn;
  70.                 } catch (IOException e) {
  71.                     e.printStackTrace();
  72.                 }
  73.             }
  74.         });
  75.         root.getChildren().add(click);
  76.         stage.setScene(scene);
  77.         stage.show();
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement