Using Java With The Xtralien X100

Java is one of the most popular Programming languages to use as it offers a simple introduction to many programming concepts. Because of this, we have created a Java Module to enable a simple start into using Java to control the X100.

Connecting to the X100 is essentially the same as in any other language, and as such will consist of a Socket connection to the X100. This is available as a built-in class as java.net.Socket.

To import all of the libraries that we will need to connect to, and communicate with the X100, add the following lines to the top of your Java file.

import java.lang.*;
import java.io.*;
import java.net.*;

The rest of the code that we will be using to communicate with can be seen below for completeness, particularly as it is a short class.

class X100 {
    private String address;
    private Socket socket;
    private BufferedReader reader;
    private BufferedWriter writer;

    public X100(String address) {
        this.address = address;
        
        try {
            this.socket = new Socket(this.address, 8888);
            this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
        }
        catch (UnknownHostException e) {
            System.out.println("Could not find host");
        }
        catch (IOException e) {
            System.out.println("Error occured with IO");
        }
    }

    public String command(String command) {
        return this.command(command, false);
    }

    public String command(String command, boolean wait) {
        try {
            this.writer.write(command, 0, command.length());
            this.writer.flush();
            char[] cbuf = new char[512];
            while (wait && !this.reader.ready()) {}
            Thread.sleep(1);
            this.reader.read(cbuf, 0, 512);
            return new String(cbuf);
        }
        catch (IOException e) {
            System.out.println("IO Exception");
            return "";
        }
        catch (InterruptedException e) {
            System.out.println("Interruped");
            return "";
        }
    }

    public static void main(String[] args) {
        X100 x = new X100("192.168.2.237");
        System.out.println(x.command("cloi hello\n", true));
        x.command("set range 2");
        System.out.println(x.command("smu1 measure\n", true));
    }
}

The above class provides a very simple interface to the X100 through the Java Socket class, which at it's core is a simple TCP connection over Ethernet. In this example we also make use of Buffered readers and writers to enable easy reading and writing from the connection streams. This choice will also make it easier to add other methods of communication at a later stage by allowing the creation to only occur once, and allow all reading and writing to be the same.

When using the Socket class you need to supply both a host (either the name or IP address of a device) and a port for the connection to connect to. You will then have access to both input and output streams on which to communicate over. For this we will be using the BufferedReader and BufferedWriter classes as they are provided with Java and are well supported.

When using the BufferedWriter class we can easily send commands over the connection by calling the method BufferedWriter.write(data), which will add the data data to connection buffer. A call to BufferedWriter.flush() will then actually send the buffered data down the stream to the X100.

The inverse is also possible, you can easily read using the BufferedReader, although you will first need to create an empty character array to transfer the data into.

Using the above class, it is trivial to send commands and receive responses from the X100 by using the X100.command(command, wait_for_data) method. The String command provided is sent to the X100 and then if wait_for_data is true then the function will pause until data is ready, after which point the data will be read into a buffer and returned as a String.