Question : Serial Port Communication C++

I'm currently writing a C++ program to write/read modem commands to a Serial Port using WriteFile/ReadFile. I can write the commands but I never receive any response. (see trace below).
My questions:
1. Do I need to run the ReadFile command in a separate thread to continously monitor feedback from the port or can I just run it in a loop after a WriteFile?
2. I think all I have to do to get a response from the modem after sending an AT command is finish it using a carriage return. Is there anything else I need to do?

Thanks!

COM port is opened
<20091117152900.080 SYS>
Baud rate 115200
<20091117152900.080 SYS>
RTS on
<20091117152900.080 SYS>
DTR on
<20091117152900.080 SYS>
Data bits=8, Stop bits=1, Parity=None
<20091117152900.080 SYS>
Set chars: Eof=0x00, Error=0x00, Break=0x00, Event=0x00, Xon=0x00, Xoff=0x00
<20091117152900.080 SYS>
Handflow: ControlHandShake=(DTR_CONTROL), FlowReplace=(TRANSMIT_TOGGLE, RTS_CONTROL), XonLimit=100, XoffLimit=100
<20091117152900.080 SYS>
In/out queue size 1600/1600
<20091117152900.080 SYS>
Set timeouts: ReadInterval=100, ReadTotalTimeoutMultiplier=0, ReadTotalTimeoutConstant=250, WriteTotalTimeoutMultiplier=100, WriteTotalTimeoutConstant=10000
<20091117152902.079 TX>
+++AT [len=5]
<20091117152902.079 TX>
AT [len=2]
<20091117152912.297 SYS>

Answer : Serial Port Communication C++

The normal sequence is to sent the AT command (don't forget to send the CR (0x0d) ... then do the Read.  There is a cool trick that most people don't know:  If your read request is for 100 bytes, there is no return until you get 100 bytes.  So, how do you know how much to request?

Use ClearCommError ( http://msdn.microsoft.com/en-us/library/aa363180(VS.85).aspx ) and check COMSTAT.cbInQueue to find out how many bytes there are to read.

Send the AT command.  Wait a half second, then check for incoming data, then read that data.


For background, see:
Serial Communications in Win32
http://msdn.microsoft.com/en-us/library/ms810467.aspx

however, I do not recommend using overlapped ID or even COMM events - that always just confused the issue for me.  With a modem , you send a command, then read the response.  Very straight forward.

-- Dan

Random Solutions  
 
programming4us programming4us