Basically I have a program that has 6 sliders that send a value through a serial port, I’m using a timer to dynamically resend these values as they are changed by the user. The problem I am having is that the 6 functions called by my timer are behaving strangely, the first function call works as it should, and continually sends the signal, but the other 5 called afterwards only send the signal when the values are changed by the user. (I want the current value of each slider resent constantly so the device I am sending it to doesn’t drop out, and the code for them is the same, but the timer only works properly with the first one). Was wondering if this was a known issue with the timer or I’m doing something wrong.
My Timer is:
Loop_Timer = SetTimer(1,50,0);
…
void CSerialPortDlg::OnTimer(UINT nIDEvent)
{
Write_1(); //works as expected, continually writes from this function without user input
Write_2(); // these only work when they are changed, and stop sending afterwards..
Write_3();
Write_4();
Write_5();
Write_6();
CDialog::OnTimer(nIDEvent);
}
I’ve never done serial communication with MFC, but I don’t think that SetTimer is the way to go. Those timers are very unreliable. When you set it to 50 ms, what you are really saying is “tick no sooner than 50 ms”, but it can be 50, 55, 500, or whatever, depending on what’s the computer doing (and the system’s timer resolution). Your app will get WM_TIMER messages every time the indicated time has elapsed and there are no other messages in the message queue. So, if the user is moving the mouse, the message will be “late”.
Apart from that, I don’t know how long do those Write_X functions take, but you should not be doing lengthy things in a timer handler, or you’ll miss ticks (which might be what’s happening).
That said, if you really need continuous sending of the slider status (or whatever data you need to send) you should find a way to do it continuously, not based on a timer. I mean, find a way to send your data and when finished, notify your app, or sender thread, and start sending again. But I’m not really sure how it should be done.