Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating a timer

Status
Not open for further replies.

vitellozzo

Programmer
Oct 15, 2003
8
IT
Hello
I'm writing a program which read data from a serial port,the program uses timers and i'm new to their use.
Could someone suggest a beginner tutorial about this matter please?
Thanks in advance
Vitellozzo
 
thanks for answering
I'm using visual c++ 6 , under win2k
 
Is this a windows GUI program, or a console program?

In windows GUI mode, you would use WM_TIMER messages to tell you about elapsed time, at which point you would go look at the serial port to see if there was any data.

It kinda depends what else you're planning to do in your program whilst you're not looking at the serial port.



--
 
hi
It's a console program,there are some functions to read the serial port(someone else wrote them).
the next operations are insde a do while loop:
read the buffer, take a flash or check a timeout depending on buffer state (empty/full),verify the validity and the kind of message(plus send ACK/NACK),print the message or check a timeout if message_kind==D.
Theese are the main actions.
So my problem is how can i take a flash and how do i evaluate if time is elapsed in the time_out function?I mean Can i use the time.h functions, and also how is logically structured and used a timer.
thanks again
regards
 
Well this relies on your serial line module having a function which tests for a character, which you will need if your code isn't to get stuck in one place.

The pseudo-code might look like this
Code:
while ( 1 ) {
    // test to see if a char is present
    // if so, then read it and add it to the current message
    // if that completes the message, then handle it
    if ( serial_char_ready() ) {         /* at least 1 char available */
        ch = serial_read_char();         /* read it */
        add_char_to_message( msg, ch );  /* append it to the message */
        if ( complete_message( msg ) ) { /* is this now a complete message ? */
            decode_message( msg );       /* yes, figure out what it means */
        }
    }
    if ( do_flash_now ) {
        flash();
    }
    now = clock();
    if ( now - prev > timeout ) {
        do_timeout();
    }
}
Basically, the code just loops forever checking for possibilities and taking appropriate action when those events occur. The key point is to make sure that the code does not block at any point, otherwise it becomes impossible (in this simple scenario) to handle things like timeouts.

> Can i use the time.h functions
So long as your timeouts are measured in seconds, or perhaps 10's of milliseconds at a pinch, then yeah, I think this should work.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top