1

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. I also added comm.jar in the library and i am using windows xp (service pack 3). Kindly look into the code and help me.

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

public class Main
{
    public static void main(String[] args)
    {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        System.out.println(ports.hasMoreElements());
        while(ports.hasMoreElements())
        {
            CommPortIdentifier cpi =
                           (CommPortIdentifier)ports.nextElement();
            System.out.println("Port " + cpi.getName());
        }
    }
}

Thanks in advance..

Ramesh Manly
  • 113
  • 9
  • Do you have the `dll` also on the library path ? – fyr Dec 22 '11 at 10:14
  • Are the commports above 4? e.g. com17: If so, you have to tell the windows commapi the ports yourself (sorry no code in the moment, have no access to my sources). Another problem in windows are too long paths and underscores!!! in the path to commapi. Try using a short path and copy the dll in your working dir. – Andreas Dec 22 '11 at 10:17
  • No, i dont use any dll. Can you point me on which dll to use? – Ramesh Manly Dec 22 '11 at 10:17
  • The communication API uses RMI to reach comports. The native part is in the win32com.dll (s. answer from @fyr) The DLL have to be in the librarypath. The directory from which you start your application is automaticly in the librarypath. The javax.comm.properties must be reachable too. The jar, dll and properties file should be in your downloaded package. – Andreas Dec 22 '11 at 10:39
  • Maybe your better of with rxtx:http://rxtx.qbang.org/wiki/index.php/Main_Page – Andreas Dec 22 '11 at 10:45
  • http://www.knopflerfish.org/releases/3.2.0/docs/jars/comm-win32/comm-win32_all-2.0.1.html here i am download that package.But in that package don't have the properties file.Can you share me that link. – Ramesh Manly Dec 22 '11 at 10:50
  • Thanks. I use this link rxtx:rxtx.qbang.org/wiki/index.php/Main_Page – Ramesh Manly Dec 22 '11 at 11:14
  • @RameshManly http://llk.media.mit.edu/projects/picdev/software/javaxcomm.zip – fyr Dec 22 '11 at 11:26
  • how to send data throw COM Port – Ramesh Manly Dec 22 '11 at 11:29
  • @RameshManly http://stackoverflow.com/questions/900950/how-to-send-data-to-com-port-using-java – fyr Dec 22 '11 at 11:46

5 Answers5

1

you just import "import gnu.io.*;" and RxTxcomm.jar add into libraries.

1) Download Rxtx binaries from http://rxtx.qbang.org/wiki/index.php/Download
2) Extract the zip file.
3) Copy RXTXcomm.jar ---> <JAVA_HOME>/jre/lib/ext
4) Copy librxtxSerial.so ---> <JAVA_HOME>/jre/lib/i386/
5) Copy librxtxParallel.so ---> <JAVA_HOME>/jre/lib/i386/

tmwoods
  • 2,353
  • 7
  • 28
  • 55
1

There are some tricks involved in getting the Java Communications API to correctly interact with the Windows system. Among the items that you download from Sun are three very important files:

comm.jar  
win32com.dll  
javax.comm.properties  

For the JVM to recognize the serial ports, proper placement of these files is important.

Use the following installation methods to be effective:
comm.jar should be placed in:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib/ext    

win32com.dll should be placed in:

%JAVA_HOME%/bin   
%JAVA_HOME%/jre/bin  
%windir%System32  

javax.comm.properties should be placed in:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib
Igor Jerosimić
  • 13,621
  • 6
  • 44
  • 53
Mohsin
  • 852
  • 8
  • 28
0

Thats THE prototypical problem with the commapi :-)

Read through the installation guide, beside javacomm.api or wossname, two or three other files have to be copied to a very specific location.

All of them.

Otherwise you simply don't see any com ports, error handling's a bit on the weak side, sorry, I went through the same thing about thrice myself ;-)

yeoman
  • 1,671
  • 12
  • 15
-1
%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib/ext    

win32com.dll should be placed in:

%JAVA_HOME%/bin   
%JAVA_HOME%/jre/bin  
%windir%System32  

javax.comm.properties should be placed in:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib  //this work out for me by sann seun
Abubakr Dar
  • 4,078
  • 4
  • 22
  • 28
-2

comm.jar should be placed in:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib/ext 

win32com.dll should be placed in:

%JAVA_HOME%/bin   
%JAVA_HOME%/jre/bin  
%windir%System32

javax.comm.properties should be placed in:

%JAVA_HOME%/lib  
%JAVA_HOME%/jre/lib  //this work out for me by sanni seun
Avi
  • 21,182
  • 26
  • 82
  • 121