Has Java been forsaken? Where art thou?

I was looking at the interfacing with software section in the Arduino playground to see how I might actively send and receive information to the arduino in real time. I saw a listing of most of the major languages besides one glaring omission... Java. What's happened? It's supposed to the epitome of multi-platform open source programming.

So, I checked out processing and found that the processing preprocessor really just acts like a go-between the "processing" language and the java language(A binding, I think it's called?) and that one could ultimately write in Java. Sweet! Reading on the article, though, it appears that processing uses its own graphics engine(based on OpenGL?) so AWT and Swing couldn't be used. It could however be placed on its own frame, so AWT and Swing components could be used in a different frame.

But all I just want to do is interact with the microprocessor in Java; I don't want processing's graphics and I don't want processing to grab my system around the neck and choke down my frame-rate to a noticeable choppiness. So, I was looking into adapting the Serial class for stand-alone Java use and... it appeared to be tightly integrated with its graphics. I also noticed that the Arduino library for processing was wrapped around processing's finger. The only seemingly non-dependent libraries were rxtxComm.jar and the corresponding .dll for the windows system(and other files for other operating system) and it seems it'd be up to me to write my own Serial libraries with the Arduino and I don't think I'm that knowledgeable nor skilled and learning the requisite knowledge and skills would be a huge investment of time looking at how freakin' large the other libraries were for other languages. :-/

So, I was thinking that using processing and using the "noLoop" function would stop processing's graphics engine from strangling my system, but would the Serial functionality remain intact if that was stopped? And I also noticed that processing required Firmata(Or something like it?) on the Arduino which seemed like it might limit my ability to program the Arduino to compute on its own; in essence, it seemed Firmata just transformed the arduino into a communications device and robbed it of its own independent computations and transferred that responsibility to the PC. Also, according to someone who created the revised version of the SimpleMessageSystem, Firmata takes up half the memory which doesn't sound optimal.

I may be horribly wrong on many different points, so please do not hesitate to correct any mistaken impressions. I'm just wanting to send and receive information to/from the Arduino without choking my system's graphics while allowing the Arduino to do its own computations if so desired, and it seems there should be something available for Java but what's available seems inextricably married to Processing.

I do straight java all the time. The ide comes with rxtx, so I just use those jars/shared libraries that got installed with the ide to talk to the serial port. All of the ide is written in java, so you can streamline your program to use the same preferences.txt (and indeed reuse some of the ide classes) to do things like get the com port you just uploaded the program to. The ide is all swing too.

The code is Arduino Starter Kit kaufen [verschiedene Ausführungen]
the ide starts by running Base, borrow the classpath and definitions from the startup script in your arduino directory.

Here is an example that leverages the existing preferences.txt and arduino classes to connect but is basically a straight up java class:

/* simple Java comm example based on arduino installation 
 via Dave Brink 

Start like you would start Base.java.  Copy the run.bat or shell script and 
modify so this class (or your derivative) is added first 
and run from the arduino installation directory
*/

import processing.app.Preferences;
import processing.app.Serial;

public class Test {

//echo strings sent from the arduino over the serial port
      public static void main(String[] args) throws Exception{
            
            Preferences.init();
            System.out.println("Using port: " + Preferences.get("serial.port"));
            Serial s = new Serial(true);
            while(true){
                  String t = s.readString();
                  if(t != null)
                        System.out.println(t);
            }
      }
}

That's an encouraging sign but I can't say I completely understand. How did you link the Processing library to use in your example code? I don't see Processing anywhere in the arduino installation and I thought it was an entirely separate program/library. And I fail to understand where Base.java comes into play and I haven't really worked with it...

Consider me a n00b with Java. My understanding is limited to two quarters of Java programming classes + whatever experience I have in netBeans.

Also, how do you download the code for Arduino(And Processing if necessary)? It doesn't seem like it's included with my installation but I see that it's online on an SVN repository, but it seems I can only download individual files as opposed to the entire project folder. As you can imagine, individual file downloading would take a loooong time.

I tried opening the SVN on netbeans by doing a subversion checkout, but it keeps saying that it's relocated to another address and it appears the redirected address is the address I typed in. :-?

How did you link the Processing library to use in your example code?

I looked at run.bat and put all the same jar files in the classpath when I was compiling my class and when running it (including adding my class to the classpath). I believe Preferences and Serial wind up in pde.jar, but I put the path to my class first and otherwise left the classpath alone, and changed the class name passed to java to execute it (instead of Base).

Thanks for the comments. I created the appropriate Java file "Test.java" and copied the code to it. I also copied everything inside Arduino's "lib" folder(That's where all the libraries seemed to be contained as was stated in the run.bat file) to the folder that contained the Java class, and I then had to reference the correct libraries using netBean's particular libraries functionality. It all compiled fine and when the program ran, I got...

"init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" java.lang.NoClassDefFoundError: com/apple/mrj/MRJOSType
at processing.app.Preferences.init(Preferences.java:158)
at arduinoone.Test.main(Test.java:31)
Caused by: java.lang.ClassNotFoundException: com.apple.mrj.MRJOSType
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
... 2 more
Java Result: 1
"

... I just checked out the class and it appears it exists... hmmm... I'll link the jars directly instead of referencing their folder..

does so

Oh, it works now! But it doesn't actually seem to do anything when I ran it and upgraded this to my arduino:

/*

  • Blink
  • The basic Arduino example. Turns on an LED on for one second,
  • then off for one second, and so on... We use pin 13 because,
  • depending on your Arduino board, it has either a built-in LED
  • or a built-in resistor so that you need only an LED.
  • http://www.arduino.cc/en/Tutorial/Blink
    */

int ledPin = 13; // LED connected to digital pin 13

void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop() // run over and over again
{
digitalWrite(ledPin, HIGH);
Serial.println("Hello"); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW);
Serial.println("World"); // sets the LED off
delay(1000);
Serial.println(" "); // waits for a second
}

The blinking is just to make sure it's actually executing and the arduino is blinking. After doing some quick line by line testing, it appears the Java program is geting stuck on the line:

Preferences.init();

Need to investigate more... :confused:

Ok, just clicked on the error boxes that said that preferences couldn't be found and realized it was trying to find the preferences text in the lib folder. I'll create a temporary lib folder, garsh...

Hmmm... I'm getting errors that pretty much seem to indicate that I don't have all the folders in the right spot. So I copied all of the Arduino's folders and files into the directory where the project is being kept and I'm waiting for it to finish copying...

I would also look into eclipse, it is a very good java (and others) IDE, you should be able to single step through the source in the debugger and look at values etc. and get to the bottom of the issue quickly.

I haven't messed with netbeans recently but it should also have a source code debugger.

Wow, that sounds like some useful features. I've gone my entire life without learning how to use a debugger and instead have seemed to emulate its features by using programmed in checkpoints(System.out.println("It got to line 34!") and so on...). I should get to know the debugger.

So I basically copied the entire arduino folder into my project folder and the program reached the infinite loop! But then... it didn't seem to receive anything and my arduino's LED continues to blink. :/

Here's the code for the Test.java:

import processing.app.Preferences;
import processing.app.Serial;

public class Test {

//echo strings sent from the arduino over the serial port
      public static void main(String[] args) throws Exception{
                System.out.println("IT started 1!");
            Preferences.init();
                System.out.println("IT started 2!");
            System.out.println("Using port: " + Preferences.get("serial.port"));
                System.out.println("IT started 3!");
            Serial s = new Serial(true);
                System.out.println("IT started 4!");
            while(true){
                  String t = s.readString();
                  if(t != null)
                        System.out.println(t);
            }
      }
}

And here's the arduino's code:

int ledPin = 13;                // LED connected to digital pin 13

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);
  Serial.println("Hello");   // sets the LED on
  delay(1000);                  // waits for a second
    digitalWrite(ledPin, LOW);
  Serial.println("World");    // sets the LED off
  delay(1000);
  Serial.println(" ");  // waits for a second
}

And here's the static output I'm seeing:

"
IT started 1!
IT started 2!
Using port: COM4
IT started 3!
Stable Library

Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
IT started 4!
"

And it doesn't receive anything. Hmmm...

Try adding a Serial.begin(9600) to setup().

Also check if you can see the output on the serial monitor screen in the ide (might have to play with the baud).

Here was my test script:

void setup(){
  Serial.begin(19200);
}

void loop(){
 Serial.println("Hello world");
delay (1000); 
}

At 19200, I got this..

"?Æ?
áÅô
D¬æô
¡ÅôDÄæøá¥ô?ÄæôáÅô?Äæô¿Åô?Äæô
á­ôDľô¿ÅôD¬æôáÅôDĽô±ÅôDĽôáÅôD­æôáÅô
DÄæ
ôáÅüD¬æôáÅôDĶô
áÅô
DĦôá­ô
?Äæô
áÅü
D¬æô
áÅô
DĦô
±Åô
D¬æôáÅôDĶôá¥ô?Äæô
á¥ô
DÄæø
áÅô
D¤æô
áÅý
D¤æô
áÅô
DÄæô
áÅô
ÄÄæô¡ÅôD¬æôáÅôDÄæôáÅôDĦôáÅô
?Äæô
áÅô
DĦô
áÅô
?Äæô
áÅô
DÄæüá®ôD¬æôáÅôD¤æô
áÅô
DÄæô
¡Åô
DÄæô
áÅü
DÄæø
áÅô
DÄæø
áÅô
"

@9600

"Hello
Wo
rld Hello World Hello
World
Hello
World Hello World
Hello
World
He
llo World Hello World
Hello
World
Hello
World
Hello
Worl
d Hello World Hello World
Hello
World
Hello
World Hello
World Hello World
Hello
World
Hello
World
Hello "

I can see recognizable characters and words now! I just wonder why it doesn't seem to be formatted correctly as I would expect from a regular java program.

@14400

"`¥êAçÁý
ùèçÁý(ÙùèçÁý(ÙùèçÁý
ùè
çÁý
ùè
çÁý
ùè
çÁ*
ý(ÙùèçÁý(ÙùèçÁý
ùè
çÁ
ý
ùè
çÁ
ý
ùè
çÁ
ý
"
@28800

";;
: {ü: {ü: {ü:

:

:

:

:
{ü: {ü:

:

:
"

@ 38400

"ÎþÏÚÎÏ
ÚÎÏ
ÚäÏ
ÚÎ
å
ÚÎ
Ï
ÚÎ
Ï
Úæ
ç
ÚÎ
Ï
Úå
ÏÚä
Ï
ÚÎ
Ï
Úî
Ï
ªÎ
ÏÚæÏÚÎ
Ï
Úæ
Ï
ÚÎ
Ï
Úî
Ï
Úî
Ï
"
@57600

"8
ü
½ÿ
ü
½þ
ý
½
þÿ½þü
¼ÿ
ÿ
½þ
ü
½þ
ü
¾þ
ý
¼ÿ
ü
"
@115200

"ÿ
é
ì
é
è
é
é
é
é
éèé
ì
"
@4800

"
??f?à?à?þ?
" (Those are actually on their own separate lines in the program.)

@2400

... It doesn't seem like it wants to copy and paste. :-?

@1200

... That doesn't seem like it wants to copy and paste either.

@300

... surprise, surprise. The previous three consisted of question marks and rectangular boxes, with rectangular boxes predominating the lower the baud rate.

It looks like 9600 is the best! But not a winner. I wonder why the formatting isn't being sent over correctly?

(And, oh, the serial rate of the computer over the port is started by the Serial object in the Serial code that I'm guessing was borrowed from the Processing object? So I'm guessing a quick peek at Processing's serial code might put a rest to it... or it might be programmatically decided. :/)

Anyways, the code for Processing is at http://dev.processing.org/source/index.cgi/ .

I checked the defaults and they're...

" // defaults

static String dname = "COM1";
static int drate = 9600;
static char dparity = 'N';
static int ddatabits = 8;
static float dstopbits = 1;
"

9600 does look like the closest to the computer's rate.

Anyways, the full Serial code for Processing's Serial code is at http://dev.processing.org/source/index.cgi/trunk/processing/serial/src/processing/serial/Serial.java?rev=4823&view=log .

I was analyzing the 9600 code more deeply and... it seemed like there was some randomness. But computers aren't random, are they?! :stuck_out_tongue:

I changed my example to 9600 after seeing it was the default value.

Ok.

I was looking at your new guide and followed it and it seemed like there were no formatting errors when running that code from the command prompt. I'm trying to figure out why there's none with that code but there's some with mine in netBeans, but I don't understand the purpose of '%1 %2 %3' in the bat file (Let alone REM, but that doesn't seem like it'd be the crux of the solution). What does that do?

Nice noting that you're not going to teach Java. :stuck_out_tongue: I wonder if anyone in particular had sparked that warning. :smiley:

Sweet, it works :slight_smile:

Those %1, %2, %3 are just parameters to testrun.bat.

When you type "testrun jikes Test.java"
%1 is replaced with "jikes" and %2 is replaced with "Test.java"

When you type "testrun java Test"
%1 is replaced with "java" and %2 "Test"

REM is Remark, you are commenting out that line is all