Serial connection with Vista problem
I ended up piecing together code to open a serial port on my machine for use with my X10 hardware. I have pasted the code below. When I run it, I get a error saying the port can't be opened because it is in use by windows (don't have the exact error handy) but it won't say what is using it. I think its Vista doing somthing to protect the system because I am not using COM1 or COM2 on my machine.
I took the exact same code and set it up on my work machine running XP, it worked fine, and even took the port over another program using it currently. Does anyone have any idea what could be done with Vista to allow me to use the port? or any ideas for what I could do, short of installing XP on a machine to write this code?
Thanks
import javax.swing.JOptionPane;//used for GUI
import java.util.Scanner;//used for scanner
import javax.comm.*;
import java.io.*;
import java.util.*;
//import com.micheldalal.x10
publicclass test
{
publicstaticvoid main(String[] args)
{
/*
try
{
String driverName = "com.sun.comm.Win32Driver"; // or get as a JNLP property
CommDriver commDriver = (CommDriver)Class.forName(driverName).newInstance();
commDriver.initialize();
}
catch (Exception e)
{}
*/
String wantedPortName ="COM3";
System.out.println("comm name");
//
// Get an enumeration of all ports known to JavaComm
//
Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
System.out.println("comm name2");
//
// Check each port identifier if
//(a) it indicates a serial (not a parallel) port, and
//(b) matches the desired name.
//
CommPortIdentifier portId =null;// will be set if port found
System.out.println("null portID");
while (portIdentifiers.hasMoreElements())
{
System.out.println("Element");
CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
pid.getName().equals(wantedPortName))
{
portId = pid;
System.out.println("Found Port " + portId);
System.out.println(portId.getCurrentOwner());
break;
}
System.out.println(pid.getName());
}
System.out.println("end while");
if(portId ==null)
{
System.err.println("Could not find serial port " + wantedPortName);
System.exit(1);
}
//
// Use port identifier for acquiring the port
//
//
// Use port identifier for acquiring the port
//
SerialPort port =null;
System.out.println("serial null");
try{
System.out.println("trying to open port");
port = (SerialPort) portId.open(
"name",// Name of the application asking for the port
10000// Wait max. 10 sec. to acquire port
);
}catch(Exception e){
System.err.println("Port already in use: " + e);
System.exit(1);
}

