简体   繁体   English

延迟使用jssc进行串行通信

[英]Delay in serial communication using jssc

I am using jssc for serial port communication with simulator which I made. 我正在使用jssc与我制作的模拟器进行串行端口通信。 The thing is whenever server requests for a device from my simulator I encounter a delay as device in my simulator replies after some time, not exactly after the request. 问题是,每当服务器从我的模拟器请求设备时,我的模拟器中的设备都会在一段时间后(而不是完全在请求之后)答复时遇到延迟。 For replying to the request packet I am using jssc method writeBytes() inside the serial event listener which is: 为了回复请求数据包,我在串行事件侦听器中使用了jssc方法writeBytes()

SerialPort.writeBytes(packet);

and the packet is less than 20 bytes and also I am checking my serial event that is 并且数据包少于20个字节,而且我正在检查我的串行事件

if(event.isRXCHAR() && event.getEventValue() > 0){}

Can you guys help me out to reduce this delay so that simulator device replies just after the request? 你们可以帮我减少这种延迟,以便模拟器设备在请求后立即回复吗? Here is a piece of code- 这是一段代码-

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();
        }

    }
}

inside processPacket() 内部processPacket()

if (packet.equals(REQUEST))
         {
            receiver_Port.writeBytes(device.getReply());
         }

So, I think what's happening with your system is that the response from your simulator back to the server is taking too long, or the server requests are too close together to be useful. 因此,我认为您的系统正在发生的事情是,模拟器返回到服务器的响应时间太长,或者服务器的请求之间的距离太近而无用。 If your simulator's response to the server takes too long, then it may disregard or ignore your server's subsequent requests, and your server may handle this by either ignoring the response (since it's for a request it already gave up on) or worse, thinking the response to request #1 is the response to request #3 (which may have had different parameters, and would therefore be invalid). 如果模拟器对服务器的响应花费的时间太长,则它可能会忽略或忽略服务器的后续请求,并且服务器可以通过忽略响应(因为它已放弃的请求)来处理此问题,或者更糟,以为对请求1的响应是对请求3的响应(可能具有不同的参数,因此无效)。

时序图

The solution for this is to either have the server wait a longer amount of time for the response before trying another request, or to somehow reduce the amount of time the simulator needs to respond to the server's request. 解决方案是让服务器在尝试另一个请求之前等待更长的时间来响应,或者以某种方式减少模拟器响应服务器的请求所需的时间。

If you're just doing an "is device connected" or "get device info"-style request of the device, or one that doesn't require real-time responses, you could have your simulator do that on its own (via a request loop on a separate thread or something) and cache the response, and just hand it back when requested from the server. 如果您只是在对设备执行“是否已连接设备”或“获取设备信息”样式的请求,或者不需要实时响应的请求,则可以让模拟器自己进行操作(通过请求循环到一个单独的线程或其他东西上)并缓存响应,并在服务器请求时将其返回。 However, you'd have to make sure that it gets aborted when a real-time request comes through, so it's almost more complicated than necessary. 但是,您必须确保在实时请求通过时中止该请求,因此它几乎比必要的复杂。

EDIT : To clarify, I don't think that it's your serial communication that's experiencing an undue delay, because SERIAL COMMUNICATION IS SLOW . 编辑 :澄清一下,我不认为是您的串行通信遇到了不适当的延迟,因为串行通信很慢 I think you haven't considered that fact in your design, and you're expecting all communication for your potentially large number of devices to complete within a certain time frame. 我认为您尚未在设计中考虑到这一事实,并且您期望在一定时间内完成可能潜在的大量设备的所有通信。 In addition, each device may take a variable amount of time to deliver a response back over serial; 此外,每个设备可能花费可变的时间量才能通过串行方式传递响应; some of these may not even have flow control implemented properly, resulting in occasional delays or, in rare cases, delivery failures. 其中一些甚至可能没有正确实施流量控制,从而导致偶尔的延迟,或者在极少数情况下会导致交付失败。

You should have some other thread in your Simulator be requesting updates from devices periodically and storing them in a table. 您的模拟器中应该有其他线程,这些线程会定期请求设备更新并将其存储在表中。 That way, when a request comes in from the Server that asks about all devices, their information is already there, and can be packaged and delivered back to the server without the need for serial communication. 这样,当服务器发出一个询问所有设备的请求时,它们的信息已经存在,可以打包并传递回服务器,而无需进行串行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM