我创建了一个通过串口读取数据的Java线程。代码的主要部分如下:
public class dthread implements Runnable{
public dthread(CommPortIdentifier portId)implements SerialPortEventListener {
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {System.err.println(e.toString());}
}
public void serialEvent(SerialPortEvent oEvent){
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
String inputLine =null;
try {
// String inputLine=null;
if (input.ready()) {
inputLine = input.readLine();
System.out.println(inputLine);
}
} catch (Exception e) {System.err.println(e.toString());}
}
}
public void run() {}
}
portId - 包含commportidentifier信息。关于连接的COM端口;来自另一个班级。
public dthread(CommPortIdentifier portId)implements SerialPortEventListener {
显然,这是错误的。我应该如何在我的代码中实现SerialPortEvent?