I am trying to display the available ports in my computer using the following codes..
public static void test(){
Enumeration lists=CommPortIdentifier.getPortIdentifiers();
System.out.println(CommPortIdentifier.PORT_SERIAL);
System.out.println(lists.hasMoreElements());
while (lists.hasMoreElements()){
CommPortIdentifier cn=(CommPortIdentifier)lists.nextElement();
if ((CommPortIdentifier.PORT_SERIAL==cn.getPortType())){
System.out.println("Name is serail portzzzz "+cn.getName()+" Owned status "+cn.isCurrentlyOwned());
try{
SerialPort port1=(SerialPort)cn.open("ComControl",800000);
port1.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
System.out.println("Before get stream");
OutputStream out=port1.getOutputStream();
InputStream input=port1.getInputStream();
System.out.println("Before write");
out.write("AT".getBytes());
System.out.println("After write");
int sample=0;
//while((( sample=input.read())!=-1)){
System.out.println("Before read");
//System.out.println(input.read() + "TEsting ");
//}
System.out.println("After read");
System.out.println("Receive timeout is "+port1.getReceiveTimeout());
}catch(Exception e){
System.err.println(e.getMessage());
}
}
else{
System.out.println("Name is parallel portzzzz "+cn.getName()+" Owned status "+cn.isCurrentlyOwned()+cn.getPortType()+" ");
}
}
}
My output is always ;
1
false
It is not looping though the while (lists.hasMoreElements()){
///
)
What could be the possible problems?
You are missing the driver:
Let know if you need more code or this is enough to get you started.
OR
have this file
javax.comm.propertiesin your class pathWith this content:
Driver=com.sun.comm.Win32Driver