문제

I want to send data from a laptop (windows 7, processor 2.60GHz) to a desktop (windows xp, processor 3.10GHz) using serial communication (using a USB to RS232 convertor). The WriteFile function is able to send the data from the laptop (NumberOfBytesWritten is correct). But on the desktop side, ClearCommError detects no data in the read buffer.

This is the relevant code in my desktop:

while(1)
{
    ClearCommError(hPort,&dwErrors,&commStatus);
    if (commStatus.cbInQue != 0)
        ReadFile(hPort,&data,1,&dwBytesRead,NULL);
}

The if condition is never satisfied. The baudrate and other parameters in the DCB struct are the same on both sides.

The same code works when I write and read in the same system by shorting the RX and TX pins in the RS232 connector.

도움이 되었습니까?

해결책 2

Sorry for the confusion guys, there was no problem with the code. My RX and TX pins were interchanged.

다른 팁

You might try reworking that loop:

while (1) 
{ 
    ClearCommError(hPort,&dwErrors,&commStatus); 
    if (commStatus.cbInQue != 0) break; 
    Sleep(10); 
} 
ReadFile(hPort,&data,1,&dwBytesRead,NULL);

The sleep will give a little time for the system to respond to more data - you may be spinning too fast.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top