Thread: Logic for Message Timeout

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    35

    Logic for Message Timeout

    I am struggling to write the logic for CAN message timeout. I am trying to avoid global variables, i have added the attachment, please advise me a good method. I also need to check the message timeout in other functions. Do i need to use global variable?
    Attached Images Attached Images Logic for Message Timeout-timeout1-jpg 

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    How to you think clearFlag will work unless it is global?

    1. It needs to be visible where needed
    2. It needs to retain the value

    Since, you are using it in a interrupt/timer function it likely needs to be global; but, maybe your CPU/MCU has a feature that allows some other storage class to work.

    Tim S.
    Last edited by stahta01; 12-24-2022 at 08:10 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I would do something like this.

    Create a separate source file just to handle this problem, then make the variables static.
    The variables have persistent storage, but their use is limited to the few functions in the same source file.

    Code:
    static int clearFlag = 0;
    static void *timeoutContext = 0;
    static void (*handleTimeout)(void*);
    
    void canRxInterrupt(void) {
      clearFlag = 0;
    }
    
    void timer1ms(void) {
      clearFlag++;
      if ( handleTimeout ) {
        handleTimeout(timeoutContext);
      }
    }
    
    void registerTimeoutFn( void (*fn)(void*), void *ctx) {
      timeoutContext = ctx;
      handleTimeout = fn;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCP TimeOut
    By vishal_askqns in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2009, 07:02 PM
  2. rsh timeout
    By jlai in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 07:18 AM
  3. NFS timeout problem
    By rotis23 in forum Linux Programming
    Replies: 3
    Last Post: 10-20-2003, 03:02 PM
  4. Function Timeout
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2003, 07:23 AM
  5. timeout for handshake
    By iain in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2001, 01:02 PM

Tags for this Thread