Visual C development This article is 7022
If you need to reprint, please contact the author.
VC serial control programming, solving the increasing problem of memory
Category: VC
Author Name: chao_jie
Email address: chaojie2003@yahoo.com.Cn Author: Tianjin University of Technology
Development environment: VC6 Windows 2000
Use license: code is used
Explanation: Many people often encounter when using the VC serial control, when the serial port accepts the amount of data, from the Windows 2000 Task Manager, you can see that the memory occupied by the program will continue to increase. The API is written from the newly prepared, and this article is mainly solved.
It is written when writing a serial control to receive data:
// Initialization setting
M_mscomm.setcommport (1) // Open COM1M_MSCOMM.SETPORTOPEN (TRUE); // Open the serial port m_mscomm.setSettings ("115200, n, 8, 1"); // Serial port parameter setting m_mscomm.setInputMode (1); // CominputModebinary Set the Binary buffer input mode m_mscomm.sethreshold (5); // excitation oncomm () event m_mscomm.setInputlen (5) each receives; // Read 5 characters each time
//Receive data
Void ccomdlg :: oncomm () {Variant Variant_inp; ColesafeArray Safearray_INP; Long Len, K; BYTE RXDATA [5]; // Setting Byte array cstring strtemp;
Switch (m_mscomm.getcommEvent ()) {case 2: // The event value is 2 indicates that there is a character in the receiving buffer.
Variant_inp = m_mscomm.getinput (); // Read Cushion -------------------------------- *
SafeArray_inp = variant_inp; // variant type variable converted to a ColesafeArray type variable
Len = SafeArray_inp.getonedimsize (); // Verify that the valid data length assert (len == 5);
For (k = 0; k DEFAULT: BREAK;} //send Void ccomdlg :: Sendcommand (unsigned char command) {byte strbuf [5], tempbyte; cbeyteaRray Outbuf; Colevariant Varoutput // Command word strbuf [0] = commandID; // Address High Tempbyte = (Para1 >> 8) & 0xFF; strbuf [1] = Tempbyte; // Address low tempbyte = para1 & 0xff; strbuf [2] = tempbyte; // Data High Tempbyte = (Para2 >> 8) & 0xFF; strbuf [3] = Tempbyte; // Data Low Tempbyte = Para2 & 0xFF; strbuf [4] = Tempbyte; Outbuf.setsize (5); for (int i = 0; i <5; i ) Outbuf [i] = strbuf [i]; varoutput = Outbuf; m_mscomm.setput (varoutput); The problem is in the Variant type variable. When this line is executed, the Variant type variable will increase the memory, and can be solved by the following method: Void ccomdlg :: ONCOMM () {long len, k; coolvariant myvar; ColesafeAut Safearray_inp; Byte RxData [5]; // Setting Byte array Switch (m_mscomm.getcommEvent ()) {case 2: // The event value is 2 indicates that there is a character in the receiving buffer. Myvar.attach (m_mscomm.getinput ()); -------------------------------------- * SafeArray_inp = myvar; // Colevariant type variable converted to a ColesafeArray type variable Len = SafeArray_inp.getonedimsize (); // Verify that the valid data length assert (len == 5); For (k = 0; k Break;