我正在使用jssc
我制作的模拟器进行串行端口通信。问题是每当服务器从我的模拟器请求设备时,我都会遇到延迟,因为模拟器中的设备会在一段时间后回复,而不是在请求之后。为了回复请求数据包,我在串行事件侦听器中 使用了以下jssc
方法:writeBytes()
SerialPort.writeBytes(packet);
并且数据包小于 20 字节,而且我正在检查我的串行事件
if(event.isRXCHAR() && event.getEventValue() > 0){}
你们能帮我减少这种延迟,以便模拟器设备在请求后立即回复吗?这是一段代码-
public void serialEvent(SerialPortEvent event)
{
if(event.isRXCHAR() && event.getEventValue() > 0)
{
byte Server_PacketByte;
try {
Server_PacketByte = receiver_Port.readBytes(1)[0];
byte[] form_packet = PacketFormation(Server_PacketByte);// for adding bytes to make packet
if(form_packet == null)
{
return;
}
for(Device d : devices)
{
if(form_packet != null)
{
d.processPacket(form_packet);// in the list of devices I have all the information of device and also reply packet
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
在 processPacket() 内部
if (packet.equals(REQUEST))
{
receiver_Port.writeBytes(device.getReply());
}