Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

javax.comm can't enum ports  RSS feed

 
Dorj Galaa
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my program can't enumerate ports
please tell me how to enumerate? my OS is Win2000
import java.io.*;
import java.util.*;
import javax.comm.*;
public class ComPortEnum
{
static CommPortIdentifier portId;
static Enumeration portList;
static int i=0;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args)
{
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
System.out.println(portId.getName());
}
}
}
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Well you cant get the ports if you are running your code from a drive other than the one in which you have WINDOWS!!..
So to do that you will need a dll.. I will give you the dll through mail.. but please do mail me @ gkasinath@hotmail.com
Regds
Gautham Kasinath
 
Jamie Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This comes from an example included with the javax.comm class called serial connection. I just thought that it might help you
to get the portId's with out using the enumeration. Sorry about the spacing.
/**
Attempts to open a serial connection and streams using the parameters in the SerialParameters object. If it is unsuccesfull at any step it returns the port to a closed state, throws a
<code>SerialConnectionException</code>, and returns.
Gives a timeout of 30 seconds on the portOpen to allow other applications to reliquish the port if have it open and no longer need it.
*/
public void openConnection() throws SerialConnectionException {
// Obtain a CommPortIdentifier object for the port you want to open.
try {
portId =
CommPortIdentifier.getPortIdentifier(parameters.getPortName());
} catch (NoSuchPortException e) {
throw new SerialConnectionException(e.getMessage());
}
// Open the port represented by the CommPortIdentifier object. Give
// the open call a relatively long timeout of 30 seconds to allow
// a different application to reliquish the port if the user
// wants to.
try {
sPort = (SerialPort)portId.open("SerialDemo", 30000);
} catch (PortInUseException e) {
throw new SerialConnectionException(e.getMessage());
}
// Set the parameters of the connection. If they won't set, close the
// port before throwing an exception.
try {
setConnectionParameters();
} catch (SerialConnectionException e) {
sPort.close();
throw e;
}
// Open the input and output streams for the connection. If they won't open, close the port before throwing an exception.
try {
os = sPort.getOutputStream();
is = sPort.getInputStream();
} catch (IOException e) {
sPort.close();
throw new SerialConnectionException("Error opening i/o streams");
}
// Create a new KeyHandler to respond to key strokes in the
// messageAreaOut. Add the KeyHandler as a keyListener to the
// messageAreaOut.
keyHandler = new KeyHandler(os);
messageAreaOut.addKeyListener(keyHandler);
// Add this object as an event listener for the serial port.
try {
sPort.addEventListener(this);
} catch (TooManyListenersException e) {
sPort.close();
throw new SerialConnectionException("too many listeners added");
}
// Set notifyOnDataAvailable to true to allow event driven input.
sPort.notifyOnDataAvailable(true);
// Set notifyOnBreakInterrup to allow event driven break handling.
sPort.notifyOnBreakInterrupt(true);
// Set receive timeout to allow breaking out of polling loop
// during input handling.
try {
sPort.enableReceiveTimeout(30);
} catch (UnsupportedCommOperationException e) {
}
// Add ownership listener to allow ownership event handling.
portId.addPortOwnershipListener(this);
open = true;
}
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error message are you getting? I ran this code on my Win2K machine successfully. Did you remember to include the win32com.dll file in your <jdk_home>\bin directory? That will cause an unsatisfied link error if you don't.
 
ST Moong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have more than one version of JDK installed? If you do, try running from a batch file. For eg.,

Of course, you need to make sure the dll, jar, and the property files are placed in the correct directory. Refer to the readme for installation guide.
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Well you sure wouldnt be able to list the ports if you are running the second code from a directory other than yer Windows directory..
The work around for the same is to copy the Win32com.dll to Windows\system or Winnt\System32 for Win9x adn WinNT resp.
Then the lines to load the dll will work and not give you the unsatisfied link error.
Regds
Gautham kasinath
my Sample Code that works is :
ListPorts.java
 
umar hathab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Check whether u followed the following steps when installing comm API.
Steps:
------
Unzip the file javacomm20-win32.zip. This will produce a hierarchy with a top level directory commapi.
The examples in this document assume that you have unzipped the javacomm20-win32.zip file in your C: partition and your JDK installation is in C:\jdk1.1.6. If you have installed JDK in an other location or unzipped javacomm20-win32.zip in an other location modify the example commands appropriately.
If you are installing the Java communications API into a JRE (Java runtime environment) follow the same instructions as for the JDK except where noted. See the additional instructions for running using the JRE.
1.Copy win32com.dll to your <JDK>\bin directory.
C:\>copy c:\commapi\win32com.dll to c:\jdk1.1.6\bin
2.Copy comm.jar to your <JDK>\lib directory.
C:\>copy c:\commapi\comm.jar c:\jdk1.1.6\lib
3.Copy javax.comm.properties to your <JDK>\lib directory.
C:\>copy c:\commapi\javax.comm.properties c:\jdk1.1.6\lib
The javax.comm.properties file must be installed. If it is not, no ports will be found by the system.
Add comm.jar to your classpath (do not do this step for a JRE installation).

I hope u will get the available ports after this.
A.umar
 
umar hathab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Check whether u followed the following steps when installing comm API.
Steps:
------
Unzip the file javacomm20-win32.zip. This will produce a hierarchy with a top level directory commapi.
The examples in this document assume that you have unzipped the javacomm20-win32.zip file in your C: partition and your JDK installation is in C:\jdk1.1.6. If you have installed JDK in an other location or unzipped javacomm20-win32.zip in an other location modify the example commands appropriately.
If you are installing the Java communications API into a JRE (Java runtime environment) follow the same instructions as for the JDK except where noted. See the additional instructions for running using the JRE.
1.Copy win32com.dll to your <JDK>\bin directory.
C:\>copy c:\commapi\win32com.dll to c:\jdk1.1.6\bin
2.Copy comm.jar to your <JDK>\lib directory.
C:\>copy c:\commapi\comm.jar c:\jdk1.1.6\lib
3.Copy javax.comm.properties to your <JDK>\lib directory.
C:\>copy c:\commapi\javax.comm.properties c:\jdk1.1.6\lib
The javax.comm.properties file must be installed. If it is not, no ports will be found by the system.
Add comm.jar to your classpath (do not do this step for a JRE installation).

I hope u will get the available ports after this.
A.umar
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!