Create Your own MFC Dialog Project
Insert a EditControl and a Button
Add files CommCtrl.cpp and CommCtrl.h to your project
If you have problems download the complete project
download source and EXE
The EXE is in the "Release" directory

we insert now SerialCtrl.cpp and SerialCtrl.h without a class (old C) the kind windows needs two WM_MESSAGES: WM_INITDIALOG function OnInitDialog (to open the COM port) WM_TIMER function OnTimer (to read every 50 msec the COM port)
void CRs232aDlg::OnTimer(UINT nIDEvent)
{
unsigned char buf[500];
int xx,index;
if(nIDEvent==1)
{
xx = ReadRs232Input(buf, COM1);
if(xx)
{
index=0;
while(xx--) m_strEditRead += buf[index++];
UpdateData(false);
}
}
CDialog::OnTimer(nIDEvent);
}
There is a periodically readout of the input buffer of RS232 with a TIMER-Function.
void CRs232aDlg::OnTimer(UINT nIDEvent)
{
unsigned char buf[500];
int xx,index;
if(nIDEvent==1)
{
xx = ReadRs232Input(buf, COM1);
if(xx)
{
index=0;
while(xx--) { m_strEditRead += buf[index];
RxBuffer[iRxIndex++] = buf[index];
if(buf[index] == LF) LineFeedDetected();
index++;
}
UpdateData(false);
}
}
CDialog::OnTimer(nIDEvent);
}
void CRs232aDlg::LineFeedDetected()
{
CString str;
RxBuffer[iRxIndex]=0; // end of string; now you can evaluate this string
iRxIndex=0;
str.Format("LineFeed%c%c",CR,LF);
m_strEditRead += str;
}
There is a periodically readout of the input buffer of RS232 with a TIMER-Function.
In the function "LineFeedDetected()" you can evaluate the content of RxBuffer
|
To control a Modem we need automatics steps.
One solution is a state machine.
This is an example with six states.
Send ATZ with CarriageReturn and LineFeed, the modem answers with ATZ CR LF followed with OK CR LF. Every time we receive a LF the input string is checked. |