How to detect a lost serial port connection

Posted on
  • I am working on a project where I am using my Espruino board to monitor Serial1. With regular intervals there are a couple of bytes arriving. The message contains status information, and I have to take action depending on the status bits. The action can be to turn on some LEDs that show status and fault information.
    I have figured out how to read the status bytes by converting them to decimal numbers, but I am struggling with how to detect if the communication suddenly stops. For example, the gadget that I am monitoring goes silent because there is a power interruption. How can you detect that there has been no communication for 3 seconds, for example?

  • Hi tage

    Use a variable where you count the received bytes. Use a timer (setTimer, setInterval) to check every N seconds the counter.

    Sacha

  • Or something like this

    var timer
    function callWhenBytesReceived() {
    if (timer) {clearTimeout(timer); timer=0;}
    timer=setTimeout("onDisconnect()",3000)
    }
    
  • Thanks,
    I tried Sacha's suggestion and it worked. I did try DrAzzy's method but did not manage to get it to work yet, will try again tomorrow if I find the time.

  • Personally I think Sacha's method is a little more efficient if you have a lot of data coming in - otherwise it won't matter...

  • Yeah, it probably is.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to detect a lost serial port connection

Posted by Avatar for tage @tage

Actions