Click to See Complete Forum and Search --> : flushing the COM port
nicholso
April 20th, 1999, 02:27 PM
I've written an app which reads and write from COM1 and COM2. The input from COM1 is data from a camera telling me the location of a robot and a ball it is chasing. I read in that data from COM1 a few bytes at a time, process it, then send out data on COM2 to tell the robot where to move.
The problem is, when I go back after this cycle to read more data from the camera, the data has been buffered so instead of the most current data it is at least a second old. This is not acceptable when the ball and the robot are both moving in realtime.
If anyone knows how to force a flush of a COM port in vc++ I would be most appreciative.
anthony
Randy C
April 20th, 1999, 02:45 PM
I'm not sure if there is a single call "flush" available now that all I/O looks like file I/O. But even if you could, the trouble with a flush is that unless you manage it yourself, you could could be flushing data you want.You didn't say if you were dealing with overlapped I/O, but in any case it seems like the best thing to do is put the reading of data, along updating of all key variables the data relates to in a separate thread. You'll need to play with the timeouts of the read, and of course it gets more complicated if you're using overlapped I/O. But just make sure you manage access to key variables by putting them in a class with a CCriticalSection object, and make all data reads and writes go through member functions that make use of the Lock() and Unlock() members. You should then be able to just read the variables of interests (using member functions of the thread safe class), and know you're always reading the most recent information. This would better mimice the way a real time system works. You'll even be able to control the priority of the data gathering thread.
The real fun begins when you need to know within milliseconds just how old the data is!
--Randy C
* * * Second star to the Right!
Magnus
April 21st, 1999, 05:49 AM
I think FlushCom(?) is the answer to your problem
You could create a thread running at high priority to poll COM1,
process the data and send to COM2. This thread would be launched by the UI and
would have to be terminated by it. Use TransmitCommChar while writing to COM2.
I've met a similar problem.
I created separate thread to deal with COM. Both thread and process'
priority are set:
SetThreadPriority(THREAD_PRIORITY_TIME_CRITICAL);
and
SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
Since real thread priority is sum of its own priority and the process priority
this couse the highest possible priority for the thread which daels with COM.
It helps a lot but I reconed some delay in receiving data especially when system does some disk operations. I noticed that the thread despite its
priority is not notified immediately that there are any new data to read
in the buffer.
But I haven't solved the problem so far and I don't know if it is possible without dealing with drivers which doesn't seem to be an easy job.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.