Part II.
Go to the Resource View, Select the Dialog folder and double click the IDD_READCOM_FORM.
It will open the main dialog box. Do the Following steps.
Step 5: Add a Button
1. Change the ID Property to BTN_CONNECT and Caption to Connect.
2. Add the Click Event for that Button. Simply double click the button. it will open the coding part.
3. Enter the following Code to OnBnClickedConnect() method
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(!ConfigureCOMPort(1))
return;
uResult = SetTimer(1,1000,NULL);
if ( uResult == 0 )
AfxMessageBox("Error: Could not connect the RS232 port!", MB_OK|MB_ICONEXCLAMATION);
Step 6: Add another Button
1. Change the ID Property to BTN_STOP and Caption to Stop.
2. Add the Click Event for that Button. Simply double click the button. it will open the coding part.
3. Enter the following Code to OnBnClickedStop() method.
// Stop the timer. Indirectly it will stop the reading
BOOL blStatus = KillTimer(uResult) ;
// Check Whether the timer is killed properly or not.
if ( blStatus == 0 )
AfxMessageBox("Error: Could not stop the watching of RS232 port!", MB_OK|MB_ICONEXCLAMATION);
// Reset the handle variable
CloseHandle( m_hFile ) ;
Step 7: Add the Label Box.
1. Change the ID Property to LBL_RECEIVEDDATA.
Step 8: Add a Edit Box.
1. Select the Editbox in toolbox and Paste the Dialog window.
2. Right click the editbox and go to the add variable option.
3. Enter the Variable name (in our case m_txtRead) and simply press ok button.
CEdit m_txtRead;
Part III.
The basic setup of a communication port takes place using a DCB, or device control block structure. Members of this structure specify the baud rate, parity, data and stop bits, handshaking, and other aspects of port behavior. The current settings can be obtained using the GetCommState function and can be set by calling the SetCommState function. A helper function, BuildCommDCB, can be used to fill in parts of this structure on the basis of a string formatted in the style of an MS-DOS MODE command.
Now you have to add some supporting methods.
Attachments
Source Files Sample Program