Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stirante.Everything.arduino;
- import gnu.io.CommPortIdentifier;
- import gnu.io.SerialPort;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.HBox;
- import javafx.stage.Stage;
- import javafx.stage.WindowEvent;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import java.util.concurrent.atomic.AtomicBoolean;
- /**
- * Created by Artia
- */
- public class Main extends Application {
- private static CommPortIdentifier cpi;
- private static SerialPort port;
- private static AtomicBoolean isOn = new AtomicBoolean(false);
- private static InputStream inputStream;
- private static AtomicBoolean running = new AtomicBoolean(true);
- public static void main(String[] args) {
- Enumeration enums = CommPortIdentifier.getPortIdentifiers();
- while (enums.hasMoreElements()) {
- cpi = (CommPortIdentifier) enums.nextElement();
- if ("COM27".equals(cpi.getName())) {
- break;
- }
- System.out.println(cpi.getName());
- }
- if (cpi != null) {
- try {
- port = cpi.open("ArduinoJavaBridge", 1000);
- if (port != null) {
- port.setSerialPortParams(9600,
- SerialPort.DATABITS_8,
- SerialPort.STOPBITS_1,
- SerialPort.PARITY_NONE);
- }
- System.out.println("Ready!");
- launch(args);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void start(Stage stage) throws Exception {
- HBox root = new HBox();
- Scene scene = new Scene(root, 800, 600, true);
- final Button click = new Button("Click");
- final OutputStream outputStream = port.getOutputStream();
- click.setOnMouseClicked(new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent mouseEvent) {
- try {
- outputStream.write(0xFF);
- if (isOn.get()) outputStream.write(0x00);
- else outputStream.write(0xFF);
- outputStream.flush();
- isOn.set(!isOn.get());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- root.getChildren().add(click);
- stage.setScene(scene);
- stage.show();
- stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
- @Override
- public void handle(WindowEvent windowEvent) {
- running.set(false);
- port.close();
- }
- });
- inputStream = port.getInputStream();
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- while (running.get()) {
- if (inputStream.available() >= 2) {
- int a = inputStream.read();
- int b = inputStream.read();
- if (a == 0x00) {
- if (b == 0x00) isOn.set(false);
- else if (b == 0xFF) isOn.set(true);
- Platform.runLater(new Runnable() {
- @Override
- public void run() {
- if (isOn.get())
- click.setText("ON");
- else
- click.setText("OFF");
- }
- });
- }
- }
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }).start();
- outputStream.write(0xFF);
- outputStream.write(0x01);
- outputStream.flush();
- }
- }
Add Comment
Please, Sign In to add comment