Re: Serial port monitoring

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



int characters=ComStat.cbInQue;
if (!characters) return;

if there is no character present it returns instantly so there is no hanging at all - our program has been in use for 10 years with happy users.

phil

"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message news:tm2ir3hnfmtjj2ud5t4h0s0e31nkmg4dgp@xxxxxxxxxx
Generally, get rid of the timer. There's generally no need for it if its purpose is just
to limit the amount of time spent waiting for the serial port. The problem with this code
is that it blocks if there is no character present, so if you have a large delay, the
thread that issues this call just hangs indefinitely. So this cannot be in the main GUI
thread. Generally, you would not want to use blocking I/O on a serial port.
joe

On Sun, 17 Feb 2008 23:58:17 -0000, "philip" <mfc@xxxxxxxxxxxxxxxxxxxxx> wrote:

In my program I have a timer every 100ms that just does something like

COMSTAT ComStat ;
int characters=ComStat.cbInQue;
if (!characters) return;

if there is something I then use
DWORD dwLength;
BOOL fReadStat = ReadFile(m_digitizerFile, buffer,characters,&dwLength,
NULL);

to actually get the data

Like Scott says if you poll too often (1ms) the program just hogs the whole
processor and customers complain! But at around 100ms intervals it works
pretty well - note: my implementation is not in a seperate thread

Phil



"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:e%23dX9zbcIHA.536@xxxxxxxxxxxxxxxxxxxxxxx
"Kahlua" <kahlua@xxxxxxxxxx> wrote in message
news:gV2uj.7748$wK4.308@xxxxxxxxxxx
In my earlier days of "Turbo C" when I wrote an aplication that uses the
serial port I did something like this.

1st I would initialize the comport and open it, of course (Com1)
I would then have a loop that checks for char in serial buffer.
I would read the char and depending on its value I would jump to other
parts of the program
Then I would return to the loop which just sits there looking for chars
in serial buffer.
As far as I know this is obviosly not desireable in VC++
So, my app has to be able to "watch for char in serial buffer" while also
able to react to buttons clicked on screen.
If a char enters the serail buffer I want to be able to retrieve it and
check its value.
Everything is done within my app.
Like a large loop running.


With a Windows program you can't poll the port because your program can be
suspended for hundreds of milliseconds, so your loop would miss
characters. The built in serial port device driver handles inputting and
queues the input to a buffer for you.

You also have to avoid a loop continuously running, because that steals
all the CPU time from other programs. MFC runs your message handlers when
you have an incoming message, but at all other times your main thread is
suspended. So the ideal way to handle serial input is to input it in
another thread (you will often get more than one character) and transfer
the received data to your main thread by posting a message to it. I don't
know if your COMDrv++ library does that for you. If not, you will have to
do that yourself.

There is a sample program and article in MSDN named MTTTY that shows how
to do it.

--
Scott McPhillips [VC++ MVP]
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

.



Relevant Pages

  • Re: Serial port monitoring
    ... There is no need for a timer; you want to get data when it is available, ... The main thing is that every 100ms or so you check the serial port and read ... I would then have a loop that checks for char in serial buffer. ...
    (microsoft.public.vc.mfc)
  • Re: Serial port monitoring
    ... There is no need for a timer; you want to get data when it is available, ... line in the serial port, then by the time of the next trigger the rest has ... I would then have a loop that checks for char in serial buffer. ...
    (microsoft.public.vc.mfc)
  • Re: Serial port monitoring
    ... There is no need for a timer; you want to get data when it is available, ... line in the serial port, then by the time of the next trigger the rest has ... I would then have a loop that checks for char in serial buffer. ...
    (microsoft.public.vc.mfc)
  • Re: Serial port monitoring
    ... I would then have a loop that checks for char in serial buffer. ... Then I would return to the loop which just sits there looking for chars in serial buffer. ... The built in serial port device driver handles inputting and queues the input to a buffer for you. ...
    (microsoft.public.vc.mfc)
  • Re: Serial port monitoring
    ... Use a separate thread to handle the serial port. ... I would then have a loop that checks for char in serial buffer. ...
    (microsoft.public.vc.mfc)