-1-
VC ++Application Design for Serial Communication
Abstract: This article introduces a Serial Communication Class Integrated with Win32 API functions and the basic idea of C ++ class.
Cserialport class. The following describes how to implement serial communication in VC ++ 6.0 through specific examples.
Key words: VC ++ serial communication cserialport
1.Introduction
In the development of the control field, there are many communication methods between PC and peripheral control devices, such as serial communication ports and parallel
Communication Port, a network port (which must be supported by the NIC), a USB communication port, or a self-made crisis interface card. Serial Communication is
When the data communication speed is very high, developers adopt the most common, simple, and common communication methods. And
Considering that both the current standard configurations of PC machines have two serial ports, serial port resources are used to develop PC machines and peripheral control
Communication between devices should be the most convenient and convenient [3].
There are many methods to implement serial communication in common development environments (such as VB, Vc, and Delphi ).
Type:
First, the easiest way is to use ActiveX controls provided by microsofe to achieve serial communication, that is
MSComm control. The programming method for implementing serial communication is relatively simple in principle and easy to implement, but the programming flexibility is slightly lower,
When the application is running, the file related to the communication control must be merged.
Secondly, it is also a good method to implement serial communication using Win32 API functions. Use API functions to implement serial ports
The communication programming method is powerful and flexible, but it is complicated in principle and requires programmers to work on the hardware of the serial port.
Have a deep understanding of the principles.
Finally, you can use the standard communication function of the VC Runtime Library to implement serial communication. Its programming principle is simple, but it makes
Poor usability and flexibility.
In short, the implementation methods of various serial communication have their own advantages and disadvantages in terms of usage conditions, flexibility, and complexity.
It is not suitable for different application scenarios. This article introduces a basic combination of Win32 API functions and C ++ classes.
The idea encapsulates a serial communication class cserialport class. Through an instance introduction, we find this custom string
The interface communication class makes the implementation of serial communication more flexible and convenient.
2.Implementation of Serial Communication
Although there are many methods to achieve serial communication, generally, the procedure of the serial communication program is divided into four steps [2]:
(1) Open the serial port: In this step, we can enable communication resources, set communication parameters, and set communication
Events, create read/write events, and wait for the serial port to receive interruptions.
(2) read serial port: In this step, when a character or interference arrives at the serial port, the EV _ rxchar message is sent.
Send. After receiving the message, the response function reads the serial port receiving buffer.
(3) Write serial port: This step is responsible for writing the characters to be sent to the sending serial port buffer.
(4) disable the serial port.
2.1 cserialport
Introduction to Communication
Http://www.paper.edu.cn
-2-
2.1.1 cserialport
Class Definition Declaration
Class cserialport
{
Public:
Void closeport ();
Cserialport ();
Virtual ~ Cserialport ();
Bool initport (cwnd * pportowner, uint portnr = 1, uint baud = 19200, char parity =
'N', uint databits = 8, uint stopbits = 1, DWORD dwcommevents =
Ev_rxchar, uint writebuffersize = 1024 );
Handle m_hcomm;
Bool startmonitoring ();
Bool restartmonitoring ();
Bool stopmonitoring ();
DWORD getwritebuffersize ();
DWORD getcommevents ();
DCB getdcb ();
Void writetoport (char * string );
Void writetoport (char * string, int N );
Void writetoport (lpctstr string );
Void writetoport (lpctstr string, int N );
Protected:
Void processerrormessage (char * errortext );
Static uint commthread (lpvoid pparam );
Static void receivechar (cserialport * port, COMSTAT );
Static void writechar (cserialport * port );
Cwinthread * m_thread;
Critical_section m_cscommunicationsync;
Bool m_bthreadalive;
Handle m_hwriteevent;
Handle m_hshutdownevent;
Handle m_heventarray [3];
Overlapped m_ov;
Commtimeouts m_commtimeouts;
DCB m_dcb;
Cwnd * m_powner;
Uint m_nportnr;
Char * m_szwritebuffer;
DWORD _ dwcommevents;
DWORD m_nwritebuffersize;
};
2.1.2 cserialport
Class Function Description
The data member of this class is initialized in the constructor cserialport. The initport function completes the initial steps of the serial port.
Start chemical engineering, including opening the serial port, setting the SPC parameters, setting the time of the communication event, and setting the Device Control of the serial port
Block. Because the serial port is a critical resource and does not allow multiple threads or users to access it at the same time, two
Http://www.paper.edu.cn
-3-
Windows API functions entercriticalsection and leavecriticalsection to achieve mutual access to the serial port.
A. Serial Port operation functions:
The startmonitoring function calls the Windows API function: afxbeginthread starts a SPC thread
Open the serial port.
The restartmonitoring function calls the member function of cwinthread: resumethread to restart the SPC thread,
Restart the serial port;
The stopmonitoring function calls the cwinthread member function: suspendthread stops the SPC thread and closes
Close the serial port.
B. Serial Port read/write functions:
The writechar function calls the Windows API function: writefile writes data to the serial port;
The receivechar function calls the Windows API function: readfile reads data from the serial port.
Writetoport function: In addition, the extended function uses different variable parameters to meet different requirements for writing serial ports.
The commthread function is the SPC thread that completes the message loop of the SPC. This function calls waitcommevent.
Function to wait for the event message and then process and distribute the message. The writechar function is called in message processing.
And receivechar functions. In addition, necessary exception handling is performed in this SPC thread function.
C. Other related functions
Other functions processerrormessage, getdcb and getwritebuffersize are used to complete some auxiliary tasks.
.
2.2 cserialport
Application of Serial Communication
The Declaration of the cserialport class and some important functions are introduced earlier. The following describes the VC ++ 6.0 environment
Use MFC to compile a dialog box-based serial communication test application [1]. Defines the class for implementing serial communication
Cserialport.
2.2.1Serial Port Initialization
In our example, we declare a serial port object in cserialport. h:
Cserialport m_serialport;
To implement the serial port, you must first set and initialize the serial port. After the serial port is enabled, the status of the serial port can be monitored.
Control. The serial port initialization function is as follows:
Initport (this, nport, band, checkbit, databits, stopbits, ev_rxflag | ev_rxchar, 512 );
Initialize the status of the serial port. Only one serial port can be initialized at a time. The parameter nport: port number, band: baud rate,
Checkbit: Check bit, databits: number of data, stopbits: Stop bit can be set through the interface, or in the initial
Define the serial port directly.
After the serial port is initialized, you need to start the serial port communication thread function commthread through startmonitoring,
You can immediately listen to the defined serial port.
2.2.2Definition of processing functions
Declare the Custom Handler in the definition of the cserialport. H class cserialport file:
Afx_msg void oncomm (wparam CH, lparam port );
Add the oncomm () function in cserialport. cpp (). The data processing code is implemented.
Begin_message_map and end_message_map mapped to the cserialport. cpp message in the file
Add a custom message ing macro on_message (wm_comm_rxchar, oncomm)
Http://www.paper.edu.cn
-4-
To trigger the function in bytes. In this way, oncomm can be triggered when a byte exists on the serial port.
() Function to process messages.
2.2.3Serial Port writing
After processing the received message, you need to write the feedback message to the serial port again to complete the serial port
Communication. Here we use m_serialport.writetoport (char * string, int N) to complete serial writing, which defines
Number of strings and characters written to the serial port. In the cserialport class, we add other writetoport
Function to meet the needs of other situations, you can select the appropriate serial function as needed during use,
You can also expand other functions as needed.
2.2.4Close serial port
After the serial communication is complete, use m_serialport.closeport () to close the serial port.
3.Test
Use the serial port debugging tool to debug the compiled serial port testing tool.
Serial Port 2 is connected. Set the parameters of the serial port debugging tool and the serial port testing tool as follows,
Data bit. The stop bit settings must be the same. The two port numbers are respectively set to Port 1 and Port 2. When two ports are at the same time
When it is enabled, the serial connection line on the PC will turn on the two serial ports. When the "serial port debugging tool" sends "this is a test
Program !" This information is received by the serial port test tool. The test result is shown in Figure 1: Serial Port debugging
Figure 2: Information accepted by the serial port test tool.
Figure 1 sending information through the Serial debugger
Figure 2 acceptance information of the serial port Test Tool
4.Conclusion
This article uses the basic concepts of Win32 API functions and C ++ classes to encapsulate serial port operations into a serial communication.
Class cseriaport, which uses the serial communication class to "shield" the serial device, increasing the transparency and
The reliability of Serial Data Transmission facilitates programming by programmers. In addition, you can configure cseriaport based on your needs.
Class.
Http://www.paper.edu.cn
References
[1] kruglinski. Visual C ++ technology insider [M]. Beijing: Electronics Industry Press, 1999.
[2] Li xiyong. Visual C ++ serial communication technology and engineering practices [M]. Beijing: People's post and telecommunications press.
[3] Wang Ying. Three Methods of Serial Communication Using vc6.0 [J]. VC Knowledge Base online help period, http://www.vckbase.com /,
2000 Stage 1: 18-20.
Application Design of serial communication based on VC ++
Wang Zhen, Chen Yongtai
School of Information Engineering, Wuhan University of Technology, Wuhan, PRC (430070)
Abstract
This paper introduces a new serial port class, which is named cserialport and is comprised based on
Win32 API and some C ++ classes. By introducing one example, we analyze the application of
Cserialport class in the serial communication based on VC ++.
Keywords: VC ++, serial communication, class cserialport