Arduino <--> Java using javax.comm under Ubuntu

Greetings!

This is my first post here and, before anything else, I have to warn you that I’m moving my really really first steps in the programming and Linux world, as an university student.

Let’s talk about the problem:

The javax.comm utilities should be correctly installed, since every test code provided with the package works correctly.

So, I tried to write down a very simple code, just to see if the port where Arduino is connected would show. Obviously, it doesn’t.
This is the code I wrote:

import javax.comm.*;
import java.util.*;

public class Serial {

      static CommPort serial;
      static CommDriver driver;
      static Enumeration portList;
      static CommPortIdentifier serialId;


      public static void main(String args[]) {
            portList = CommPortIdentifier.getPortIdentifiers();
            

            while(portList.hasMoreElements()) {
                  serialId = (CommPortIdentifier) portList.nextElement();
                  System.out.println(serialId.getName());
            }
      }
}

And this is the output I get:

gabriele@desktop:~/ProveJava$ java Serial
/dev/ttyS0
/dev/ttyS1
/dev/parport0

Arduino is connected to the /dev/ttyUSB0 port, which is not listed by the getCommPortIdentifiers() method.
If I try to change the code in order to request the port using directly his name, it throws a “NoSuchPortException”.

Is important to know that, using minicom, the port is immediatly recognized and the stream is correctly printed as it is in the built-in serial monitor coming with the Arduino software.

Hope you will help me, please excuse me for my bad english and for any mistake I could have made. :slight_smile:

Bye bye!

Gabriele

Have you tried installing Processing? It's a Java-based programming package that interfaces very well with the Arduinos. It includes several sample sketches that use the serial port to communicate with the Arduino. Each sketch include a function call to get a list of com ports. That list should give you some idea what ports are available, and what their names are.

Thank you PaulS, but finally I made the javax.comm work with the Arduino!! :smiley:

I’ll write you how, hoping it could be useful for someone with the same problem.
Solution is very simple: you just have to edit the javax.comm.properties file, the one they talk about in this part of the libraries platform specific README file:

Copy the file javax.comm.properties to your /jre/lib or your /lib directory.

If you don’t have write permission to /jre/lib or /lib, you can keep
javax.comm.properties in the same directory as comm.jar. The search order for
javax.comm.properties is:

  1. /lib
  2. the directory that contains the first valid comm.jar that is included in the classpath.

[ch12288]

The javax.comm.properties file must be installed. If it is not, no ports will be found by the system.

Open the javax.comm.properties file with admin rights and just add this line: serpathX = path of Arduino serial port (where X is the first integer after the last one already written in the properties file).

So, for example, my file looks like this:

# Paths to server-side serial port devices
serpath0 = /dev/ttyS0
serpath1 = /dev/ttyS1
serpath2 = /dev/ttyUSB0

Save, and now your getCommPortIdentifiers() method will show the port you added.
Communication tested with the example program “Serial Demo” contained in the javax.comm library. EVERYTHING WORKS!!! ;D

Thank you all and hope this will help!!

See you!!

Thank you very much! I was looking just for that solution, but using rxtx.org and mac. I hope it works!