Visual c ++ MSComm serial control program development

Source: Internet
Author: User

In computer systems, there are two main types of data communication: one is serial communication, where the binary bit of each character is arranged by bit for transmission, which is slow, but the transmission distance is far away. Both the mouse port and USB port belong to the serial port. The other is parallel communication. The binary bit of each character uses multiple data lines for simultaneous transmission. The transmission speed is fast, but the transmission distance is relatively close. This method is generally used for internal computer transmission, and the print port is also a parallel port.

I. Serial Communication Principle

The serial port is the encoding converter between the CPU and the serial device. when data is sent from the serial port, the byte data is converted to the serial bit. During receiving, the serial bit is converted into byte data. In Windows, the serial port is a type of system resource. To use the serial port for communication, the application must apply for the serial port resource from the computer. after use, resources should be released.

Ii. Features of Serial Communication

1. Low communication cost, only one communication link

2. Slow transmission speed.

Iii. Serial Communication Transmission Mode

1. Work-order communication, fixed by sending data from the sender to the receiver station and television

2. half-duplex communication: two-way data transmission is allowed.

3. duplex communication, allowing both parties to send and receive data to and from each other at the same time

Iv. MSComm control Serial Communication Development

MSComm space is an ActiveX space provided by Microsoft to simplify serial program development. You can easily add this control in the Visual C ++ and vs2005 environments. After importing the cmscomm class, you can use the MSComm control in the program.

1. Common MSComm Methods

1) Get the serial port number by getcommp ORT ()

2) setcommp ORT (): Set the port number.

3) getrthreshold () and setrthreshold () are used to obtain and set the number of characters received by the receiving buffer. The MSComm control triggers the oncomm event.

4) getinputmode () and setinputmode () are used to obtain and set the data transmission type. The parameter is 1, binary transmission, parameter is 0, and text transmission.

5) setsettings () is used to set the baud rate, parity, data bit, and stop bit parameters.

6) getinput () is used to obtain data from the receiving buffer.

7) setoutput () is used to write data to the sending buffer.

8) getportopen ()/setportopen () to obtain the serial port status, enable or disable the serial port

9) getcommevent () obtains the events that occur on the serial port. 1 ---- send data; 2 --- receive data; 3----cts signal changes; prepare to send data; 4---dsr signal changes; prepare to accept data.

2. serial port configuration code

If (m_com.getportopen ())


M_com.setportopen (false );

M_com.setcommp ORT (COM); // set the port

If (! M_com.getportopen ())


M_com.setportopen (true); // open the serial port

Else


Afxmessagebox ("cannot open serial port ");

M_com.setsettings ("9600, N,"); // set the baud rate, check, data bit, stop bit

 

M_com.setinputmode (0); // retrieves data in text mode

M_com.setrthreshold (1); // The parameter indicates that an oncomm event that receives data is triggered whenever there are more than or equal characters in the serial port receiving buffer.

M_com.setinputlen (0); // set the Data Length in the current receiving area

2. The code for serial port to receive oncomm events is as follows:

Intresevent;

Resevent = m_mscom.getcommevent ();

Switch (resevent)

{

Case 2: // receive data

{

If (issended) // start receiving data

{

Variantdata;

Data = m_mscom.getinput ();

 

Cstringstr;

STR = data. bstrval;

Issended = false;

MessageBox (STR );

}

}

Break;

}

3. The serial data sending code is as follows:

Cstring STR;

M_data.getwindowtext (STR );

Issended = false; // sending completed

M_mscom.setoutput (colevariant) Str );

Issended = true; // sending completed

Sleep (1000); // 1 wonderful latency

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.