0

I'm trying to connect a serial application on ubuntu with Java
After searching and reading resources,I add comm.jar and RXTXcomm.jar in the library.
I use the following code to identify the comports. In my system there are three ports but it is showing false in ports.hasMoreElements() method.
Kindly look into the code and help me.

String wantedPortName = "/dev/ttya";
///dev/ttyS0 و /dev/ttyS1 نیز تست شد
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;  // will be set if port found
while (portIdentifiers.hasMoreElements())
{
    CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
    if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
      pid.getName().equals(wantedPortName)) 
  {
    portId = pid;
    break;
  }
}
if(portId == null)
{
     System.err.println("Could not find serial port " + wantedPortName);
     System.exit(1);
}    

2 Answers 2

1

In my case, I'm using Ubuntu, and my notebook does not have any serial or paralel ports.

So, you have to simulate this kind of port:

apt-get install socat

Run it:

socat -d -d pty,raw,echo=0, pty,raw,echo=0

According to output, pay attention to "devices" created:

2014/02/05 01:04:32 socat[7411] N PTY is /dev/pts/2
2014/02/05 01:04:32 socat[7411] N PTY is /dev/pts/3
2014/02/05 01:04:32 socat[7411] N starting data transfer loop with FDs [3,3] and [5,5]

Stop socat [CTRL]+[C] and symlink it to a location that RXTX will recognise as a device, due to "tty" prefix:

sudo ln -s /dev/pts/2 /dev/ttyUSB02
sudo ln -s /dev/pts/3 /dev/ttyUSB03

Now, run socat again

socat -d -d pty,raw,echo=0 pty,raw,echo=0

Now, using following code, you will see 2 virtual ports:

        Enumeration portList = CommPortIdentifier.getPortIdentifiers();//this line was false
        System.out.println(portList.hasMoreElements());

        while(portList.hasMoreElements()){
            System.out.println("Has more elements");
             CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
               if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    System.out.println(portId.getName());
               }
               else{
                     System.out.println(portId.getName());
               }
        }

system.out:

true
Has more elements
/dev/ttyUSB03
Has more elements
/dev/ttyUSB02
Sign up to request clarification or add additional context in comments.

4 Comments

I followed your instruction but still get false at starting line. not able to entering Has more elements constraint.
Are you using ubuntu? It's a Virtual Machine?
yes ubuntu.. yeah i know above commands i fired from terminal but when i used your following code.. it returns false.
I think it might be about RxTx versions because they changed how they look for serial ports. rxtx.qbang.org/wiki/index.php/Trouble_shooting
0

It looks like you are filtering out the port you want. A /dev/tty is a special character device... not a serial port so

if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
  pid.getName().equals(wantedPortName)) 

should never match your string.

To prove this, try iterating over your available ports. I don't know if a tty can can be detected by RXTX but play with it and let us know.

Ref: http://rxtx.qbang.org/wiki/index.php/Discovering_available_comm_ports

EDIT: So you don't have any serial devices to test with? All I sat is make sure you have everything installed properly, including the properties file as described in this file.

http://rxtx.qbang.org/pub/rxtx/rxtx-2.0-7pre2/INSTALL

Once done, install a null modem emulator or find a serial device to test with.

3 Comments

this if statment is on the while statment and while statment return false . So the program does not execute this if!!!
Sounds like it doesn't like tty! Do you have an RS-232 device or similar to test if rxtx sees it? RXTX can be picky setup and config.
Unfortunately do not have RS-232 device or similar to test rxtx please help me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.