views:

255

answers:

1

Hi.

I have been doing overlapped serial port communication in Delphi lately and there is one problem I'm not sure how to solve.

I communicate with a modem. I write a request frame (an AT command) to the modem's COM port and then wait for the modem to respond. The event mask of the port is set to EV_RXCHAR, so when I write a request, I call WaitCommEvent() and start waiting for data to appear in the input queue. When overlapped waiting for event finishes, I immediately start reading data from the queue and read all that the device sends at once:

1) write a request
2) call WaitCommEvent() and wait until waiting finishes
3) read all the data that the device sends (not only the data being in the input queue at that moment)
4) do something and then goto 1

Waiting for event finishes after first byte appears in the input queue. During my read operation, however, more bytes appear in the queue and each of them causes an internal event flag to be set. This means that when I read all the data from the queue and then call WaitCommEvent() for the second time, it will immediately return with EV_RXCHAR mask, even though there is no data to be read.

How should I handle reading and waiting for event to be sure that the event mask returned by WaitCommEvent() is always valid? Is it possible to reset the flags of the serial port so that when I read all data from the queue and call WaitCommEvent() after then, it will not return immediately with a mask that was valid before I read the data?

The only solution that comes to my mind is this:

1) write a request
2) call WaitCommEvent() and wait until waiting finishes
3) read all the data that the device sends (not only the data being in the input queue at that moment)
4) call WaitCommEvent() which should return true immediately at the same time resetting the event flag set internally
5) do something and goto 1

Is it a good idea or is it stupid? Of course I know that the modem almost always finishes its answers with CRLF characters so I could set the comm mask to EV_RXFLAG and wait for the #10 character to appear, but there are many other devices with which I communicate and they do not always send frame end characters.

Your help will be appreciated. Thanks in advance!

Mariusz.

+1  A: 

Your solution does sound workable. I just use a state machine to handle the transitions.

(psuedocode)

ioState := ioIdle;
while (ioState <> ioFinished) and (not aborted) do
Case ioState of
  ioIdle : if there is data to read then set state to ioMidFrame
  ioMidframe : if data to read then read, if end of frame set to ioEndFrame
  ioEndFrame : process the data and set to ioFinished
  ioFinished : // don't do anything, for doc purposes only.
end;
skamradt