From programming in visual c++ to connect through i/o serial port #1 for send and receive data with serial port , In this section ,We will learn controllering program communication with serial port by programming in vc++ 6 to create simple program using basic in the part 1 .
1. New project Microsoft VC++ 6
2. Go to Step1 of MFC AppWiard Dialog
3. Next to Finish Step of MFC AppWizard Dialog
4. Create GUI form and Edit,Setting,Name,Input Value in all control

5. Add Variable to Edit Box control for send and receive charecter

6. Defind all Variable for comunicate with comm port.

7. Map Massage BN_CLICKED to all Button Control.
You can by coming in at Form of a program is thatting build ,Next that time switch off click at Button control only ,That will have Input box pop-up show ,
Add Member Function of CLICKED , if do not change the name. So, click OK which will show a window of Source code to write give a program works when click Button that already ,which , have as follows.
// Button function for Connect with com port 1
void CVcserialDlg::OnButton1()
{
hComp = CreateFile( chCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hComp == INVALID_HANDLE_VALUE) {
m_text1=”Create file occur error“;
exit(1);
UpdateData(FALSE);
}
valReturn = GetCommState(hComp, &dcb);
if (!valReturn) {
m_text1=”Com Port verify Error “;
exit (2);
UpdateData(FALSE);
}
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
valReturn = SetCommState(hComp, &dcb);
if (!valReturn) {
m_text1=”Defind Com port Incorrect“;
exit (3);
UpdateData(FALSE);
}
}
// Button function for send character
void CVcserialDlg::OnButton2()
{
UpdateData(TRUE);
WriteFile(hComp,m_text2,8,&nWritePort, NULL);
}
// Button function for receive character .
void CVcserialDlg::OnButton3()
{
ReadFile(hComp,&chBuff,8,&nReadPort,NULL);
m_text1=chBuff;
UpdateData(FALSE);
}
//Button function for Close program
void CVcserialDlg::OnButton4()
{
CDialog::OnCancel();
}
——————— Finish ———————
Now , We can compile already,yes sir . If , not wrong ? It will be show a window of a program .Next time you try press “connect” button.
Experience by send and receive the alphabet , Might be open Hyper terminal program for check error of a program in send/receive the alphabet ,
Oh ! , this program will defind at Com port1 if you will be change Port number that use comminicate with Com2,Com3 .You must change variable name are “chCommPort” to be “COM2″ or “COM3″ follow to you want ,yes sir .