I'm writing a DLL that is used with an existing application to interface some RS232 devices (up to 6) that must be fed data about every 50 ms. The devices cannot starve of data, if so they go into stop mode and a graceful recovery is impossible. In order to feed them data at the proper time the program must poll the devices about every 20-24 ms ms asking if they have space for additional data. I have this working in a limited fashion now talking to only one device with a worker thread. The worker thread monitors the buffer level in the RS232 device and sends data to the device if it has room available. However when other simple windows events occur, windows resize, etc, the thread scheduling changes and I am getting thread latencies which prevent the polling to occur on time, or else the thread is interrupted during the data send routines and the device runs out of data. I tried increasing the thread priority but I am limited to I believe two levels above main process threads priority and that is not sufficient. It helps but it is not enough to ensure reliable operation.
I decided to try using a Multimedia timer ( timeSetEvent) with a callback to run the RS232 data handling routine. Using DebugView I could see that the timer was working well and that windows events have little effect on the callback function execution due to its high priority. But when I tried this setup with my application (parent) program and I found out that my use of the Multimedia timers in my DLL had affected the operation of the parent (.exe) program.
It's timing is now way off, even though I was able to sucessully create the MM timer in the DLL. By doing some testing I was able to create a total of 6 more MM timers before the parent program developed a serious fault so this is not an issue of the number of MM timer limits.
So this is my dilemma. A worker thread gets pre-empted and the device runs out of data, a MM timer interferes with the .exe application.
My thoughts at this point are to find another timing mechanism that I can use to fire a callback?
Perhaps a sound system timer? or ?
Somehow hook the system timer interrupt which occurs about every 15 ms (plenty fast) or ?
I've looked for code to hook the system timer interrupt and haven't found anything yet. I have no problem putting asm code into this application if it gets me what I need.
I'm using VS2003 MFC.