📄 simpleeventlistener.java
字号:
package org.firstopen.singularity.devicemgr.interrogator;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SimpleEventListener implements SerialPortEventListener {
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;
Log log = LogFactory.getLog(this.getClass());
/**
* @return Returns the outputStream.
*/
public OutputStream getOutputStream() {
return outputStream;
}
/**
* @param outputStream The outputStream to set.
*/
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
/**
* @return Returns the serialPort.
*/
public SerialPort getSerialPort() {
return serialPort;
}
/**
* @param serialPort The serialPort to set.
*/
public void setSerialPort(SerialPort serialPort) {
this.serialPort = serialPort;
}
/**
* @param port
*/
public SimpleEventListener(SerialPort port) {
super();
serialPort = port;
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Method declaration
*
*
* @param event
*
* @see
*/
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:
ByteArrayOutputStream message = new ByteArrayOutputStream();
try {
/*
* Refresh the reader buffer, each call
*/
byte[] readBuffer = new byte[64];
int numBytes = 0;
while (inputStream.available() > 0) {
numBytes = inputStream.read(readBuffer);
message.write(readBuffer, 0, numBytes);
log.debug("Bytes Read = " + numBytes);
log.debug("message is: " + message);
}
System.out.println(message.toString());
} catch (IOException e) {
log.error("Failed to read from serial input stream ", e);
}
break;
}
}
/**
* @return Returns the inputStream.
*/
public InputStream getInputStream() {
return inputStream;
}
/**
* @param inputStream The inputStream to set.
*/
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -