Implement MSComm control serial programming in single-document MFC Program

Source: Internet
Author: User
 In a single documentMFCProgram ImplementationMSCommControl serial Programming

 

I used to summarize how to use the MSComm control provided by Microsoft to perform read/write control programming on the serial port in the dialog box-based MFC program, or refer to Li xiyong's book, I wrote a program that receives and decodes GPRS data through a serial port. At that time, I thought the serial port programming was very simple, so I didn't learn more. As the project ended, I gave up.

A few months later, I suddenly met a project that needed serial programming, but the preliminary project was based on single-document MFC programming. Although I can implement it in the dialog box for specific programming, however, the data cannot be operated directly in the document class, and the serial port can only be operated when the dialog box is used. Looking for Li Xianyong's book, I spoke very simply. I just said, "If the class is not based on cformview, then I modified some data in mainfrm, but I still don't know how to operate it. After referring to some online materials, I sorted out the following process to implement the serial port read/write control programming using the MSComm control.

 

1. Add MSComm control resources for the program

 

In vc6, you can select Microsoft Communications Control by opening the "Project-> Add to projcet-> components and controls-> registered active controls" menu, the version6.0 command inserts the MSComm control into the current program.

In the same way, in vc7, click "Add/Remove items" in the toolbox. In the pop-up dialog box, check Microsoft Communications Control and version6.0 items and click OK.

After completing the preceding steps, you can drag the MSComm control in the toolbox to any dialog box to add resources between MSComm controllers. Suppose we name it idc_mscomm.

 

2. Create an MSComm instance object for the Control Resource

 

Because there is no initialization function in the document, I chose to create an instantiated object for idc_mscomm IN THE oncreate function of view. Specifically, add the following code to the oncreate function:

If (! M_comm.create (null, ws_visible | ws_child, crect (0, 0, 0), this, idc_mscomm ))

{

Afxmessagebox ("An error occurred while creating the serial port! ");

}

M_comm.setcommp ORT (m_ncom); // sets the comport.

M_comm.setinbuffersize (1024); // you can specify the input buffer size.

M_comm.setoutbuffersize (512); // you can specify the size of the output buffer.

If (! M_comm.getportopen ())

M_comm.setportopen (true); // If the serial port is not enabled, open the serial port

M_comm.setinputmode (1); // sets the input mode to binary.

M_comm.setsettings ("4800, N, 8, 1"); // set the serial port features, such as the baud rate.

M_comm.setrthreshold (1); // responds to an oncom event when one character is received

M_comm.setinputlen (0 );//

 

3. Add corresponding Event Handlers for control resources

 

When programming, I need to use the data received by the serial port in the document class. Therefore, in this step, right-click idc_mscomm to add an event handler and select the doc class in the class list, IDE automatically adds the corresponding binding code and oncomm function for you. You only need to add the specific implementation code to the function. The program is as follows:

Carcmeasureview * pview = (carcmeasureview *) (cmainframe *)

(Carcmeasureapp *) afxgetapp ()-> getmainwnd ()-> getactiveview (); // get the view pointer address

Variant m_input;

Char * STR;

Int K, nevent;

Cstring str2, m_revdata;

Nevent = pview-> m_comm.getcommevent (); // obtain the Event Type

Switch (nevent)

{

Case 2: // If characters are accepted

K = pview-> m_comm.getinbuffercount ();

If (k> 0)

{

M_input = pview-> m_comm.getinput ();

STR = (char *) m_input.parray-> pvdata;

If (* STR = 'H ')

{

· // Processing procedures

}

}

Break;

········

}

Of course, if you want to process the received data in the View class, it is simpler than the above code. You can simply select the View class in the above class list. Add the specific code to the oncomm function of the View class.

 

4. Compile peripheral programs

 

Serial programming must be a communication type, so you must write some code functions such as sending data and function code programs that change serial parameters. I will not expand it here.

Note: If you need to switch the serial port in the program, you must first close the opened serial port and then open another serial port. If the newly opened serial port has the same attribute as the previous one, when you open the new serial port, you do not need to reset the attributes.

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.