Modem communication of MSComm control

Source: Internet
Author: User

The MSComm control transmits and receives data through the serial port (serial port), providing the application with serial communication capabilities. And in the popular visual programming today, can be very convenient in visual Basic (VB), Visual C + + (VC), Delphi and other languages and development platform applications.

MSComm is an ActiveX control provided by Microsoft to simplify the programming of serial ports in Windows, providing an interface for a range of standard communication commands that provide applications with a concise way to send and receive data through a serial port. There are two ways to process data, such as event-driven (event-driver), Query Method (Inquire).

1. Event-driven method

When you use event-driven design programs, the MSComm control will extract the OnComm event whenever a new character arrives, the port state changes, or an error occurs, and after the application captures the event, you can learn about the event or error that occurred by checking the CommEvent property of the MSComm control. thereby taking the appropriate action. The advantage of this method is that the program response is timely and the reliability is high.

2. Query method

This approach is appropriate for smaller applications. In this case, every time the application finishes a serial port operation, the CommEvent property of the MSComm control is constantly checked to check execution results or to check whether an event occurred. For example, when a program sends a command to a serial device, it may just be waiting for a specific response string to be received, rather than responding immediately to every word characters received.

Add in VC? The MSComm control is very simple. In the Open project, select Menu Project->add to Project->component and Controls, in the pop-up dialog "Component and Controls Gallery" selected Microsoft Communication Controls Version 6.0?, click the Insert button to complete the Add MSComm control action.

This project uses the event-driven method, the concrete realization is as follows:

A Serial Port Information Configuration

Complete the Add MSComm Control action in the dialog-based application and add the corresponding member variable M_comm as described above. You can set MSComm control properties by right-clicking the MSComm control on the dialog box template and selecting the Property menu item. In this modem communication program, set the "Control" property page handshaking is "2-comrts", or some of the domestic manufacturers modem can not normal communication. Others accept the default settings.

You can also set the properties of the control by modifying the OnInitDialog () function of the dialog box class. Refer to MSDN for detailed instructions on Comm control. if(m_comm.GetPortOpen())
   m_comm.SetPortOpen(FALSE); //设置串口配置信息前先要关闭串口

 m_comm.SetCommPort(1); //Com1串行口
 m_comm.SetSettings("9600,n,8,1"); //设置波特率为9600bps,无奇偶校验位,数据位8位,停止位1位
 m_comm.SetInputMode(1); //设置数据通讯格式为二进制数组格式
 m_comm.SetRThreshold(1); //设置为每次接到一个字节数据就触发OnComm事件
 m_comm.GetInput(); //清除串口输入缓冲区中残留数据
 m_SendData="ATZ\r\n"; //调制解调器初始化
two. Receive data

Add the Receive data function, double-click Comm Control in the dialog box, accept the default function, then the member function of the dialog class is Oncommmscomm () and add the following code:Sleep(500); //视各厂家modem而定。不加则数据接受不全
VARIANT m_input;
char *str,*str1;
int k,nEvent,i,len,m;
CString str2;
case 2: //收到大于RTHresshold个字符
   k=m_comm.GetInBufferCount(); //接收缓冲区的字符数目
    str=(char*)(unsigned char*)m_input.parray->pvData;
  str2=(const char*)str; //清除字符串中的不必要字符
  m_ReceiveData=(const char *)str;
case 3: //CTS线状态发生变化
case 4: //DSR线状态发生变化
case 5: //CD线状态发生变化
case 6: //Ring Indicator发生变化

The above is a little something written under the project. VC I am a beginner, there are mistakes in the place, I also ask you to criticize. Examples in the VC 6.0+win2000 debugging pass. Win98 the test data received is not complete. Also hope master pointing twos.

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: 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.