Search This Blog

Monday, February 11, 2013

Java tool for Arduino interface from a PC


After a long hiatus where I've been getting a lot better at Java, I thought I'd come back to the Arduino board and IDE and clean up the primitive XYZ CNC control program and HPGL interpreter I wrote last year when I didn't know what I was doing.   The first step was to write a clean Java GUI for serial communication from the PC.

I had used myrxtx before, which I downloaded from some mysterious site.  But the Arduino has real USB serial libraries in it's download package and open source Java, so I'm going to see if I can write an interface and leverage the stuff that comes with Arduino so that everyone can use it easily without chasing down libraries and drivers.

This is the Arduino software, and the serial tool that comes embedded.   The goal is to make a tool that is just the serial monitor so I can use it as the basis for custom applications.  I'd also like to add a selection box to scan for Arduinos and pick the one you want to talk to all in one place.


Redownloaded the latest IDE from Arduino.cc.  This is now called 1.0.3.  Last time I downloaded it was version 23.
http://arduino.cc/en/Main/Software

I looked through the Arduino download, and RXTXcomm.jar and rxtxSerial.dll are
included in the software download, so a user would already have everything they need
to run a Java interface.

Browsed the Arduino IDE source code on github, and it appears all the .java source files are there,  so maybe I can bootleg their source to write my own stand alone PC side serial tool first.  I'm envisioning a tool that would get all the Arduinos COMM addresses, and let you pick which one you wanted to send a command to.   Some fields for setting the rate, encoding, etc.

I found the Arduino software source code for the serial tool java here:
https://github.com/arduino/Arduino/tree/master/app/src/processing/app
https://github.com/arduino/Arduino/blob/master/app/src/processing/app/SerialMonitor.java

Copied down the Arduino source files and loaded it into netbeans.  I can see there would be a lot of work to do to get all the pieces for the whole interface.   Dorked around, but all the imports aren't available.  This is going to take some effort to get going to use as is.  But I found all the real commands are in the class "Serial".   Copied just these files to a netbean project.

Their open source code is a bit of a nightmare.  There are no real comments and there are blocks of code commented out all over the place with no notes.   Free speaks for itself I guess.

Using a computer that already has the Arduino software installed and working so the drivers are already installed.

You also need to have Java installed.  Download and install the 32 bit version because the Arduino executables are all 32 bit.  That will save the headache described later in the post.
http://www.oracle.com/technetwork/java/javase/downloads/java-se-jre-7-download-432155.html

From the Arduino user software download directories I copied the RXTXcomm.jar from ...\arduino-1.0.1-windows\arduino-1.0.1\lib\ to
C:\Program Files (x86)\Java\jdk1.7.0_05\jre\lib\ext\    (where the netbeans programming jre wants it)
and also to
C:\Program Files (x86)\Java\jre7\lib\ext
This is where the standalone java is going to look for it once the program is running outside netbeans.

Copied rxtxSerial.dll from
...\arduino-1.0.1-windows\arduino-1.0.1
C:\Program Files (x86)\Java\jre7\bin
and also to
C:\Program Files (x86)\Java\jdk1.7.0_05\jre\bin   (netbeans)

Right click both and select Properties->Unblock to allow windows to execute them.

Stripped the program down to the bare essentials and made a small GUI to reference the serial class and send and receive serial data. 

I downloaded the Arduino serialcallresponse example program to the Arduino and fired up my Java progam.
Bingo, it worked!  OK it really took a couple hours to get it to work.   I had to figure out the pieces to find what port the Arduino was plugged into.   Writing serial data was easy, reading was a bit harder.  I had to write an event listener to push the data back up to the GUI whenever something showed up as available.  

time passes...


I'm finally ready to upload the software for all to see and use.  I had a mistake in the serial rate setting that confused me for a really long time (like a month of on and off pecking).  The wrong variable was in the command and the rate was always 2000 baud.  I kept reading back garbage bits intermixed with the real data and I thought I wasn't formatting the bytes correctly.  Drove me batty.  Once I finally figured it out  I added some formating and rate selections.  Here is the final version communicating with two Arduinos, running the serialCallResponse example.



This tool can form the basis of any PC side software that you might want to write to communicate with Arduinos.   My CNC machine uses three Arduinos all connected to one PC and communicating with the GUI.     Using the tool as it is you can open as many as you like on your desktop and pick a different Arduino each time.  It will complain of course if you try to use a port that is busy with another GUI.   Note that scanning ports and selecting a port takes a few seconds to respond.

The software has four .java files:
  • SiFishSerialArduino.java  - this is just the top level main program that pops the GUI.
  • Main_GUI.java - this is graphical interface that does all the work.  It was built in netbeans and the graphics were autogenerated.  That makes the code a bit dense.
  • Serial.java - this contains the serial comms code.  Most of this is lifted straight from the Arduino source code.
  • Listener_SerialEvent.java - this is a custom event listener used to wait for the rx serial data and pass it up to the GUI
Download the executable java program here:
http://code.google.com/p/arduino-java-xyzcnc/downloads/detail?name=SifishSerialArduino.jar&can=2&q=

Download a zip file with the whole netbeans setup, java source files and the executable distribution here:
http://code.google.com/p/arduino-java-xyzcnc/downloads/detail?name=SifishSerialArduino.zip&can=2&q=

You must have Java installed on your machine.  You can get that here:
http://www.java.com/en/download/index.jsp

Once Java is installed, and the Arduino software is all installed and working (this installs the arduino drivers) you just need to copy two files from the Arduino software into the java installation.  You may find they are already there.    I mentioned this above.

The java serial comm library that arduino uses:
From the Arduino user software download directories copy RXTXcomm.jar from
...\arduino-1.0.1-windows\arduino-1.0.1\lib\RXTXcomm.jar 
to
C:\Program Files (x86)\Java\jre7\lib\ext\RXTXcomm.jar
This is where the standalone java is going to look for it once the program is running outside netbeans.

The serial comm executable for windows:
Copy rxtxSerial.dll
from
...\arduino-1.0.1-windows\arduino-1.0.1\rxtxSerial.dll
to
C:\Program Files (x86)\Java\jre7\bin\rxtxSerial.dll

Right click both and select Properties->Unblock to allow windows to execute them.

The software won't work unless these files are in the right places and the Arduino drivers are installed.

To run the jar file in windows just double click on it.   It should show up with a Java icon if you computer has Java installed.   If it doesn't pop up, try opening a cmd window and typing java -jar SifishSerialArduino.jar.

Feel free to use and modify the program to make your own Arduino control programs to do other stuff.  That is the real purpose of the tool, to demonstrate communication in the simplest GUI so that I can build other programs from it.     Copy the files above into the netbeans java installation too if you want to be able to develop your own.

I tried downloading the .jar file to another computer to test it and had some troubles.  Double clicking on the SifishSerialArduino.jar got me a message that main couldn't be found.   I opened a cmd window and typed "java -jar SifishSerialArduino.jar" and the program started.   It did throw an exception that my rxtxSerial.dll was 32 bits on a 64 bit platform.  I've seen this type of error before, usually because of the specific java setup trying to use 64 bit JDK and the library is 32bit. 

These posts describe the problem
http://stackoverflow.com/questions/9379040/how-to-run-an-api-made-for-32-bit-on-a-64-bit-machine
http://rxtx.qbang.org/wiki/index.php/Using_RXTX_In_NetBeans

You can install 32 bit java or you can get the 64 bit dlls for rxtx here:  http://www.cloudhopper.com/opensource/rxtx/
or here
http://mfizz.com/oss/rxtx-for-java

Now I'm back to using a dll from another website and not the one that comes with the Arduino software.  However just to see if this solves the problem.
Downloaded the Windows-x64 version.  Opened the zip and Copied rxtxSerial.dll to
C:\Program Files\Java\jre7\lib\bin\rxtxSerial.dll
Note this is NOT the Program Files (x86) install of Java, which indicates 32 bit.
That solved the problem, now it works and my GUI runs on my 64 bit machine.
So a word of warning.  Either download the 32 bit version of java, or you will have to use this 64 bit dll.

I tried downloading the SifishArduinoSerial.jar to two other computers.  Both I could immediately run the GUI by just double clicking on the .jar file in windows.    They both had java installed, so copying the RXTXSerial dll and jar files from the arduino software to where I said above made them work without a hitch.  Note that without copying the libraries, the software starts but gets stuck scanning for ports.  Maybe I'll add an error trap to tell the user that is the problem.

Here is another version of the serial tool that includes an autodetection for an Arduino that is sending a character repeatedly in order to be identified.  It auto detects the arduino and selects it.
This includes the source java and rxtx files.  The executable is in /dist.
Double click on dist/SifishSerial ArdunioTool.jar
https://drive.google.com/file/d/0B2Qrk8yU95oCVGNBMVBtb1ctdXM/edit?usp=sharing




6 comments:

  1. I've used the code and copied the file as instructions but still getting this error

    run:
    USB Library rxtxSerial.dll Not Found
    Exception:java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path:no rxtxSerial in java.library.path
    java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
    java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
    Exception in thread "main" java.lang.NullPointerException
    at sifishserialarduino.MainGUI.(MainGUI.java:61)
    at sifishserialarduino.SifishSerialArduino.main(SifishSerialArduino.java:21)


    can you help me please

    ReplyDelete
  2. OK the error is telling you that Java looked in it's library and didn't find rxtxSerial.dll.

    Assuming you did this step..
    Copied rxtxSerial.dll from ...\arduino-1.0.1-windows\arduino-1.0.1
    C:\Program Files (x86)\Java\jre7\bin and also to
    C:\Program Files (x86)\Java\jdk1.7.0_05\jre\bin (netbeans)

    The tricky part is that you have to put this file in jre\bin directory in the CORRECT JAVA INSTALLATION. When you are running in netbeans it is different from when you run the .jar file directly. Above I put the file in EVERY java install on my machine. It is often hard to figure out which one is being used when you have several.

    If you have done that, there are a couple other things that could be wrong.
    Restart your netbeans, it may not have noticed the file.
    Right click on the rxtxSerial.dll file in windows and click unblock if it is blocked.
    Make sure you are using 32bit java. rxtx is compiled for 32 bit only. I have found a 64 bit version mentioned at the end of the post above, but that is asking for trouble.

    ReplyDelete
  3. Maybe I should back off that last comment. The 64 bit version worked fine as the end of my post said. You can get it here: http://mfizz.com/oss/rxtx-for-java

    ReplyDelete
  4. The people not reading your blogs are missing out a lot of quality contents. registry cleaner

    ReplyDelete
  5. I want to communicate with one arduino and i know some java programming. It's good to see you sharing your knowledge

    ReplyDelete
  6. Be that as it may, be cautious in picking registry cleaner apparatuses on the grounds that some of these items don't be able to settle the issues completely in your windows registry. registry cleaner windows 10

    ReplyDelete