|
Use communication controls in VC ++ to develop Serial Communication Programs
Zeng Ming
Abstract:
This article describes how to use communication controls in Visual C ++ 5.0 and provides
A simple and Common Communication Example program written using this control.
Keywords: Serial Communication communication control
With the gradual popularization of Win 95, programmers are more and more willing to program in Win95, and Visual C ++ is becoming the main
Sending tool. However, developing a serial communication program with vc in Win95 is a real headache for programmers.
Not only need to understand many complex API functions, but also need to master multi-threaded programming. Fortunately, what we provide in Visual C ++
Communication Control helps us solve this problem. Through correct use of this control, we can easily compile the required
.
The following is a detailed example. The programming environment of this program is Win95 and Visual C ++ 5.0. This
Programming examples have strong functions. You can select the communication port for transmission and set the parameters of this port.
Including the baud rate, data bit, stop bit, parity and traffic control. It also has a pair of sending and receiving data
Function.
1. embedded communication control in the program
Start visual c ++ 5.0, use MFC Appwizard (exe) to create a project file, and name it commtest,
In step 1 of Appwizard, select a dialog box-based application type (dialog based). in step 2, set ActiveX
The "controls" check box is selected, indicating that the program supports ActiveX controls. All others receive the default settings, and the Appwizard will automatically
Generate an application that uses the dialog box as the main window. Next we will add communication controls to this program. In resource
View to open the dialog box (idd_commtest_dialog) and change it to the displayed dialog box. Note that the Dialog
The language attribute of the box is changed to Chinese (P. r.c ).
Figure 1 dialog box
Click the project menu, select Add to project from the menu, and then click components and controls.
In the displayed dialog box, click registered ActiveX controls, and then select Microsoft communic from the list box.
Ations control, version 5.0, and click OK. Then you will be asked if you want to add the cmscomm class, click OK, and return
You can see that a communication control (such as a phone) is added to the control toolbar, drag it to the dialog box, and note
A new class is added to the project. By viewing the MSComm-class source file MSComm. cpp, we can understand this
The properties and usage of the control. The get... Function can be used to access the current value of this attribute, and set... The function is used
Set the new value of this attribute.
2. Set the communication control property value
In the "Activate communication control properties" dialog box, some important properties and their descriptions are shown in table 1:
Genus
Set Value
Description
Commp ORT
2
Serial Port Number. Generally, Serial Port 1 is used by the mouse, so Serial Port 2 is used.
Inbuffersize
1024
Receiving buffer size
Inputlen
0
Number of bytes read from the receiving buffer, 0 indicates all read
Inputmode
1
Type of received data. 0 indicates the text type, and 1 indicates the binary type.
Outbuffersize
1024
Sending buffer size
Rthreshold
1
Set the oncomm event to be triggered when several characters are received. 0 indicates that no event is generated. 1 indicates that one event is generated every time one character is received.
Events
Sthreshold
0
Set the minimum number of characters allowed in the sending buffer before the oncomm event is triggered. 0 indicates that no data is generated during sending.
Event. 1 indicates that an oncomm event is generated when the sending buffer is empty.
Settings
9600, N, 8, 1
Serial Port parameter settings, in turn for the baud rate, parity (N-no verification, e-even verification, o-odd verification), data digits, stop
Ending digits
Table 1 Serial Port attributes and descriptions
The attribute setting value in the preceding table is the value used in this routine and can be set flexibly as needed. Other attributes can be used
Default Value. For more information, see the online help file.
Communication controls work in a similar way and interrupt mode. When a communication event occurs (such as sending data or receiving data ),
The oncomm event is triggered, and the getcommevent () function is called in the event processing function, and can be determined by returning the value.
This is the type of event, and then the corresponding processing.
3. Main Program Compilation
First, add the corresponding variables and response functions for the controls in the dialog box. Table 2 shows the specific practices:
Control name
Control ID
Corresponding variable or function
Send data edit box
Idc_senddata
M_senddata
Receive data edit box
Idc_receivedata
M _ receivedata
Send button
Idc_send
Onsend ()
Clear button
Idc_clear
Onclear ()
Communication Control
Idc_mscomm
M_comm
Table 2 add controls
Open classwizard and select idc_mscomm. The message that the control can respond to is oncomm. Add and modify the function.
Rename it oncomm () and add code to the function to process serial events. Note that
The problem is that the getinput () function returns a variant variable, while the cstring variable is displayed in the editing box.
Conversion is required. Convert a variant variable to a colesafearray variable, and then convert it to a byte array.
And then convert the array to a cstring variable. This conversion process looks complicated, but it can meet different requirements.
Variable type to display the received data. The main code of this program is added to commtestdlg. cpp, as shown below:
Void ccommtestdlg: onsend ()
{
If (! M_comm.getportopen ())
M_comm.setportopen (true); // open the serial port
Updatedata (true );
M_comm.setoutput (colevariant (m_senddata); // send data
}
Void ccommtestdlg: onclear ()
{
M_receivedata.empty (); // clear the data in the receiving dialog box.
M_senddata.empty (); // clear the data in the send dialog box.
Updatedata (false );
}
Void ccommtestdlg: oncomm ()
{
Variant m_input1;
Colesafearray m_input2;
Long length, I;
Byte data [1024];
Cstring STR;
If (m_comm.getcommevent () = 2) // The receiving buffer contains characters
{
M_input1 = m_comm.getinput (); // read data in the buffer
M_input2 = m_input1; // convert a variant variable to a colesafearray variable.
Length = m_input2.getonedimsize (); // determine the Data Length
For (I = 0; I <length; I ++)
M_input2.getelement (& I, Data + I); // converts data to a byte array.
For (I = 0; I <length; I ++) // converts an array to a cstring variable.
{
Char A = * (char *) (Data + I );
Str. Format ("% C", );
M_receivedata + = STR;
}
}
Updatedata (false); // updates the content of the edit box.
}
Iv. Test Procedure
Now that the program has been written, you can compile and run it. We can do a small experiment to verify the functions of the program. First test you
Serial Port 2 to check whether it works normally. Common dos program comdebug check is available. After confirming that the serial port works properly
The serial line connects the serial port 2 of the two computers and runs the program on the two computers at the same time (you can also use only one microcomputer to separate the Serial Line
2 and 3 are transient, that is, the spontaneous receiving status). In the send dialog box, enter some characters and click the mouse to send them.
Button, you will see the data in the receiving dialog box of another machine, and even Chinese characters can be sent. As you can see,
The communication control can easily compile a serial communication program. However, communication controls are used more in VC than in V
B is much more complicated. To develop more and more flexible use methods, we need to constantly explore and practice. |