Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



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

Identifying the COM,LPT ports in WIndows OS thru Java COMM API  RSS feed

 
Vijay K Natarajan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
For some reason the ports in my windows 2000 machine(also tested in winNT) are not being recognised.
This is the testing code I have(modelled on a sample program from jguru):
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleReadWrite implements Runnable, SerialPortEventListener {
static String DEFAULT_PORT = "COM1";
InputStream in;
OutputStream out;
/***********************************************************************/
public static void main(String[] args) {

String portname = DEFAULT_PORT;
if (args.length > 1) {
System.err.println("Usage: java SimpleReadWrite [port_name]");
System.exit(1);
} else if (args.length == 1) {
portname = args[0];
}
try {
CommPortIdentifier portId =
CommPortIdentifier.getPortIdentifier(portname);
SimpleReadWrite srw = new SimpleReadWrite(portId);
} catch (NoSuchPortException e) {
System.err.println("No such port: " + portname);
System.exit(1);
} catch (PortInUseException e) {
System.err.println("Port in use: " + portname);
System.exit(1);
}
}
/***********************************************************************/
public SimpleReadWrite(CommPortIdentifier portId) throws
PortInUseException {
try {
SerialPort serialPort = (SerialPort)
portId.open("SimpleReadWrite", 5000);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
Thread readerThread = new Thread(this);
readerThread.start();
}catch (IOException e) {
System.err.println("Problem opening streams");
System.exit(1);
}catch (TooManyListenersException e) {
System.err.println("Too Many Listeners");
System.exit(1);
}catch (UnsupportedCommOperationException e) {
System.err.println("Problem setting port parameters");
System.exit(1);
}
}
/***********************************************************************/
public void run(){
byte character;
try {
while (true) {
character = (byte) System.in.read();
out.write(character);
}
}catch (IOException e) {
// Terminate thread
}
}
/***********************************************************************/
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
// If there is data available on serial port,
// read it in in chunks <=20 bytes
//
byte[] readBuffer = new byte[20];
try {
while (in.available() > 0) {
int numBytes = in.read(readBuffer);
}
System.out.print(new String(readBuffer));
}catch (IOException e) {
// Terminate handling this event on read error
}
break;
default:
System.out.print("Unknown SerialPortEvent type = " +
event.getEventType());
break;
}
}
/***********************************************************************/
}

Am I missing anything ? Any help would be greatly appreciated.
Best Regards.
[ January 15, 2002: Message edited by: Vijay K Natarajan ]
[ January 15, 2002: Message edited by: Vijay K Natarajan ]
[ January 15, 2002: Message edited by: Vijay K Natarajan ]
[ January 15, 2002: Message edited by: Vijay K Natarajan ]
 
Vijay K Natarajan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends ,
I tracked the problem . Thought it will be useful for others too if I post this.
The problem was with the location of comm.properties file. For some reason , the comm api was not able to identify it in the <jdk>/lib folder. Instead I moved it to <jdk>/jre/lib folder and it fixed the problem. The other files like the windows dll and comm.jar are in the same location as specified in the installation guide.
Regards,
Vijay
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!