Can't get arduino to communcicate with Java

I'm using this this really simple program to test the USB connection between my pc and my arduino uno. The arduino IDE says the arduino is connected to the pc an port COM4, and I can upload programs just fine. When I try to run that program it throws the (expected, custom) error "Expected port, COM4, not found". I tried putting a debug print at line 29, but that line is never reached, because the CommPortIdentifier.getPortIdentifiers(); metod returns an empty Enumeration. I have managed to get the port working, but that was only once and it stopped working the second time I tried. I closed the arduino IDE to make sure the port in not in use.

Can someone help me?

Are you using the RXTX library? Have you tried the newer, easier to use, JSSC?

I have got both to work using JRuby but I steer well clear of the Java language.

Are you sure the Arduino IDE has released the port, though I never had a problem with that (but I am using Linux).


Yes I'm using the RXTX library, I'll look into JSSC. I would however still like to know how to get this working with RXTX. Do you have any idea how to fix the problem you're talking about?

how to fix the problem you're talking about?

YOU are the person talking about a problem :slight_smile:

Sorry, I know little or nothing about the Java language. I may use your code to do some learning.

My general approach to problem solving would be to write code to print all the available ports. Until that shows up something useful there is not much point going further. For what it is worth, there is a JRuby example here (in addition to the Python version).


Are you sure the Arduino IDE has released the port

I meant that problem.

And yes, I did that and I didn't find any ports.

I can’t get your code to compile. What else do I need, apart from RXTXcomm.jar (which I have).

AND the JSSC code from here worked first time


You need to set this as a VM option, pointing to the location of rxtxSerial.dll on your pc:

-Djava.library.path="D:\Install\Java Projects\Arduino\mfz-rxtx-2.2-20081207-win-x64"

I downloaded rxtxSerial.dll from here, but you probably have that file already.
Be careful to scroll down on that download page if you have a 64 bit operating system.

And what things do I need setup for JSSC?

And what things do I need setup for JSSC?


Really, that's all.
By the way, it's not a drop-in replacement for RXTX - you will need to change your code.


By the way, it's not a drop-in replacement for RXTX - you will need to change your code.

Offcourse, that's not a problem... I'll test it out tomorrow.

Actually, it turned out to be a problem on my part. When I ran my program for the first time yesterday, I didn't have the line


The next time I ran it it couldn't find the port because it wasn't closed. When I restarted my pc (today) the ports were cleared and it all works now!

Thank you for the help, it was definitely appreciated.

Development of RXTX seems to have stopped so if you are starting a new project it may be a good idea to use JSSC.


Ok, fine you win :wink: . I'll start switching over now...

I'm using JSSC right now, and I have an issue.

This is the code running on the arduino:

And this is the java code:

I have a led connected on port 4 on the arduino. The led on the arduino is expected to go on whenever the arduino is receiving any data, and it should stay on longer if there's more data. I get that behavior when I type text in the Serial Monitor in the arduino IDE, but not when I run my Java program. But I see the small led on the board itself flicker, so there is clearly some communication going on.

I don't know what the the diffrence between the two is, can someone help me?


That is the culprit.

When the PC program opens the serial port it causes the Arduino to reset. It must wait for a few seconds before sending anything - to allow the reset to complete. If you close the port this keeps happening. Leave the port open until you are completely finished with the Arduino.

It is also a good idea to put Serial.println("Arduino is Ready"); (or something similar) in setup() and make the PC program wait until that is received before trying to send anything.

This technique is in the JRuby and Python code in the link I gave you earlier.


PS please post code directly in your post so I don't have to go elsewhere to get it.

Thanks, it works!

I'll post code here in the future. On other forums the rules are to post code on a website with syntax highlighting, Pastebin or Github, so I thought it would be the same here.

post code on a website with syntax highlighting,

Glad it works.

This Forum doesn’t do syntax highlighting, and it would be nice if it did - but there are many other higher priority problems.

For anything beyond a few lines I find it much easier to copy the code into my text editor so I can use its search and highlight features. I still need to to that with Github or whatever. When code is in a code box here I can select it all with one click.