Tag Archives: Serial communication

[Solved] Error reading comm device when writing serial communication with MSComm control

1. Problem description

In the process of serial communication written with MSComm control, when receiving the data sent by the serial port under the message response function OnComm function, in get_ An error reading comm develop error occurs in the input() function. The specific codes are as follows:

void XXXDlg::OnCommMscommLaser()
{
	memset(chstrLaser, 0, 1024);    //chstrLaseris a global variable.

	short i = m_mscomLaser.get_InBufferCount(); // statement 1.
	if(m_mscomLaser.get_CommEvent() == 2) // An event value of 2 means there are characters in the receive buffer.
	{
		/*m_recivedMsg = "";
		
		CString csstr = "";*/

		VARIANT InputData = m_mscomLaser.get_Input(); //Statement 2, read buffer.

		COleSafeArray csa = InputData; //VARIANT variable is converted to COleSafeArray variable.

		//translate to byte class.
		DWORD size = csa.GetOneDimSize();
		for(long k = 0; k < size; k++)
			csa.GetElement(&k, chstrLaser + k);
	}

    // Further processing of the received data ......

	return;
}

Each time the function is triggered, the data length of the receive buffer obtained by statement 1 is 30, which means that the receive buffer has data with a length of 30. However, the above error prompt will appear in statement 2, and the code after statement 2 will not be executed.

2.Method exploration

1. At first, I thought it was a data line problem. By changing the data line and making the data line myself, the problem could not be solved.

2. I suspected that there was a problem with the communication equipment. I also changed the equipment for debugging, but the same problem still occurred.

3. Suspected that the communication protocol was not clear.

4. Wait

3. Solution

After many serial port debugging assistants failed, I found that a serial port debugging assistant can communicate normally. I will provide the debugging assistant later. The current problem may be that the code is written incorrectly, and the data cable and equipment are OK.

Through a large number of searches, I found that one solution was to update the serial driver. After trying, I found that it was really possible. After a whole day, I finally solved this problem. I was very excited.

Here is the solution:

1. Download serial driver

My is a USB to 232 data cable, so I downloaded the prolific USB to serial comm port driver. You can download the corresponding driver according to your actual situation. Here I provide the driver that solves my problem: Click to download the serial port driver.

2. Update driver

After downloading the driver, start updating the driver:

a. Click Manage – > Click device manager -> Locate the serial port configured through the MSComm control

b. Right click the serial driver and click Update Driver Software – > Select browse computer for driver software

c. Click Browse to find the downloaded serial driver

d. Click next to solve the above problems after updating.