We help IT Professionals succeed at work.

javax.comm port communication

javamanpro
javamanpro asked
on
I am trying to establish a connection on a serial port (COM 1) but its just skips thru the enumeration and does not find any ports at ALL! What is wrong here? I am running this on a NT 4.0 machine and I have been able to establish a communication using a program Hyperterminal and the swiped information comes thru. Experts please advise.

import javax.comm.*;
===============Here is the code=====================
  public static void main(String[] args) {
    // First establish a connection to the Serial Port that will provide the
    // swiped information:

    Enumeration portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
      portId = (CommPortIdentifier) portList.nextElement();
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        if (portId.getName().equals("COM1")) {
          try {
            serialPort = (SerialPort)portId.open("ATM", 2000);
            } catch (PortInUseException e) {}
          try {
            outputStream = serialPort.getOutputStream();
            } catch (IOException e) {}
          try {
            serialPort.setSerialPortParams(9600,
            SerialPort.DATABITS_7,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_EVEN);
            } catch (UnsupportedCommOperationException e) {}
          try {
            outputStream.write(messageString.getBytes());
            } catch (IOException e) {}
          }
        }
      }

    if (portId == null)
      System.out.println("No communication ports found on the system!!");

===============End of the code=====================
Comment
Watch Question

Jim CakalicSenior Engineer

Commented:
As explained in the javax.comm FAQ:

In order for the Java communications API to find ports the file javax.comm.properties must be in the correct place. The preferred location is in <jdk>/lib. See the installation instructions for alternate locations and further information.

Jim
hey javaman,

first try to print what are the ports available on your machine.I am thinking that either there is no COM1 port on your machine.That's why i am telling try to print ports on console.

further queries
suresh

Author

Commented:
As I said before. I used hyperterminal and connected to COM1 at the specified port and it works and reads the device input.
Senior Engineer
Commented:
Again, from the Java Communications API FAQ (http://java.sun.com/products/javacomm/javadocs/CommAPI_FAQ.txt)

Q: My application does not find any ports when it enumerates available ports.
Q: BlackBox gives me a message that says "No serial ports found!"

A: In order for the Java communications API to find ports the file
   javax.comm.properties must be in the correct place. The preferred location
   is in <jdk>/lib. See the installation instructions for alternate locations
   and further information.

This answer should work for you if you are using JDK 1.2. If you're using JDK 1.3, try putting the properties file in <jdk>\jre\lib. Here, <jdk> refers to the directory in which the JDK was installed. Usually something like C:\jdk1.2 or C:\jdk1.3 being the default.

You might also look at the solutions offered in these JDC Discussion Forum threads:
http://forums.java.sun.com/thread.jsp?forum=8&thread=14209
http://forums.java.sun.com/thread.jsp?forum=31&thread=154932
http://forums.java.sun.com/thread.jsp?forum=14&thread=24208
http://forums.java.sun.com/thread.jsp?forum=31&thread=40363

Sorry to not give a more exact answer. Its been a long time since I messed with this package and haven't had time to install it recently to work out the problems. Could you answer:
- What version of the JDK are you using?
- Where did you install?

I'll take some time and do the install as soon as I have a chance.
Jim

Author

Commented:
Using JDK 1.2.2
Installed into c:\jdk1.2.2\lib

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for jim_cakalic .

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer
Jim CakalicSenior Engineer

Commented:
Thanks. :-)
Jim

Explore More ContentExplore courses, solutions, and other research materials related to this topic.