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