MFC serial port Programming MSComm control and SerialPort class

Source: Internet
Author: User

MFC production host computer, the first thing to understand is the serial port programming, there are generally two methods, one is to use ActiveX control, such as MSComm serial port control, there is a serialport class or some other serial port class, The difference between the two is that using the SerialPort class does not require registering the control and can be used on other computers that do not have the control installed.

One • Use MSComm serial control

Using the MSComm serial control method online A lot of, roughly speaking some methods and some places needing attention. If you're using VC6.0 to write on WIN7, there's a problem. The method for adding controls is usually selected for the Project à "project" à "add to project" à "components and Controls" à "Microsoft Communications control "This adds an error, prompting that this interface is not supported because the software is incompatible on the Win7, and the workaround is to right-click on the project interface to" Insert ActiveX Control "and then select the relevant control and then give the control a variable name using the Build Class Wizard, and the control-related classes are automatically added. And then you can use it. This control class is the same in this version of VS2008 and above, and it is different for controls with multiple classes.

Then right-click on the control to add the event, this is the event is received serial data processing function, the next initialization of the serial port can be.

Two · Using the SerialPort class

The SerialPort class is open source and easy to use, and the following is a tutorial for others who copy:

1 building a procedural framework project

1. In the VC6.0 development environment, create a new dialog-based MFC application named EXP and add the primary control to change the control ID.

2. Use the Class Wizard to add variables for the corresponding control. The main variables to be added are display types, such as edit boxes, combo boxes, and so on.

3. Add the class file. Download or copy the class files Serialport.h and serialport.cpp copy to the folder where the project is located, then click the VC6.0 menu Project->add to Project->files ..., Select Serialport.h and serialport.cpp Click OK to add the class file to the current project.

4. In FileView open ExpDlg.h, the header file description, that is, add # include "SerialPort.h", so that the Cserialport class is added. Complete the serial operation through the Cserialport class.

5. The Cserialport class object is defined in the main dialog header file ExpDlg.h, and if only one class object is manipulated, only one class object is defined.

 Public :          // Cserailport class object         BOOL  // flag serial port open

To operate multiple serial ports to define a class object for each serial port, this can be defined by an array, the class object defined here is M_serialport, and then a Boolean variable is used to flag whether the serial port is open, as above.

2 Add Message response (add 3 locations)

2.1 Adding a message response function declaration in the header file ExpDlg.h

In the Cserialport class, there are multiple serial port events can respond, in the general serial programming, only need to deal with Wm_comm_rxchar messages, all of the messages of this class need to manually add message processing functions. Define the processing function name as OnComm () and first add the serial character in ExpDlg.h to receive the response function declaration of the message Wm_comm_rxchar (a character in the serial receive buffer):

protected :         Hicon M_hicon;          // Generated message map functions          // {{afx_msg (Cexp2dlg)          ...... // just add this line          // }}afx_msg

2.2 Wm_comm_rxchar message mapping in the ExpDlg.cpp file:

Begin_message_map (Cexp2dlg, CDialog)          // {{Afx_msg_map (Cexp2dlg)          ...... // just add this line          // }}afx_msg_mapEnd_message_map ()

2.3 The message handler function that receives the data. Manual input: The implementation of the function OnComm () is then manually entered in the ExpDlg.cpp file (note: This does not add a button, but instead directly enter the entire function body manually!!!) )。

and complete the processing of the received characters in the docking, the received characters are displayed in the Receive edit box:

LONG cexp2dlg::oncomm (WPARAM ch, LPARAM port) {         + = ch;         UpdateData (FALSE);   // displays the received characters in the Receive edit box         return 0 ;}

3 Serial port Operation function

3.1 Open the serial port and close the serial port (must first open the serial port, in order to send data, otherwise the memory address write error!) )

Add a click response function for the button, add Onbuttonopen () to the "Open serial port" button, add Onbuttonclose () to the "Close serial port" button, and make the initial settings in Onbuttonopen ():

voidCexp2dlg::onbuttonopen () {//Todo:add your control notification handler code here         intnport=1;//Select serial COM1         if(Chuankoudx.initport ( This, Nport,9600,'N',8,1, Ev_rxflag | Ev_rxchar, +) ) {chuankoudx.startmonitoring (); m_bserialportopened=TRUE; }         Else{AfxMessageBox ("This serial port is not found or is occupied"); m_bserialportopened=FALSE; }}voidCexp2dlg::onbuttonclose () {//Todo:add your control notification handler code hereChuankoudx.closeport ();//Close the serial portM_bserialportopened=false;

3.2 Sending data

Add Onbuttonsend () for the Send button.

voidCexp2dlg::onbuttonsend () {//Todo:add your control notification handler code here         if(!m_bserialportopened)return;//Check if the serial port is open         Charbuf[ -]; memset (&buf,0,sizeof(BUF)); GetDlgItemText (Idc_send, buf,sizeof(BUF)); if(strcmp ("Not FOUND", buf) = =0)                   return;       Chuankoudx.writetoport (BUF); //Send Data}

MFC serial port Programming MSComm control and SerialPort class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.