I am maintaining a MFC program which can send data from computer A to computer B through RS232. Sometimes it transmits data smoothly, but sometimes it hangs forever. There are two threads sending the same data to a com port in sequence. The first thread sends data successfully, but the second thread hangs at the code "WriteFile". When the second thread on computer A hangs at "WriteFile", I send some meaningless data like "1" from computer B back to computer A. Then the hanging at "WriteFile" on computer A stop hanging, and computer B finally see the data sent by the second thread on computer A.
Here is the RS232 log from computer B .
The picture shows that two threads on computer A initiate there own test and send messages back to computer B. Each thread complete there own test and send TEST_DONE to computer B almost at the same time. But computer B only see the TEST_DONE sent by first thread on computer A(The second thread is hanging at WriteFile at this point.) until I manually send "1" to computer A from computer B.
Here is my code sending message from computer A to computer B. The length of cmd is 255.
BOOL SerialPort::AutoHandlerRES(unsigned char* cmd){
while(wait_transfer.IsLocked())
Sleep(1000);
wait_transfer.Lock();
CString out;
BOOL RetB;
UCHAR EndChar[2]={0x0D,0x0A};
out=CString(cmd);
DWORD num = out.GetLength()+2;
cmd[num-2]=EndChar[0];
cmd[num-1]=EndChar[1];
RetB=WriteFile(this->m_hCom, cmd, num, &num, NULL);
Sleep(1000);
wait_transfer.Unlock();
return RetB;}
My question is what could be the possible cause causing thread B hanging at "WriteFile"? Why the hanging does not happen on thread A? Thank you!
.Lock()is to wait if the lock isn't open ; you don't have to wait yourself. The second sleep is also suspicious. - ElderBug