Delay in serial communication using jssc

1.1k views Asked by At

I am using jssc for serial port communication with simulator which I made. 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:

SerialPort.writeBytes(packet);

and the packet is less than 20 bytes and also I am checking my serial event that is

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

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

There are 1 answers

10
Shotgun Ninja On BEST ANSWER

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).

timing diagram

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.