Wixel + Java

Hi,

Im currently working on a pojekt where I need to comunicate to a wixel connected to a java program via USB.

Unfortunatly, as I understand it, java doesn’t work to well with USB ports.

Has anybody figured out a “simple” solution to this problem?

At the moment we are considering taking a detour via a C program that reads from the java application and writes to the USB. Is this a reasonable solution?

Appreciate any help!
/Simon

The Wixel identifies itself as a serial port. So all you need to do is open the right /dev/ttyACM* or COM# and Bob’s your uncle.

That simplifies things =)
Anybody have any example java code that comunicates with a wixel connected to a computer?

/Simon

Hello, Simon. You could also just look for any example Java code that communicates with a serial port; you’ll be much more likely to find example code that way, and the code you find will probably work for the Wixel. Some other people on this forum have used the RXTX library but I can’t say for sure if it will work for you:

rxtx.qbang.org/wiki/index.php/Main_Page

–David

Here is a simple java program that exercises the Example Serial Communication Sketch, assuming two wixels, one on an arduino shield, and the other plugged into the machine running java (a Mac, in my case). It needs the purejavacomm.jar library. I’m not claiming great style, but it does work. It is hacked from the Arduino Java example.

import purejavacomm.*;
import java.io.*;

public class ControlWixelSerial implements SerialPortEventListener {

    private SerialPort serialPort;
    private InputStream input;
    private OutputStream output;

    public static void main(String[] args) throws Exception {
        ControlWixelSerial main = new ControlWixelSerial();
        main.doit();
    }

    public void doit() {
        try {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/cu.usbmodemfa141");
            if (portIdentifier.isCurrentlyOwned()) {
                System.out.println("Port in use!");
            } else {
                SerialPort serialPort = (SerialPort) portIdentifier.open(this.getClass().getName(), 1000);
                serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                output = serialPort.getOutputStream();
                input = serialPort.getInputStream();
                serialPort.addEventListener(this);
                serialPort.notifyOnDataAvailable(true);
                Thread.sleep(2000);
                String command = "H";
                System.out.println("Turning LED on");
                output.write(command.getBytes());
                System.out.println("Command sent");
                output.flush();
                Thread.sleep(3000);
                command = "L";
                System.out.println("Turning LED off");
                output.write(command.getBytes());
                System.out.println("Command sent");
                close();
                System.exit(0);
            }
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.getMessage());
        }

    }

    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                int available = input.available();
                byte chunk[] = new byte[available];
                input.read(chunk, 0, available);
                System.out.print(new String(chunk));
            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }
        // Ignore all the other eventTypes, but you should consider the other ones.
    }

    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }
}