Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Questions About Talking to Serial Ports

807591Jun 7 2008 — edited Jun 14 2008
I have a GPS receiver that plugs into USB but the driver I'm using for it is a Prolific USB-Serial Port Adapter and sends its signals via a COM port. So because of this, should I be able to use the javax.comm package which is meant specifically for (serial) COM ports? Is that a fair assumption even though it physically attaches via USB?
Next question, what if I use a serial port emulator like Xport3? This basically takes one COM port and splits it up into multiple virtual COM ports.

Now assuming either of the above work, I've already tried them and gotten undesirable results. I'm wondering if anyone here knows a way to troubleshoot beyond simply running the application and scratching my head. I've installed the drivers, plugged in the receiver, watched it show up in Device Manager, and then successfully used the receiver with iGuidance which is GPS software for a computer. I was able to stand outside and get readings like lattitude/longitude/altitude and all those things from the GPS receiver. So basically the driver I'm using does work.

Now I've run the following simple Java applications that simply give no output. Some of them are supposed to print out available COM ports, others are supposed to open ports, neither of them give any sort of output. They compile and run but do absolutely nothing. Here are a few:
import javax.comm.*;
import java.util.*;

public class PortLister {

  public static void main(String[] args) {
    Enumeration e = CommPortIdentifier.getPortIdentifiers();
    while (e.hasMoreElements()) {
      System.out.println((CommPortIdentifier) e.nextElement());
    }
  }
}
import javax.comm.*;
import java.util.*;

public class PortOpener {

  public static void main(String[] args) {

    Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();
    while (thePorts.hasMoreElements()) {
      CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();
      System.out.print(com.getName());

      switch(com.getPortType()) {
        case CommPortIdentifier.PORT_SERIAL:
          System.out.print(", a serial port, ");
          break;
        case CommPortIdentifier.PORT_PARALLEL:
          System.out.print(", a parallel port, ");
          break;
        default:
          // important since other types of ports like USB
          // and firewire are expected to be added in the future
          System.out.print(" , a port of unknown type, ");
          break;
      }
      try {
        CommPort thePort  = com.open("PortOpener", 10);
        System.out.println("is not currently owned.");
        thePort.close(); 
      }
      catch (PortInUseException ex) {
        String owner = com.getCurrentOwner();
        if (owner == null) owner = "unknown";
        System.out.println("is currently owned by " + owner + ".");
      }
    }
  }
}
import javax.comm.*;
import java.util.*;

public class PortTester {

  public static void main(String[] args) {

    Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();
    while (thePorts.hasMoreElements()) {
      CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();
      System.out.print(com.getName());

      switch(com.getPortType()) {
        case CommPortIdentifier.PORT_SERIAL:
          System.out.println(", a serial port: ");
          break;
        case CommPortIdentifier.PORT_PARALLEL:
          System.out.println(", a parallel port: ");
          break;
        default:
          // important since other types of ports like USB
          // and firewire are expected to be added in the future
          System.out.println(" , a port of unknown type: ");
          break;
      }

      try {
        CommPort thePort = com.open("Port Tester", 20);
        testProperties(thePort);
        thePort.close();
      }
      catch (PortInUseException ex) {
        System.out.println("Port in use, can't test properties");        
      }
      System.out.println();
    }
  }

  public static void testProperties(CommPort thePort) {

    try {
      thePort.enableReceiveThreshold(10);
      System.out.println("Receive threshold supported");      
    }
    catch (UnsupportedCommOperationException ex) {
      System.out.println("Receive threshold not supported");   
    }

    try {
      thePort.enableReceiveTimeout(10);
      System.out.println("Receive timeout not supported");      
    }
    catch (UnsupportedCommOperationException e) {
      System.out.println("Receive timeout not supported");   
    }
    
    try {
      thePort.enableReceiveFraming(10);
      System.out.println("Receive framing supported");      
    }
    catch (UnsupportedCommOperationException e) {
      System.out.println("Receive framing not supported");   
    }
  } 
}
I've added comm.jar to the /bin/ext folder, I've added the win32com.dll file to the /lib folder and I added the .properties file to the /bin folder just like I was told in the installation instructions and everything compiles/runs fine. There's just zero output. Is it because it's plugged in by USB? Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 12 2008
Added on Jun 7 2008
7 comments
246 views