stirante

Untitled

Dec 19th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 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.application.Platform;
  7. import javafx.event.EventHandler;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.input.MouseEvent;
  11. import javafx.scene.layout.HBox;
  12. import javafx.stage.Stage;
  13. import javafx.stage.WindowEvent;
  14.  
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. import java.util.Enumeration;
  19. import java.util.concurrent.atomic.AtomicBoolean;
  20.  
  21. /**
  22.  * Created by Artia
  23.  */
  24. public class Main extends Application {
  25.  
  26.     private static CommPortIdentifier cpi;
  27.     private static SerialPort port;
  28.     private static AtomicBoolean isOn = new AtomicBoolean(false);
  29.     private static InputStream inputStream;
  30.     private static AtomicBoolean running = new AtomicBoolean(true);
  31.  
  32.     public static void main(String[] args) {
  33.         Enumeration enums = CommPortIdentifier.getPortIdentifiers();
  34.         while (enums.hasMoreElements()) {
  35.             cpi = (CommPortIdentifier) enums.nextElement();
  36.             if ("COM27".equals(cpi.getName())) {
  37.                 break;
  38.             }
  39.             System.out.println(cpi.getName());
  40.         }
  41.  
  42.         if (cpi != null) {
  43.             try {
  44.                 port = cpi.open("ArduinoJavaBridge", 1000);
  45.                 if (port != null) {
  46.                     port.setSerialPortParams(9600,
  47.                             SerialPort.DATABITS_8,
  48.                             SerialPort.STOPBITS_1,
  49.                             SerialPort.PARITY_NONE);
  50.                 }
  51.  
  52.                 System.out.println("Ready!");
  53.                 launch(args);
  54.  
  55.             } catch (Exception e) {
  56.                 e.printStackTrace();
  57.             }
  58.         }
  59.     }
  60.  
  61.     @Override
  62.     public void start(Stage stage) throws Exception {
  63.         HBox root = new HBox();
  64.         Scene scene = new Scene(root, 800, 600, true);
  65.         final Button click = new Button("Click");
  66.         final OutputStream outputStream = port.getOutputStream();
  67.         click.setOnMouseClicked(new EventHandler<MouseEvent>() {
  68.             @Override
  69.             public void handle(MouseEvent mouseEvent) {
  70.                 try {
  71.                     outputStream.write(0xFF);
  72.                     if (isOn.get()) outputStream.write(0x00);
  73.                     else outputStream.write(0xFF);
  74.                     outputStream.flush();
  75.                     isOn.set(!isOn.get());
  76.                 } catch (IOException e) {
  77.                     e.printStackTrace();
  78.                 }
  79.             }
  80.         });
  81.         root.getChildren().add(click);
  82.         stage.setScene(scene);
  83.         stage.show();
  84.         stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
  85.             @Override
  86.             public void handle(WindowEvent windowEvent) {
  87.                 running.set(false);
  88.                 port.close();
  89.             }
  90.         });
  91.         inputStream = port.getInputStream();
  92.         new Thread(new Runnable() {
  93.             @Override
  94.             public void run() {
  95.                 try {
  96.                     while (running.get()) {
  97.                         if (inputStream.available() >= 2) {
  98.                             int a = inputStream.read();
  99.                             int b = inputStream.read();
  100.                             if (a == 0x00) {
  101.                                 if (b == 0x00) isOn.set(false);
  102.                                 else if (b == 0xFF) isOn.set(true);
  103.                                 Platform.runLater(new Runnable() {
  104.                                     @Override
  105.                                     public void run() {
  106.                                         if (isOn.get())
  107.                                             click.setText("ON");
  108.                                         else
  109.                                             click.setText("OFF");
  110.                                     }
  111.                                 });
  112.                             }
  113.                         }
  114.                         try {
  115.                             Thread.sleep(10);
  116.                         } catch (InterruptedException e) {
  117.                             e.printStackTrace();
  118.                         }
  119.                     }
  120.                 } catch (IOException e) {
  121.                     e.printStackTrace();
  122.                 }
  123.             }
  124.         }).start();
  125.         outputStream.write(0xFF);
  126.         outputStream.write(0x01);
  127.         outputStream.flush();
  128.     }
  129. }
Add Comment
Please, Sign In to add comment