How to Read & Write the COM Port - A Step by Step Approach
CoderSource.net
How to Read & Write the COM Port - A Step by Step Approach - Article by bagavathikumar
Level: BeginnerType: Article
Rating: 3Page: 2 of 3

Date: 2/3/2006 12:00:00 AM

Environment: Windows, VC, VC.NET

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

1 2 3

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us