Formula AllCode with Java

Java is a very commonly used programming language and is used on many devices from your mobile phone to your PC to your car.

To get started with Java you first need to download and install a Java SE Development Kit or JDK. I downloaded Java SE Development Kit 8u91 which is Java version 8 update 91.

Once the JDK is installed you can verify it is setup correctly by clicking start and running CMD.

In the command terminal type in the following commands and both should return the same number which confirms that the JDK is operational and correctly setup.

java -version

javac -version

The java command line is what we use to run a program and the javac is what we use to compile a java program.

Using a text editor I created my first simple program Hello.java.

class Hello
{
    public static void main (String[] args)
    {
        System.out.println(“Hello World”);
    }
}

I compiled this by using the CMD window. I navigated to the directory containing my source code and then ran the following line to compile the code.

javac Hello.java

To run the code I simply type the following command.

java Hello

On doing this I get the message “Hello World” appearing in the command window.

The next program starts to use serial communications. To do this I used the jssc library which can be downloaded from here.

https://github.com/scream3r/java-simple-serial-connector/releases

You just need the file jssc.jar to be in the same folder as your java source code.

import jssc.*;

class AllCode
{
    public static void main (String[] args)
    {
        System.out.println(“Opening Port”);

        //Assign the COM port number we wish to open
        SerialPort serialPort = new SerialPort(“COM32”);

        try
        {
            //Open The Port
            System.out.println(“Port Opened: ” + serialPort.openPort());

            //Setup The Port
            System.out.println(“Port Setup: ” + serialPort.setParams(SerialPort.BAUDRATE_115200,
                                 SerialPort.DATABITS_8,
                                 SerialPort.STOPBITS_1,
                                  SerialPort.PARITY_NONE));

            //Writes data to port
            serialPort.writeBytes(“LCDClear;”.getBytes());
            serialPort.writeBytes(“LCDPrint 0 0 Hello World;”.getBytes());

            //Closing the port
            serialPort.closePort();

        }
        catch (SerialPortException ex)
        {
            System.out.println(ex);
        }
    }
}

You then compile the project using this command line.

javac -cp jssc.jar AllCode.java

And finally run the project using this command line on a Windows machine, other operating systems may differ.

java -cp .;jssc.jar AllCode

Here is the same program but this time using functions to drive various aspects of the robot to provide a more modular way of working.

import jssc.*;

class AllCode
{
    public static void main (String[] args)
    {
        //Assign the COM port number we wish to open
        SerialPort serialPort = new SerialPort(“COM32”);

        //Open Port
        COM_Open(serialPort);

        //Collect Light Level and save to variable
        int Light = ReadLight(serialPort);

        //Send Commands to the FA Robot
        //Clear LCD
        LCDClear(serialPort);

        //Print Light Level to LCD
        LCDPrint(serialPort, “Light: ” + String.valueOf(Light), 0, 8);

        //Print Light Level to Command Window
        System.out.println(“Light: ” + String.valueOf(Light));

        //Close the port
        COM_Close(serialPort);
    }

    //Port Functions
    private static void COM_Open(SerialPort serialPort)
    {
        try
        {
            //Open The Port
            System.out.println(“Port Opened: ” + serialPort.openPort());

            //Setup The Port
            System.out.println(“Port Setup: ” + serialPort.setParams(SerialPort.BAUDRATE_115200,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE));
        }
        catch (SerialPortException ex)
        {
            System.out.println(ex);
        }
    }

    private static void COM_Close(SerialPort serialPort)
    {
        try
        {
            serialPort.closePort();;
        }
        catch (SerialPortException ex)
        {
            System.out.println(ex);
        }
    }

    private static int ReadLight(SerialPort serialPort)
    {
        int retval = 0;
        try
        {
            serialPort.writeBytes(“ReadLight;”.getBytes());
            retval = ConvertInDataToInt(serialPort, 4);
        }
        catch (SerialPortException ex)
        {
            System.out.println(ex);
        }
        return retval;
    }

    //LCD Functions
    private static void LCDClear(SerialPort serialPort)
    {
        try
        {
            serialPort.writeBytes(“LCDClear;”.getBytes());
        }
        catch (SerialPortException ex)
        {
            System.out.println(ex);
        }
    }

    private static void LCDPrint(SerialPort serialPort, String data, int x, int y)
    {
        String dataOut = “LCDPrint ” + x + ” ” + y + ” ” + data + “;”;

        try
        {
            serialPort.writeBytes(dataOut.getBytes());
        }
        catch (SerialPortException ex)
        {
            System.out.println(ex);
        }
    }

}

A more complete version of the Java code can be found on the Formula AllCode Downloads page.

4,368 total views, 1 views today

Leave a Reply