VC Serial Control MSComm control

Source: Internet
Author: User

MSComm controls

Visual C + + provides us with an easy-to-use ActiveX control , Microsoft Communications Control (MSCOMM), to support application access to the serial port, Inserting MSComm controls in your application makes it easier to send and receive data over a computer's serial port.

To use the ActiveX control MSComm, the programmer must add it to the project by:

(1) Click the Main Menu Project submenu add to Project's components and controls option;

(2) in the "Components and Controls Gallery" dialog box that pops up, select "Microsoft Communications Control in the registered ActiveX Controls folder, Version 6.0 "options, such as:


By clicking the Insert button, the MSComm control is added to the project. At the same time, the related files of class CMSComm mscomm.h and Mscomm.cpp are also included in project header files and source files. Of course, programmers can modify filenames themselves, such as:


By directly analyzing the mscomm.h header file, you can get a complete use of the control (mainly the public type of the interface function), we extracted the main code of the header file and commented on its key parts:

#if!defined (afx_mscomm_h__) #define Afx_mscomm_h__#if _msc_ver > #pragma once #endif//_msc_ver >//Machine generated IDispatch wrapper class (es) created by Microsoft Visual C + +
Note:do not modify the contents of this file. If This class is regenerated by//Microsoft Visual C + +, your modifications would be overwritten.
CMSComm wrapper class
Class Cmscomm:public CWnd {protected:declare_dyncreate (cmscomm) public:clsid const& getclsid () {static CLSI   D Const CLSID = {0x648a5600, 0x2c6e, 0x101b, {0x82, 0xb6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14}};  return CLSID; } Virtual BOOL Create (lpctstr lpszclassname, LPCTSTR lpszwindowname, DWORD dwstyle, const rect& RECT, cwnd* pParentWnd, UINT nID, ccreatecontext* pContext = NULL) {return CreateControl (Getclsid (), lpszWindowName, Dwstyle, rec T, pParentWnd, NID); }
BOOL Create (LPCTSTR lpszwindowname, DWORD dwstyle, const rect& RECT, cwnd* pParentWnd, UINT NID, cfile* ppersist = NULL, BOOL bstorage = FALSE, BSTR Bstrlickey = null) {return CreateControl (Getclsid (), lpszWindowName, Dwstyle, re CT, pParentWnd, NID, Ppersist, Bstorage, Bstrlickey); }
Attributes Public:
  Operations public:void setcdholding (BOOL bnewvalue);   BOOL getcdholding ();   void Setcommid (long nnewvalue);   Long Getcommid ();   void Setcommport (short nnewvalue);   Set the port number, such as Nnewvalue =1 represents COM1 short Getcommport ();   void Setctsholding (BOOL bnewvalue);   BOOL getctsholding ();   void Setdsrholding (BOOL bnewvalue);   BOOL getdsrholding ();   void Setdtrenable (BOOL bnewvalue);   BOOL getdtrenable ();   void sethandshaking (long nnewvalue);   Long gethandshaking ();   void Setinbuffersize (short nnewvalue);   Short Getinbuffersize ();   void Setinbuffercount (short nnewvalue);   Short Getinbuffercount ();   void Setbreak (BOOL bnewvalue);   BOOL Getbreak ();   void Setinputlen (short nnewvalue);   Short Getinputlen ();   void Setnulldiscard (BOOL bnewvalue);   BOOL Getnulldiscard ();   void Setoutbuffersize (short nnewvalue);   Short Getoutbuffersize ();   void Setoutbuffercount (short nnewvalue);   Short Getoutbuffercount ();   void Setparityreplace (LPCTSTR lpsznewvalue); CString GetparityrEplace ();   void Setportopen (BOOL bnewvalue);   Turn the serial port on or off, TRUE: On, FALSE: Turn off BOOL getportopen ();   The serial port is open, TRUE: On, FALSE: Close void Setrthreshold (short nnewvalue);   If set to 1, indicates that a character is received to send the number 2nd event short getrthreshold ();   void Setrtsenable (BOOL bnewvalue);   Hardware handshake enabled?   BOOL getrtsenable ();   void Setsettings (LPCTSTR lpsznewvalue);   Settings is composed of 4 parts, the format is: "Bbbb,p,d,s", that is, "baud rate, whether parity check, data bits//number, stop bit", such as set to: "9600,n,8,1" CString getsettings (); void Setsthreshold (short nnewvalue); //if the default value of 0 is not changed, then the process of sending data does not occur on the serial port event short getsthreshold ();   void Setoutput (const variant& newvalue);   A very important function for writing the serial port, note that the input parameters it receives are variant type objects,  //We need to convert the string to a Variant type Object   VARIANT GetOutput ();   void SetInput (const variant& newvalue);   VARIANT GetInput ();   A very important function for reading the serial port, note that it returns a Variant type Object, we need//convert it to a string void Setcommevent (short nnewvalue);   Short getcommevent ();   A very important function, to get the event that just happened on the serial port ("event" can be understood as the software sense of//"message" or the hardware sense of "interrupt"), the sending of the event will lead to the birth of OnComm message!   void Seteofenable (BOOL bnewvalue);   BOOL geteofenable ();   void Setinputmode (long nnewvalue);  Long Getinputmode (); };
//{{afx_insert_location}}//Microsoft Visual C + + would INSERT additional declarations immediately before the Previou S line. #endif


The analysis of the above source code shows that, basically, the mscomm of many interfaces can be divided into the following categories:

(1) Open and set the serial interface function;

(2) Obtain the serial port setting and the serial port State interface function;

(3) Set the serial port to send data mode, buffer interface and send data interface function;

(4) Set the serial port receiving data mode, buffer interface and receive data interface function;

(5) Set up and get the event interface function that occurs on the serial port.

VC Serial Control MSComm control

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.