Serial communication with Java and Arduino Leonardo

On my Arduino UNO the example here
http://playground.arduino.cc/Interfacing/Java
works just fine.

Here is my Arduino Code:

void setup() {
  Serial.begin(9600);
  //Serial1.begin(9600);
}

void loop() {
  Serial.println("ping");
  //Serial1.println("ping1");
  delay(1000);
}

The Java code is the same as in the example. I only changed the PORT_NAMES variable to the COM Port of the Uno (in my case it's COM7).

If I upload the code and run the Java program, I get the following output:

WARNING:  RXTX Version mismatch
	Jar version = RXTX-2.2pre1
	native lib Version = RXTX-2.2pre2
trying to connect to port COM7
Started
ping
ping
...

Which is what I expected.

However, if I upload the code to the Leonardo the Java program apparently does not receive any messages.
I have changed the PORT_NAMES variable to the COM Port of the Leonardo (COM9).
I even tried to uncomment the lines that use Serial1.
I can however, receive data sent from the Java program to the Leonardo.

I noticed that if I send data from the Java program to the Leonardo, the RX led blinks. However, if I send data from the Leonardo RX and TX leds are off.

Why does sending data from the UNO to the PC work, but not from the Leonardo?

It takes some time for the Leonardo to establish a serial connection but I have only got one a short time and I haven't tried connecting it to a PC program other than the Serial Monitor.

Have you tried starting the Leonardo 10 or 20 seconds before trying the Java program?

Edit to add ...

I just tried a JRuby program that I already had which works with an Uno and Mega. It worked fine with the Leonardo after I adjusted the port reference. (By the way I'm using Unbuntu Linux). However it wouldn't run a second time and I think that's because opening the Serial port on the PC doesn't cause the Leonardo to reset and my code relies on a message that is sent from setup() to let it know the Arduino is ready. JRuby is slow to start so it probably gives plenty of time for the Leonardo to reset after I press the reset button. (And in case you aren't familiar with it JRuby runs on the JVM). (And one other thing, my JRuby program uses JSSC rather than RXTX but I doubt if that makes any difference).

...R

I've encountered the same problem for my skecth to interface with Ardulink (http://www.ardulink.org). You can solve adding a simple while in the setup method after Serial.begin statement:

Serial.begin(115200);
while(!Serial); // Wait until Serial not connected (because difference between Leonardo and Micro with UNO and others)

Leonardo or Micro have a communication schema a bit different because they USB capabilities. Maybe you can find some detail here: http://arduino.cc/en/Main/ArduinoBoardLeonardo

Ardulink:
You can solve adding a simple while in the setup method after Serial.begin statement:

I suspect you need something similar in the PC software because (I think) there won't be a serial port to connect to until the Leonardo has sorted itself out.

...R

I just tried adding while(!Serial); int the sketch and several Thread.sleep in the Java program.
Unfortunately, that did not change a thing. I still can't receive any data and the TX led is not blinking (should it anyway?).

Are there any other things I should try?

In order to understand if your Leonardo is working you can try to:

  • download my software from here: Ardulink
  • upload sketch for Leonardo and Micro you'll find inside the distribution
  • start Ardulink Console and try to turn on/off the led

I've tested my software with a Micro (check Ardulink compatibility list from here: Ardulink) but it should work with a Leonardo too. So if Ardulink doesn't work maybe your Leonardo has some problem.

Let me know
Luciano

Is there a 64 bit version?
I get the error "Can't load IA 32-bit .dll on a AMD 64-bit platform" if I click on the Discover button.

Take RXTX dll (rxtxSerial.dll) you find under your Arduino IDE distro and copy it in the Ardulink folder, replacing 32 bit version.

Thank for the feedback!!!

Ardulink works now.
The LED on pin 13 turns on and off as I press the button on the switch panel.

However, if I go to the digital sensor panel and turn on the sensors I get no feedback.
I tried it with two Leonardo boards.

1 Like

Good,

so to interface your Leonardo with Java you can:

  • Read Ardulink library code or
  • Use Ardulink library (in the site you can find several article to how get started, you can also ask to me if something is not clear)

In order to see sensor on/off you have to apply a voltage to the pin you are listening for. You can try this:

  • Take a wire and insert it into pins 13 and 11
  • Turn on listening for pin 11
  • Turn on in swith panel pin 13

Well, I can send messages from Ardulink TO the Leonardo, but I don't get any data FROM it.
I tried connecting the wire from pin 11 to 13 but if I turn on pin 13 I still get no feedback.

I assume it worked for you with a Leonardo. So I guess it's not a problem with the Java program or the sketch.
Maybe it's a Windows and/or 64-bit issue!?
You said you are running on a Ubuntu machine. Is it 32 or 64 bit?

I haven't a Leonardo to try messages from Arduino. However I've uploaded Ardulink sketch for Leonardo in my UNO and it works. So or Leonardo has some issues with memory or it should work. I've written this article to help you http://www.ardulink.org/how-to-manage-input-from-arduino/. Have you setup your hardware like me? Of course Ardulink Console is able to do what I've done with the little application written in the article.

I'll give it a try in the evening.

When I upload the Leonardo sketch to the UNO everything works (Ardulink and Serial communication in both ways).
So there must be an issue with the Leonardo. The Leonardo has a different IC than the UNO, Duemilanove,...

Maybe it handles serial communication differently?

You might be interested in this link which explains how to force a Leonardo to reset.

I have just successfully tested the idea with JRuby and JSSC.

...R

This is a very useful information for me! I always wanted to know how to soft-reset the Arduino.

So you think my problem is related to an "unreseted" board?
If so, should a hardware-reset (pressing the reset button) do the trick as well?
When I tried the serial communication with a Java program, I first connected the Arduino and then started the Java application which listens to the Arduino.
At which point should I reset the Leonardo?

In my JRuby code I did the reset thing (open at 1200 baud and close), waited 10 secs for the Leonardo to go through the reset process and then my JRuby code opened the serial port at the baud rate that Serial.begin(bbb) uses.

I'm using Ubuntu Linux so I can be confident that the same serial port will be used and that is hardwired into my code. I've never bothered to experiment with looking through a list of possible serial ports.

...R

Sketch for Leonardo and UNO are different because Leonardo hasn't the same Serial management. Why my sketch doesn't work for messages from Arduino to PC is difficult to understand because I haven't a Leonardo.

Sorry, if you are some news please contact me.

Robin2:
In my JRuby code I did the reset thing (open at 1200 baud and close), waited 10 secs for the Leonardo to go through the reset process and then my JRuby code opened the serial port at the baud rate that Serial.begin(bbb) uses.

I can open a connection at 1200 Baud and close it immediately. The Leonardo resets as expected.
Then I'm waiting 15 seconds before going on opening the port again at 9600 Baud.

The Leonardo is constantly sending data (Serial.println("test"):wink: in a one second interval.
However it never reaches my program.

Can you confirm that you are actually able to send data via Serial.println from the Leonardo and receive it at the PC?
Is there an RX or TX LED blinking while your Leonardo is sending data to the PC?

Yes.

My PC code (JRuby) sends data to the Leonardo which then sends it back. It's the exact same code I used on a Uno, apart from the added bit to reset the Arduino.

The fact that your code is causing the Leonardo to reset is a positive sign.

You say your Leonardo continually sends the word "test". Can you view that in the Arduino Serial Monitor to be sure it is happening?

It may be helpful to describe the process of my code

After the reset is started it waits 10 seconds
Then it opens the serial port at 57600 baud (but I doubt if the baud rate matters)
Then it waits for a message from setup() in the Leonardo that contains the words "Arduino Ready" (any words will do)
Then it goes through a process of sending data and waiting for a reply
When everything is finished it closes the serial port.

Have you a function that tells you if the PC has received some data from the Arduino?
Have you a function to get a byte from the incoming serial buffer?
Have you code to get bytes and save them in an array until the buffer is empty?
Have you code to pront whatever has been saved to the array?

...R

Ok, For me it seems that with JRuby there is no issue when communicating with the Leonardo. (at least under Ubuntu)

I can see the Serial.println messages in the serial console of the Arduino IDE.
I try the shortcut for all of your "Have you..." questions: I'm using the example code from here
http://playground.arduino.cc/Interfacing/Java
which opens a serial connection as well as a BufferedReader for incoming messages (Arduino --> PC) and an OutputStream for outgoing messages (PC --> Arduino). This example code perfectly works with an Arduino UNO.
Hence, although most of the code you are asking for is under the hood, I can confirm that there is code that is notified if there is incoming data, gets the received bytes and fires an event if the message is complete.
I'm implementing the appropriate EventHandler and print all information available to the console.

You say your TX LED is blinking when you send data from the Leonardo to the PC. In my case it's doing nothing. I tried it with two differend boards. Only the RX LED is blinking when I send messages from the PC to the Leonardo.

Maybe it's a driver-related issue?

I have an old 32-bit laptop with a Fedora 20 installation. I'll have a look if it works there.