以下是代码段
我在这里发起一个动作监听器
try {
port_seleted.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println("too many Listeners!");
}
port_seleted.notifyOnDataAvailable(true);
当我收到数据时,会调用以下方法
public void serialEvent(SerialPortEvent Ack_Rec) {
boolean first_flash = false;
if (Ack_Rec.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
while (input_data.available() > 0) {
input_data.read(rec_ack);
}
} catch (IOException e) {
System.out.println("IO Exception in SerialEvent");
}
我没有正确收到数据,即
如果我发送一些数据为“你好今天你好吗”
收到“Hello h”“ow”“你是”“doin”“g今天”
,即串行事件方法被多次调用,即在读取完整数据之前退出while循环。
如果我插入延迟
try {
while (input_data.available() > 0) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
input_data.read(rec_ack);
}
} catch (IOException e) {
System.out.println("IO Exception in SerialEvent");
}
如上图所示,它工作正常。请帮助我如何消除延迟,因为它会降低效率!