SetTimer()

             I am using MFC's to develop and application that reads
data from serial port every sec or 500ms Parse & update data into
dialog boxes.

This is what I have in mind, Let me know if this will work. I am really
keeping it as simple as possible. I don't really need a high accuracy
in Time..

CSerialIO::OnBnSerial()
{
......
......
//All Initializations here
SetTimer(IDC_Time,500,Null)
}

CSerialIO::OnTimer(UINT nIDEvent)
{
  ReadSerialport(); //All the data reading & Parsing is done in this
function
  UpdateData(False);
  CDialog::OnTimer(UINT nIDEVENT)
}

Once I press the Serial button I should have a timer started that reads
from the serial port every 500ms....Right??

Let me know
Raghu

0
12/29/2005 8:35:56 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
1008 Views

Similar Articles

[PageSpeed] 43

Never Mind my question, I already got it working

Thanks,
Raghu

0
12/29/2005 10:13:36 PM
"Raghu" <raghavendra.bulusu@gmail.com> wrote in message
news:1135888556.856949.236800@g49g2000cwa.googlegroups.com...
....
> CSerialIO::OnTimer(UINT nIDEvent)
> {
>   ReadSerialport(); //All the data reading & Parsing is done in this
> function
>   UpdateData(False);
>   CDialog::OnTimer(UINT nIDEVENT)
> }
>
It's good that you have it working.  The code you posted looks basically
correct.

However, you should not pass the timer event onto the base class if it's
your expected ID.  Sometimes the base class eats the timer (e.g., a list
view will eat the timer and then call KillTimer()).

Your code should process the timer if it's yours, and call the base class if
it's not:
CSerialIO::OnTimer(UINT nIDEvent)
{
  if ( nIDEvent == IDC_Time )
  {
    ReadSerialport(); //All the data reading & Parsing is done in this
function
    UpdateData(False);
  }
  else
  {
    CDialog::OnTimer(UINT nIDEVENT)
  }
}


0
1/1/2006 7:38:17 AM
What happens to the data which is being sent?  If you are reading the data every n ms, and
data comes in faster, then you will eventually lose data.  You are trying to implement
what is a classic imperative interface in an environment which should be thought of as
asynchronous event-driven.  

I'm curious what you think the UpdateData is going to do.  I abhor the presence of
UpdateData in any program; it usually creates programs that are hard to write, hard to
debug, and nearly impossible to maintain.

You should also check for the nIDEvent being IDC_Time (and you shouldn't use IDC_ as a
prefix, since it indicates the ID of a Control, not the ID of a Timer).

So while this will in fact read data every 500ms, the whole idea sounds a bit dangerous.
What happens if you fall behind the data?  Note that 500ms is the minimum time, but the
maximum time is unbounded.  So you could potentially go several seconds without reading
data.  What happens to the data which is being delivered?  What about data overrun (the
kernel buffer fills up and you lose data)? What happens if you don't have a complete
message and you have to wait for all of it to arrive, and it takes longer than 500ms?
You've probably blocked the GUI.

I do a lot with serial interfaces, and this pretty much inverts the whole idea of how they
should work.  Updates should be driven by the arrival of data, not by polling by timer.
However, note that data arrival may be processed immediately but update of the display may
be deferred.
				joe

On 29 Dec 2005 12:35:56 -0800, "Raghu" <raghavendra.bulusu@gmail.com> wrote:

>             I am using MFC's to develop and application that reads
>data from serial port every sec or 500ms Parse & update data into
>dialog boxes.
>
>This is what I have in mind, Let me know if this will work. I am really
>keeping it as simple as possible. I don't really need a high accuracy
>in Time..
>
>CSerialIO::OnBnSerial()
>{
>.....
>.....
>//All Initializations here
>SetTimer(IDC_Time,500,Null)
>}
>
>CSerialIO::OnTimer(UINT nIDEvent)
>{
>  ReadSerialport(); //All the data reading & Parsing is done in this
>function
>  UpdateData(False);
>  CDialog::OnTimer(UINT nIDEVENT)
>}
>
>Once I press the Serial button I should have a timer started that reads
>from the serial port every 500ms....Right??
>
>Let me know
>Raghu
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
1/2/2006 5:31:51 AM
Reply:

Similar Artilces:

SetTimer()
I am using MFC's to develop and application that reads data from serial port every sec or 500ms Parse & update data into dialog boxes. This is what I have in mind, Let me know if this will work. I am really keeping it as simple as possible. I don't really need a high accuracy in Time.. CSerialIO::OnBnSerial() { ...... ...... //All Initializations here SetTimer(IDC_Time,500,Null) } CSerialIO::OnTimer(UINT nIDEvent) { ReadSerialport(); //All the data reading & Parsing is done in this function UpdateData(False); CDialog::OnTimer(UINT nIDEVENT) } Once I press...

SetTimer -> OnTimer driving me crazy
Hi, I found numerous examples of SetTimer and OnTimer examples on the web and they all look similiar to what I have tried here, but my OnTimer function is never executed. I created a SDF Application and launched a dialog off of the menu. In the Dialog I call a SetTimer and then delcared a OnTimer function also a member of the same dialog object. I never get the TRACE statement in the OnTimer to fire and a button displays that the global_sent variable is never updated. Here's where I set the timer in the dialog's OnItit function: ---------------------------------------------...

problem with settimer
Hi i'm using Settimer in a service. I've called Set timer as UINT nid = ::SetTimer(NULL, 0,100,(TIMERPROC) MyTimerProc); where TimerProc is defined as void CALLBACK MyTimerProc( HWND hWnd, // handle of CWnd that called SetTimer UINT nMsg, // WM_TIMER UINT nIDEvent, // timer identification DWORD dwTime // system time ) { switch(nMsg) { case WM_TIMER: break; } } I dont know why but i dont get the WM_TIMER event in the callback function..i mean the function TimerProc is never called even after the time specified in SetTimer() func...

SetWaitableTimer/SetTimer call back is not getting called
I have created a worker thread using CreateThread() function. I created a Timer Event using CreateWaitableTimer. I set the timer event using SetWaitableTimer and I include a call back function to be called when the timer expired. However, I do not hit the call back function. Is there any special code I need to add for this? Here is the snippet from my code. void CreateSTEventsAndThreads() { DWORD IDThread; stThreadEvents[0] = CreateEvent(NULL, FALSE, FALSE, NULL); timerEvent = CreateWaitableTimer(NULL, FALSE, "WaitableTimer"); stThreadHandle = CreateThread(NULL, 0,...

Another SetTimer question
Why is it that the following settimer is not working? the call back function TimerProc_Wrapper is never called after 5 secs?Why? Thanks for any help rendered. I want to be able to interrupt the timedelay instead of using the sleep(5000). ------------------------------------------------------------------------------------------------------- Inside the testDoc.h class CTestDoc : public CDocument { //UINT MyThreadFunc(LPVOID lParam); static UINT_PTR m_TestTimer; static VOID CALLBACK CTestDoc::TestTimerProc( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dw...

pop up messages after every 60 seconds using SetTimer()
Laxman Hi, i am writing an dialog based application in which i should get the pop up messages after every 60 seconds showing the time ( using GetLocalTime() Function ). But i am not able to do it. I am finding difficulty in using UINT_PTR SetTimer( UINT_PTR nIDEvent, UINT nElapse, void (CALLBACK* lpfnTimer) (HWND, UINT, UINT_PTR, DWORD) ); Can you help me ................... BOOL Dlg::OnInitDialog() { ...... ...... SetTimer(1, 60*1000, NULL); } void Dlg::OnTimer(UINT nIDEvent) { SYSTEMTIME st; GetLocalTime(&st); ...

Why I couldn't SetTimer in ListView
I create new SDI project by AppWizard, at the final stage I modify the view class from CView to CListview. Everything is fine before I try to set a Timer in the ListView. I simply call CWnd::SetTimer to start a timer, then use classwizard to generate code to process WM_TIMER message Here comes the code: void CListView::OnTimer(UINT nIDEvent) { if (nIDEvent == 1) { .... } CListView::OnTimer(nIDEvent); } But the I found the message WM_TIMER is been delivered only once, then there are no more WM_TIMER after the first call to CListView::OnTimer. I am totally puzzled, could anyb...

To get the SetTimer identity
Hi, I have a dialog on which the timer is displayed using SetTimer() which is binded to the dialog.Is there any API that identify this timer which is got set ..... So that, with that id i can call some API which makes the timer to start from that time it was already showing at..... Whatever the number you want! As long as you didn't use the same number for another timer.. It can be the memory address of the object who handle it. Every object of a collection can have its own timer.. Sometime, for a single timer, I simply use "1234" as the ID.. jmarc... "lucky" ...

How to use SetTimer
Hi All, How to use SetTimer without a Window Class? Please help I have the following code, but the TimerProc() will never been called. class MyClass { public: MyClass() { ::SetTimer(NULL, NULL, 1000, (TIMERPROC) TimerProc); } ..... static void CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { // Do Something ......... } }; Rgds, Marco From the help: "When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to...

SetTimer with nIDEvent = 0
The documentation states: "nIDEvent [in] Specifies a nonzero timer identifier." However I have mistakenly set nIDEvent = 0 on many occasions with no apparent ill effects. I've found several code examples online where people have done the same. Does anyone know the story with this? Doug E. Codemonger wrote: > The documentation states: "nIDEvent [in] Specifies a nonzero timer identifier." > > However I have mistakenly set nIDEvent = 0 on many occasions with no > apparent ill effects. > > I've found several code examples online where people ha...

SetTimer() question
I'm using the timer function 'SetTimer()' and 'KillTimer()' function to start and stop the animation. The problem is, the animation pauses during the call to the Sleep() function or call to a function that takes a long time to complete processing. What can I do so that the animation does not pause. Thank you Put the long process in a seperate thread. Or Pump messages in between your long process. You can also change your sleep to do something like this void CMyApp::Wait(DWORD HowLong) { MSG Msg; DWORD Start = GetTickCount(); while (Start + HowLong > GetTickC...

settimer and realtime issue
hello all, I have application which diplays new messages every few minutes, and I noticed that on notebooks sometimes looks like timer stalls when computer is left idle for 2 or more hours. Is there some explenation of that? is this just Windows ? thanks ...

SetTimer() for my MainFrame
Hi guys, I added a clock to my status bar, so in my CMainFrame I want OnTimer() to keep it updated. Where should I start the timer with SetTimer() and kill it with KillTimer() ? Probably in OnCreate() and OnDestroy(), does that sound right ? Thanks. Guess What. You should probably call SetTimer in OnCreate anywhere after you have already created the Status bar. Call KillTimer in your OnClose Method. AliR. "Robert A." <satan@invalid.org> wrote in message news:%23SfFBLKYFHA.228@TK2MSFTNGP12.phx.gbl... > Hi guys, > > I added a clock to my status bar, so in my C...

problem with SetTimer()
Hi All, I've a problem with SetTimer function. I've wrote this code: class myClass { void CALLBACK TimerProc( HWND hWnd, // handle of CWnd that called SetTimer UINT nMsg, // WM_TIMER UINT nIDEvent // timer identification DWORD dwTime // system time ){ //some code.... } myClass() { SetTimer (1, 1000, (TIMERPROC) TimerProc); } } the compiler returns a conversion error for 3th parameter of SetTimer (TimerProc) help me, what's wrong ?? System info: - Visual Studio 7.0 - Windows XP SP2 Make TimerProc() static. class myClass { static...

using SetTimer
I use extensively the timers in my View mfc application but I find the documentation is not absolutely clear. I use the version with 3 paramteres - the last one is NULL which indicates using the View OnTimer routine. In the documentation it says to use KillTimer with the first parameter being used in the SetTimer function and in some other palce to use it with the return value of SetTimer. Can someone clarify that plaeas? Thanks Rick. "Rick" <rick@cortex.com> wrote in message -> I use extensively the timers in my View mfc application but I find the > documenta...

SetTimer problem
Hi, I have an application that requires processing at regular intervals. I have used SetTimer and cal back function to do the processing and it had been working fine (its a service and hence had to add message loop). Recently, in the processing I added one other function that uses a COM dll (a VFP dll to run VFP cmmands). With that, i see intermittent missing timer events (the processing was not done as expected and hence added a log. The log shows that the call back function is not called in between for some random intervals of time). I am not sure why this happens. What I am looking at now ...

To get the SetTimer identity #2
Hi, I have a dialog on which the timer is displayed using SetTimer() which is binded to the dialog.Is there any API that identify this timer which is got set ..... So that, with that id i can call some API which makes the timer to start from that time it was already showing at..... isnt it the same id u called SetTimer with ..... "lucky" <Laxmanmaruthy@gmail.com> wrote in message news:1175778268.053331.256800@o5g2000hsb.googlegroups.com... > Hi, > > I have a dialog on which the timer is displayed using SetTimer() which > is binded to the dialog.Is there any AP...

SetTimer (VC++ 6.0)
Hallo! In MSDN Library CWnd::SetTimer() help, we can read "Timers are a limited global resource". What mean exactly this ? How many system timers are available ? Thanks ! - Daniele - Hi Daniele, We can have as many as Timers as we want(Just define UNIQUE IDs). "Timers are a limited global resource" means that it is not affected by other processes going in the applications. >-----Original Message----- >Hallo! > >In MSDN Library CWnd::SetTimer() help, we can read "Timers are a limited >global resource". >What mean exactly this ? How many...

SetTimer / static pointer
Hi All, Two questions.. one about SetTimer and the other one about a static pointer.... I have a problem w/ the CWnd::SetTimer function. Maybe someone can give me an insight. This question regards a Windows CE [3.0] device: Let's say I have a timer running in my main dialog window started by "SetTimer(ID, .. etc)" .. then, I display modally a new window on top of the main one. In this one, I start a new timer by the same function "SetTimer(ID, ...") where the ID of this timer in this newly displayed window is different than the ID from the main window (even though th...

timer functions-SetTimer(help-urgently)
hello, I am reading a file in chunks of 512 bytes(i am reading 2 such blocks).I am calling SetTimer function.And in corresponding OnTimer function I am writing code for displaying the file data on a graph control placed in my dialog box.I am calling the KillTimer function after the 2 chunks of data are displayed as graphs. But when i close the dialog or press OK/Cancel button the dialog MFC application error box appears which asks "whether to send the error report to microsoft".Why is this coming?First i thought maybe the KillTimer function isnt properly called but th...

CWnd::SetTimer, limited global resource.
Prior to Visual C++ 2005, CWnd::SetTimer() included the following detail in it's documentation: "Timers are a limited global resource; therefore it is important that an application check the value returned by the SetTimer member function to verify that a timer is actually available." ...which had been around for many years. Did someone suddenly come to the realization that timers are no longer a limited global resource? Or, is this simply an oversight? -- Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote. http://www.peterRitchie.com/blog/ Microsoft MV...

SetTimer ?
In my VC6 MFC app, I establish a timer by using a call to SetTimer(...) for an elapsed time of 5 second intervals. All working fine in this manner, but... I need to reset this timer to start counting at zero if a certain unrelated event occurs. Can this be done in this rather primitive arrangement? I'm wondering if I could call KillTimer and immediately call SetTimer each time I need to reset this timer? Is there a better way? Essentially, I am recognizing when a mouse cursor enters a window and at that moment, I need to set the timer to trigger a message 5 seconds later. Any...

SetTimer IDs and return values
I have an app that uses a lot of timers. I'm using enums for the IDs so that I can spot them more easily. My question is, can the return value of SetTimer be different than the ID I set it too or is it only so when SetTimer fails? IOW, can I safely count on my enum IDs to be valid nIDEvents? Thank you all and happy holidays. >I have an app that uses a lot of timers. I'm using enums for the IDs >so that I can spot them more easily. My question is, can the return >value of SetTimer be different than the ID I set it too or is it only >so when SetTimer fails? IOW, can I safel...

SetTimer strange behavior
Found a weird problem today and didn't see it in the archives. I was doing a SetTimer based on an estimated time and happened to get a very big value (c7fffb50). Timer returned immediately which surprised me since that's on the order of 39 days. I got curious and did some testing. Even though nElapse is an UINT (which should handle ffffffff milliseconds) if I got much above 6fffffff it blew up. Below that it seemed to run fine (though I didn't wait around for 21 days just to check). Anybody seen this before? Any insight into what's going on down at the system level? Fr...

OnTimer(), SetTimer() & KillTimer()
I have a view window that I want to update automaticaly every couple of seconds. I'm trying to use the timer functions but I'm getting strange behaviour and MSDN seems slightly contradictary. can anyone help My view class constructor contain m_uiTimer = 0 ; // m_uiTimer is an unsigned int member variabl My OnInitialUpdate() contain m_uiTimer = SetTimer( 1, 5000, NULL ) I have an OnTimer event handler containin TRACE ( _T("%s[%d] Development Note - Timer event occured\n"), _T(__FILE__), __LINE__ ) RefreshList() ; // rebuilds the contents of the lis MyListView::OnT...