我创建了一个应用程序,使用RxTx库从Swing中的多个通信端口读取数据。从一个通信端口读取数据工作正常,但另一个显示端口正在使用异常,即使我试图从同一窗口中的另一个端口读取。
public final void ReadData() {
byte[] readBuffer = new byte[40];
try {
serialPort = (SerialPort) portId.open("Main", 2000);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error " + e);
}
try {
inputStream = serialPort.getInputStream();
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error " + e);
}
try {
serialPort.addEventListener((SerialPortEventListener) this);
} catch (TooManyListenersException e) {
JOptionPane.showMessageDialog(null, "Too many listeners for data");
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
JOptionPane.showMessageDialog(null, "Error " + e);
}
readThread = new Thread((Runnable) this);
readThread.start();
}