Advertisement
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.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 java.io.IOException;
- import java.io.OutputStream;
- import java.util.Enumeration;
- /**
- * Created by Artia
- */
- public class Main extends Application {
- private static CommPortIdentifier cpi;
- private static SerialPort port;
- private static boolean isOn = false;
- 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);
- Button click = new Button("Click");
- click.setOnMouseClicked(new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent mouseEvent) {
- try {
- OutputStream outputStream = port.getOutputStream();
- outputStream.write(0xFF);
- if (isOn) outputStream.write(0xFF);
- else outputStream.write(0x00);
- outputStream.flush();
- isOn = !isOn;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- root.getChildren().add(click);
- stage.setScene(scene);
- stage.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement