Wall's Corners Wall's Corners Author
Title: How to use Arduino with Java : For Java Devs
Author: Wall's Corners
Rating 5 of 5 Des:
Arduino is every tech enthusiast and hacker’s favorite tool. In this article shows you how to send data using the serial port from a Java ap...

Arduino is every tech enthusiast and hacker’s favorite tool. In this article shows you how to send data using the serial port from a Java application. The library that was heavily used for this kind of this was RXTX. This library was really useful however it adds additional complexity for the developers like install native libraries for each S.O. (Linux-Mac-Windows). A good replacement for RXTX is JSSC.

JSSC is a new library that could be used for send data using the serial port from Java, and you only need to add the jar file to the classpath of your Java app and nothing else is required. Let me show you:

All the source code is available on github in this link. If you want a direct download, you could use this link (press skip-add on the right-upper corner)

I have created a Java Class to read the serial port output:

 

public class PortReader implements SerialPortEventListener {
    private SerialPort serialPort;
    public PortReader(SerialPort serialPort) {
        this.serialPort = serialPort;
    }
    public synchronized void serialEvent(SerialPortEvent event) {
        if (event.isRXCHAR()) {
            try {
                byte[] buffer = serialPort.readBytes(1);
                System.out.print(new String(buffer));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

And a simple class to interact with the serial port:

 

public class SerialPortCom {
    private SerialPort serialPort;
    public void initialize(String serialPortName) throws Exception {
        serialPort = new SerialPort(serialPortName);
        serialPort.openPort();
        serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN |SerialPort.FLOWCONTROL_RTSCTS_OUT);
        // Set a listener to read the ARDUINO output
        serialPort.addEventListener(new PortReader(serialPort), SerialPort.MASK_RXCHAR);
    }

    public void closeConnection() throws Exception {
        serialPort.closePort();
    }

    public void sendData(String data) {
        try {
            serialPort.writeString(data);
        } catch (SerialPortException e) {
            e.printStackTrace();
        }
    }

    public String[] getAvailableSerialPorts() {
        String[] ports = SerialPortList.getPortNames();
        return ports;
    }

 

initialize: method to start the communication with a specified port
sendData: method to send a string using the serial port
getAvailableSerialPorts: returns an array with all the available ports that have been found
closeConnection: close the serial connection that has been established previously

You can interact using this class (just open IntelliJ/Netbeans/Eclipse, open this class and on it, right click -> run JavaSerialIntegration… ). You will see an output with the available serial ports found, you should choose the port that you want, and the program will send two strings: A and then 1, the program will wait 10 seconds after each string sent.

arduino

public class JavaSerialIntegration {
    public static void main(String[] args) throws Exception {
        SerialPortCom serialPortCom = new SerialPortCom();
        String[] ports = serialPortCom.getAvailableSerialPorts();
        System.out.println("Serial ports on the computer are:");
        for (int i = 0; i < ports.length; i++) {
            System.out.println((i + 1) + ". " + ports[i]);
        }

        System.out.println("Chose the serial port desired (1 , 2 , anything), and press enter: ");
        Scanner scanner = new Scanner(System.in);
        int selectedPort = scanner.nextInt();
        String port = ports[selectedPort - 1];

        System.out.println("You will use the port: " + port);
        serialPortCom.initialize(port);
        System.out.println("Sending A....");
        // Send text A
        serialPortCom.sendData("A");
        System.out.println("A has been received");
        // Wait 10 seconds
        System.out.println("Waiting 10 seconds...");
        Thread.sleep(10000);

        System.out.println("Sending 1....");
        // Send text 1
        serialPortCom.sendData("1");
        System.out.println("1 has been received");
        // Wait 10 seconds
        System.out.println("Waiting 10 seconds...");
        Thread.sleep(10000);

        serialPortCom.closeConnection();
        System.out.println("Serial communication has finished");
    }
}

If you have any better suggestions, drop them down in here.

Share This:

View more at: http://yoursmart.mobi

About Author

Advertisement

Post a Comment

 
Top