Use ActiveX control to develop serial communication programs in VC ++

xiaoxiao2021-03-06  39

Using ActiveX control in VC , serial communication procedure Shanghai Jiaotong University B9703-1 Mailbox (200030) Huang Hairong Tian Zuhua

Summary: Discuss how to write serial communication programs when using the Microsoft Communications Control control when using Visual C , and gives routines, with certain practical meaning. Keywords: Visual C Serial Communication ActiveX In the process of developing microcomputer control systems, we often need to communicate with external devices via RS-232 serial interface. For example, the data exchange between the host computer and the lower computer and the communication of the computer and the digital instrument in the data acquisition system. In the DOS era, writing a serial communication program is a fairly complex job. The programmer needs considerable hardware knowledge, and the internal register definition, working mode, and instruction word, etc. related to programmable serial communication interface chips. It is likely to write a program, a lot of time and energy spend on how to deal with hardware, not spending our main purposes - gaining and processing data; Under Windows, Win32API provides CreateFile / Writefile Wait for a serial port operation, but it is still quite invisible in implementation. Fortunately, the advanced ActiveX technology of Windows platform allows us to process cumbersome details when programming for serial ports. With an existing ActiveX control, we only need to write a small amount of code, you can easily and efficiently complete the task. This article uses Visual C 6.0 under Windows 98 as an example to explore the method of serial communication using the Microsoft Communications Control control. 1 ActiveX Control Introduction ActiveX is a new technology for application development under Windows, which is the core content of the component object model COM (Component Object Model). ActiveX controls include a series of properties, methods, and events, using the ActiveX control application and ActiveX control work mode is the client / server mode, that is, the application to access the ActiveX control through an interface provided by the ActiveX control. Microsoft Communications Control (hereinafter referred to as MSCOMM) is an ActiveX control provided by Microsoft's simplified Windows serial communication programming, which provides an easy way for the application to send and receive data through serial interfaces. Specifically, it provides two methods of processing communication issues: First, an event-driven method, one is the query method. 1.1 Event Driven When using an event-driver design program, the MSCOMM control will be removed by the new character arrival, or when the port status changes, or an error occurs, the MSCOMM control will decimate the ONCOMM event, and after the application is captured, after checking MSCOMM The COMMEVENT attribute of the control can know the event or error to take the corresponding operation. The advantage of this method is that the program response is timely and reliability. 1.2 Query method This method is suitable for smaller applications. In this case, whenever the application executes a sided line operation, it will continue to check the COMMEVENT attribute of the MSCOMM control to check the execution result or check if an event occurs. For example, after the program sends a command to the serial device, it may just wait for a particular response string, not each character received, and processes. MSCOMM controls have many important properties, with a few of the first few shown in Table 1.

Table 1

Attributes

Description

CommPort Setting / Get Corresponding Serial Port Settings Settings / Get Bud Rate, Check Mode, Data Bit, Stop Bit Portopen Open / Off Communication Port Input Read Data OUTPUT Send Data 2 Programming Implementation In Using MSCOMM Controls Development PT650C Scale When the display communication program is used, the event driver is used, mainly to respond and get data in the buffer when ComeVReceive event occurs and gets the data. The implementation method is described below. Open the Visual C 6.0 integrated development environment, create a dialog-based MFC application project, name mycom, remember to select ActiveX Controls when setting the item option, and other default settings. After completing this step, select Menu Item Project / Add to Project / Components and Controls ... A dialog box will pop up to select Components (Components) and Controls in the system. Select the Microsoft Communications Control item under the Registered ActiveX Controls folder and press the INSERT button to join the MSCOMM control to this item. At this time, a C class named CMSCOMM will be generated, and the MSCOMM control icon will appear in the toolbar in the dialog editor. The CMSCOMM class is constructed by a series of interface functions exported by the MSCOMM control, using it will access the properties and methods of the MSCOMM control (Method). Suppose the PT650C weighing display is connected to the computer COM1 port, then open the resource editor, place a MSCOMM control above the program owner dialog (resource ID is IDD_MYCOM_DIALOG), and add the Class Wizard to the dialog box class to the member variable M_Wnd that should be controlled COM1. Since the PT650C scale weight display is serial communication with the computer serial communication, the baud rate is 2400/4800/9600, here I use 9600 baud rate, in The properties of the MSCOMM control are set in the dialog editor: ID: IDC_COM1 (Resource ID) CommPort: 1 (COM1) Settings: 9600, E, 7, 1 (Porter Rate 9600, Even Verification, 7 data bits, 1 Stop bit) Rthreshold: (a receiving data event is triggered) through each received data event). "Inputlen: 1 (not triggered the send buffer empty event) Inputlen: 1 (take a character from the buffer from the buffer) Other options Set according to the default setting or according to the requirements of the specific device. If you need to communicate with multiple devices through multiple serial ports, each serial port corresponds to a separate MSCOMM control. The setting parameters of the serial port can be set in the dialog editor, or in the program code to be set by calling the CMSCOMM class. For example, we can initialize the parameters of the MSCOMM control in the MyComDLG class, the code is as follows:

Bool CMYCOMDLG :: OnNitdialog () {cdialog: OnItDialog (); // The code is automatically generated for the MFC framework, not listing // Todo: add extra initialization her_wndcom1.setcommport (1); m_wndcom1.setsettings (" 9600, E, 7, 1 "); m_wndcom1.serthreshold (1); m_wndcom1.setsthreshold (0); m_wndcom1.setInputlen (1); m_wndcom1.setportopen (TRUE); // Open Communication Return True; // Return True Unless} next to the program owner dialog box to establish a process function responding to the MSCOMM event, each time the function will be called whenever the MSCOMM control triggers event. Use the left mouse button to double-click the MSCOMM control icon in the dialog editor. Enter the function name oncommcom1 in the pop-up dialog. The prototype definition of the event handler will be automatically added to the CMYCOMDLG class. Just give the specific data processing block in the oncommcom1 function, the code example is as follows: void cmycomdlg :: oncommcommcomm.com1 () {// Tod o: add your control notification handler code here cstring sinput; switch (m_wndcom1.getcommEvent ()) { Case 1: // comevsend event / * If there is data to be sent, the following code can be used: Variant Varout; Variant (& Varout); varout.vt = vt_bs; uses_conversion; varout.bstrval = sysallocstring ("my data) ); If (varOut.bstrval) {m_wndcom1.setOutput (varout); sysfreestring (varout.bstrval);} * / break; case 2: // comveVReceiv event, data arrived by SINPUT = m_Wndcom1.Getinput (). BStrVal; / / Double-received data is necessary to handle Break; Case 1009: // comeventRxparity event, parity error // Error handling code BREAK; DEFAULT: BREAK;} You must pay attention to it here is when sending character data, A string of Unicode format must be provided to the MSCOMM control. In the above code, the Uses_Conversion and T2ole macros are used to perform ansi string to the Unicode string, and the specific content can be referred to the MSDN documentation belt with Visual C 6.0. Anessat. This article explores the method of using MSCOMM serial communication ActiveX control in Windows 98, showing the powerful function of ActiveX technology, sufficient flexibility and ease of use, with certain practical significance. Reference 1 Microsoft Corporation. Microsoft Development Network. 2 Kate Gregory.Special Edition uses Visual C 5.

转载请注明原文地址:https://www.9cbs.com/read-51638.html

New Post(0)