Hi,
I am using javax.comm and am trying to listen on multiple serial ports (COM1 and COM2).
I have found that calling addEventListener() on more than one serial port (different ports) causes the program to stop listening to anything. The code example below works if you remove the line serialPort2.addEventListener(this);
Any help would be greatly apprecialted?
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
public class DummyListener implements Runnable, SerialPortEventListener
{
static CommPortIdentifier portId1;
static CommPortIdentifier portId2;
static Enumeration portList;
InputStream inputStream1;
InputStream inputStream2;
SerialPort serialPort1;
SerialPort serialPort2;
Thread readThread;
public static void main(String[] args)
{
System.out.println("Main");
try
{
portId1 = CommPortIdentifier.getPortIdentifier("COM1:");
portId2 = CommPortIdentifier.getPortIdentifier("COM2:");
DummyListener reader = new DummyListener();
}
catch (NoSuchPortException ex)
{
System.out.println("No such port " + ex.toString());
}
}
public DummyListener()
{
try
{
serialPort1 = (SerialPort) portId1.open("MainClassApp", 2000);
System.out.println("Got port: " + serialPort1.getName());
serialPort2 = (SerialPort) portId2.open("MainClassApp", 2000);
System.out.println("Got port: " + serialPort2.getName());
}
catch (PortInUseException e)
{
System.out.println("Port in use " + e.toString());
}
try
{
inputStream1 = serialPort1.getInputStream();
System.out.println("Got input stream1");
inputStream2 = serialPort2.getInputStream();
System.out.println("Got input stream2");
}
catch (IOException e)
{
System.out.println("IO Exception " + e.toString());
}
try
{
serialPort1.addEventListener(this);
System.out.println("Added listener 1");
serialPort2.addEventListener(this); //Removing this line makes it work?
System.out.println("Added listener 2");
}
catch (TooManyListenersException e)
{
System.out.println("To Many Listeners " + e.toString());
}
serialPort1.notifyOnDataAvailable(true);
System.out.println("Notify on data available 1");
serialPort2.notifyOnDataAvailable(true);
System.out.println("Notify on data available 2");
try
{
serialPort1.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
System.out.println("Set serial params 1");
serialPort2.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
System.out.println("Set serial params 2");
}
catch (UnsupportedCommOperationException e)
{
System.out.println("Unsupported Comm Operation " + e.toString());
}
readThread = new Thread(this);
readThread.start();
}
public void run()
{
try
{
Thread.sleep(20000);
System.out.println("Started thread");
}
catch (InterruptedException e)
{
System.out.println("Thread Interrupted " + e.toString());
}
}
public void serialEvent(SerialPortEvent serialPortEvent)
{
SerialPort port = (SerialPort)serialPortEvent.getSource();
System.out.println("Serial Event Called on " + port.getName());
}
}