The context is the fundamental of programming principle by MS Visual C++ 6 connecting through
4 steps for connecting
1. Set variable for keeping file names, data read (Buffer),file handles, value of getting and sending data.
HANDLE hComm; // handle for COM port
DCB dcb; // data structure that keep value for connecting serial port
char * chCommPort = “COM1″; // we use com port 1
int valReturn; // keep returning value back in some function.
char chBuffer; // keep alphabet in getting and sending data.
unsigned long nReadPort; //keep returning value back in function, ReadFile
unsigned long nWrittenPort; // keep returning value back in function, WriteFile
2. Use command CreateFile in referring new file.
hComm = CreateFile( chCommPort,
GENERIC_READ | GENERIC_WRITE,
// set as reading and writing file
0,
NULL, // not set security attributes
OPEN_EXISTING, // open communication
0, // not use verlapped I/O
NULL // we will use NULL for connecting communication
);
if (hComm == INVALID_HANDLE_VALUE) {
// verify error of program
m_text1=”creating the new file is error“; //show message on screen Edit Box
UpdateData(FALSE);
exit(1);
}
// verify status that will use Communicate
valReturn = GetCommState(hComm, &dcb);
if (!valReturn) {
//show message on screen in Edit Box
m_text1=”verify the status Com port is error “;
UpdateData(FALSE);
exit (2);
}
3. Setting value for connecting with
// in this part will be setting connection with Com port
// DCB is structure for controlling, connecting through serial port
// Constant in DCB of baudrate
// CBR_110 CBR_19200
// CBR_300 CBR_38400
// CBR_600 CBR_56000
// CBR_1200 CBR_57600
// CBR_2400 CBR_115200
// CBR_4800 CBR_128000
// CBR_9600 CBR_256000
// CBR_14400
// 9,600 bps we will use ,
// 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_9600; // set baud rate
dcb.ByteSize = 8; // size of number of data
dcb.Parity = NOPARITY; // set no parity bit //there are details of Constant like this,
// Constant Details
// EVENPARITY Even
// MARKPARITY Mark
// NOPARITY No parity
// ODDPARITY Odd
// SPACEPARITY Space
dcb.StopBits = ONESTOPBIT; // 1 stop bit , there are details of Constant
// Constant details
//ONESTOPBIT 1 stop bit
//ONE5STOPBITS 1.5 stop bits
//TWOSTOPBITS 2 stop bits
//Set value in Communcation
valReturn = SetCommState(hCom, &dcb);
if (!valReturn) {
// condition to verify error, show message on screen in Edit Box
m_text1=”setting of Com Port error“;
exit (3);
UpdateData(FALSE);
}
}
4. Use command ReadFile and WriteFile to read or write alphabet through serial port.
//the form of reading or getting data through com port
// in the part of &ch is variable that do as Buffer to keep alphabet //and number eight is the number of alphabets that get in each time.
ReadFile(hCom,&ch,8,&nReadPort,NULL);
m_text1=ch; //show variable value that can get data.
UpdateData(FALSE);
// the form of writing data in order to send through com port
//there will be taking alphabet in Edit Box that will send to keep in variable m_text2
UpdateData(TRUE);
WriteFile(hCom,m_text2,8,&nWrittenPort, NULL);
——————————————————————
|
All I mentioned, we write in File View–>Source File—> vcserialDlg.cpp After that, create Form GUI such as there is Edit Box to get and show alphabet button for Connect,Send,Receive, etc. In the section 2, we will create an example program of getting and sending alphabet through serial port. |
The learning outcome of this article
1) To know how to program by Microsoft Visual C++ to control working of
2) To know how to apply GUI to use in programming because of it’s easy and convenient in using.
3) To know programming to manage about File

