Abstract: The detailed elaboration uses ActiveX in VC 6.0 to control carries on the binary system data transmission through the serial port and the monolithic integrated circuit the concrete method.
Key word: VC ActiveX serial communication
In the monolithic integrated circuit application system, needs frequently carries on the correspondence through the RS-232 serial port and the microcomputer. At present in each kind of operating system, Microsoft Windows is more common, moreover mostly is Windows95/98 and so on 32 platforms. Formerly used the API function which in the Windows platform’s serial communication it provides to realize, this method uses needs many first floor establishments, thus is more tedious, and understood with difficulty. Microsoft promotes the ActiveX technology provided other one kind to realize the serial communication method. This method relatively is not only simpler, moreover is practical. Especially Visual in the C this kind of visualization object-oriented programming environment, may regards as the serial port an object, when the programming only need truly the simple establishment, understood comes to be very also easy. Below discusses serial communication ActiveX which Microsoft provides to control the application method in detail. Should control a corresponding document is MSCOMM32.OCX, following Jian Chengwei MSCOMM controls.
First, MSCOMM controls
MSCOMM controls, namely Microsoft Communication Control, is Microsoft is simplifies ActiveX which under Windows the serial communication programming provides to control. It has provided a series of standard correspondence order use connection, uses it to be possible to establish and the serial port connection, and may connect other communication facility through the serial port (for example modem), issues the order, exchanges event which and mistake in the data as well as the surveillance and the response serial connection occurs. MSCOMM controls to be possible to use in founding the telephone digit dialing procedure, the serial port signal procedure and the function complete terminal procedure.
MSCOMM controlled to provide two processing correspondence way:
(1) event driven way. When the correspondence event occurs, MSCOMM will control to trigger the OnComm event, the transfer may catch this event, through will inspect its CommEvent attribute then to be possible to confirm what will occur will be which kind of event or wrong, will thus carry on corresponding processing. This method’s merit is the response is prompt, the reliability is high.
(2) inquires the way. After procedure each essential function, may through inspect the CommEvent attribute value to inquire the event and the mistake. If the application procedure is small, this method possibly may take. For example, if writes a simple telephone digit dialing procedure, is not unnecessary to receive 1 character to have the event every time, because waited for only the receive the character is the modem ” determines ” the response.
When uses MSCOMM controls, 1 MSCOMM controls only to be able simultaneously to correspond 1 serial port. If the application procedure needs to visit and to control a serial port, then must use many MSCOMM to control.
In VC , MSCOMM controls only to correspond 1 C kind–CMSComm. Because MSCOMM controls itself not to provide the method, therefore the CMSComm kind besides Create () the member function, other functions is the Get/Set function is right, uses for to gain or the establishment controls a attribute. MSCOMM controls also only then 1 OnComm event, uses for to have the correspondence event to the transfer notice to occur.
MSCOMM controls to have many very important attributes, as space is limited only gives several more important and a commonly used attribute, like Table 1 arranges in order.
Table 1 MSCOMM controls a important attribute
| Attribute | Saying Bright |
| CommPort | Communication port number |
| Settings | By string of character form expression baudrate, parity check, data position |
| PortOpen | Communication port’s condition, opens perhaps the closure |
| Input | Receive data |
| Output | Transmission data |
| InputMode | Receive data type: 0 are the texts; 1 is a binary system |
Second, the programming realizes
From Table 1 may see that MSCOMM may two different form receive data, namely by the text form and by the binary mode. Controlled with MSCOMM to carry on the character data transmission the literature and the material may find many, (Microsoft Developer Network) might find such example in Microsoft MSDN, namely VCTERM. But possesses by the monolithic integrated circuit the primary data which obtains for the core measurement system is nearly the binary mode, therefore, by the binary mode transmission data will be most direct and the succinct means. Not only that because MSCOMM controls under the text form, what its transmission is the wide character format character, if wants to obtain the useful information, but also wants extra processing. Therefore this article main discussion under binary mode application method.
In VC 6.0, may produce three kind of application procedures with APPWizard: Single documents (SDI), multi-documents (MDI) and based on dialog box application procedure. In order to explain that the question and omits the nonessential detail, below take based on the dialog box application procedure as an example.
1. founds one based on the dialog box application procedure
Opens the VC 6.0 integrated development environment, selection menu item of File/New, in appears in the dialog box selects in Projects label’s MFC AppWizard(exe), then frame fills in MyCOMM in Project in the Name (to be possible according to need naming), afterward selects the OK button. In then appears in the dialog box selects Dialog the Based item, then selects the NEXT button. The following various dialog boxes defer to the default establishment, like this then produces one based on the dialog box application procedure. Will present its dialog box template in the resources programmer.
2. inserts MSCOMM to control
Selection menu item of Project/Add to project/Components and Controls…, in the dialog box which springs chooses under Registered ActiveX Controls folder’s Microsoft Communications Control, version6.0, then presses down the Insert button, will then spring a dialog box, will prompt the production a kind of name and the filename, will then realize according to the OK button controls a insertion. By now in the dialog box controlled in a toolbar to be many a telephone appearance to control an icon, in Workspace Classview also many kind of CMSComm.
This time then controls MSCOMM to join to the dialog box template, joins the method to control a dissimilarity with other. Then must join a member variable in the dialog box class, here we its naming is m_comm correspondingly. Joins the method is: First, in the dialog box template, should control with the mouse right key click, chooses ClassWizard, in appears dialog box Member Variables label’s Control under the Ids item, selects IDC_MSCOMM1. Then, according to Add Variable…The button, in dialog box Member Variable which appears in the Name item inputs m_comm. Finally, according to the OK button then.
3. establishment attribute
May to control a attribute in two places to carry on the establishment:
(1) in dialog box resources editor. On the dialog box template, single-clicks MSCOMM with the right key to control, then chooses Properties…The menu item, then may establish each attribute finally. Here only carries on the modification to following several, other accept the default establishment: Rthershold:1, InputLen:1, DTREnable: Does not elect, InputMode:1-Binary.
(2) in dialog box class OnInitDialog() function. Below is the function which above establishes realizes:
BOOL CMyCOMMDlg::OnlnitDialog ()
{
CDialog::OnlnitDialog ();
// here for the application frame automatic generating code, does not give lists
//TODO: Add extra initialization here
m_comm.SetCommPort(1); // use serial port 1
m_comm.SetSettings (”9600, N,8,1″);
// baudrate is 9600, non-parity check, 8 bit data positions, 1 stop position
m_comm.SetRThreshold(10); // receives 10 characters to trigger 1 receive event every time
m_comm.SetSThreshold(0); // does not trigger the transmission event
m_comm.SetInputLen(10); // each time reads the operation to take 10 characters from the buffer
m_comm.SetInputMode(1); // binary system data transmission form
m_comm.SetPortOpen (TRUE); // opens the serial port
return TRUE; //return TRUE unless you set the focus to a control
}
4. transmission binary system data
If needs to transmit the binary data, may do the data the following processing. The specific code is as follows:
CByteArray bytOutArr;
bytOutArr.Add(0×0); // to array assignment
bytOutArr.Add(0×1);
bytOutArr.Add(0×2);
bytOutArr.Add(0×3);
bytOutArr.Add(0×4);
COleVariant varOut;
varOut=COleVariant(bytOutArr); // data conversion for anomalous form data type
m_comm.SetOutput (varOut); // transmission data
5. receive binary system data
When needs to receive the massive data, should better select the event driven method to carry on the programming. The concrete step is as follows:
(1) responds the OnComm event. In the dialog box resources programmer, on double click dialog box template’s MSCOMM controls, in springs in the dialog box fills in the event which you hoped to respond the letter proper name, here its naming is OnCommMscomm1 ().
(2) in event response function receive and processing data. Receives the data is the anomalous form data, therefore needs to make some processing, the specific code is as follows:
void CMyCOMMDlg::OnCommMscomm1 ()
{
COleVariant varRcv;
CByteArray byt;
int i;
long num;
switch (m_comm.GetCommEvent())
{
cass 1://data transmission event
break;
case 2://data receive event
varRcv=m_comm.GetInput();
varRcv.ChangeType (VT_ARRAY |VT_UI1);
BYTE HUGEP *pbstr;
HRESULT hr;
hr=SafeArrayAccessData (varRcv.parray, (void HUGEP*FAR*) &pbstr); // gain security array indicator
if (FAILED (hr)) {
AfxMessageBox (”gain array indicator defeat!”);
break;}
num=0;
hr=SafeArrayGetUBound (varRcv.parray,1,&num); // gain array upper boundary
if (FAILED (hr)) {
AfxMessageBox (”gain array upper boundary defeat!”);
break;}
for (i=0; i
byt. Add (pbstr [i]);
SafeArrayUnaccessData (varRcv.parray);
// this time the data has preserved in binary array byt, may according to need to carry on related processing
break;
default:
break;
}
}
In the above code’s processing part may make an independent function, in here transfer then. After the above code’s processing, the data which receives has deposited in binary array byt, may according to own need to carry on related processing to it, like preservation and demonstration and so on.
Third, hardware interface
Between the monolithic integrated circuit and microcomputer’s hardware interface may use 1 piece of MAX232 or ICL232 then realizes with several electric capacities, many literature have discussed that here no longer states.
The above method after author application in reality, felt that is succinct, is convenient, has the very strong practical significance.