Recently, when sorting out files, I found a previously written serial port. Program To improve reusability, I wrote it again. According to a series of previously used serial operation software, most serial operation software does not have the automatic record storage function, so I will add it here, the record file is a time-named text file in the application directory. The current release of this program has some functions that are not implemented, but this does not affect the use. If needed, you can add Code . The following describes the program's core class csuperserialport.
This type of main functions include:
1. Control the serial port to send and receive data and use the window system message to forward the received data.
2. A csuperserialport object is supported to send data to multiple windows.
3. Supports sending large data blocks.
4. supports dynamic setting of the size of the sending and receiving cache area.
5. Support file transmission using external threads.
6. Supports sending and receiving data statistics.
7. custom error messages are supported.
This class also has two auxiliary struct win_arg and tagpackage.
Win_arg is the form description structure, which contains the form pointer, custom message, and location ID.
Typedef struct win_arg
{
Uint winid;
Cwnd * pwin;
Dword msg;
} Winarg, * pwinarg;
A tagpackage is a data packet description structure that contains the data length, data category, and data value. Csuperserialport uses this struct to split and send large data blocks.
Typedef struct tagpackage
{
Enum emmaxsize
{Emmax_size = 1024 };
Uint ilen;
Uint itype;
Byte pdata [emmax_size];
} Package, * ppackage;
During the development of the serial port program, the most troublesome problem is that you need to determine the size of the cache area in advance. If protocol-based transmission is normal during transmission, but if one byte is used for transmission, It is very troublesome to receive data. To solve this problem, I used the cbinque attribute in the serial port and enabled a receiving thread to improve the program performance, and send the collected data to the specified form object before processing. For example:
Ppackage ppack = new package;
DWORD rdlen = pcom-> m_comstate.cbinque;
Ppack-> ilen = rdlen;
Enable the file_flag_overlapped attribute when initializing the serial port, for example
Createfile (m_com.com,
Generic_read | generic_write,
0,
Null,
Open_existing,
File_flag_overlapped,
Null );
Of course, the mutex object and the critical section are added to the receiving thread for control processes and data statistics. If you directly use the csuperserialport class, the program has completed these tasks for you. For example:
M_pssp-> open (m_comname, m_ucomrate, m_ucomparity, m_ucomdatabits, m_fcomstopbits );
M_pssp-> startreceive (m_pssp)
For details, refer to the Code in testserialport2dlg. cpp.
To solve the problem that a serial instance displays data to multiple windows, I added a form vector table typedef vector <win_arg> vwinlist TO THE csuperserialport class; you can use the attach and detach functions to control the number of forms. To add other functions to the csuperserialport class, you can implement the following three methods:
1. directly modify the csuperserialport class code.
2. inherit from the csuperserialport class.
3. Call getcomhandle to return the serial port handle.
If you have friends who need code, mail to I _mlibin@163.com