0

我正在将字符写入串行端口(没有硬件握手,没有重叠),经过几次向端口发送握手字符(ACK)后,writefile 返回 true,但发送的字节是 0 而不是 1,所以我必须 writefile再次。Getlasterror 总是返回 0。然后第二次就可以了。我假设有什么问题,但不知道它是什么。部分程序如下。

谢谢

swprintf( portName, sizeof( portName ) / sizeof(TCHAR), _T("\\\\.\\COM%i"), comPort); 
port = CreateFile(portName, GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
DCB dcb;            
ZeroMemory(&dcb, sizeof(dcb));
dcb.DCBlength = sizeof(DCB);
if ( GetCommState(port, &dcb) )
{
   dcb.BaudRate = CBR_9600;     
   dcb.ByteSize = 8;      
   dcb.Parity = NOPARITY;
   dcb.StopBits = ONESTOPBIT;
   dcb.fAbortOnError = TRUE;
   if (SetCommState(port, &dcb))
   {
    errlog(-1,"GetCommState failed with error %d.\n", GetLastError());  
    exit();
   }

while (1)
{
    DWORD bytes2write=1;
    DWORD bytes_written = 0;
    ClearCommError(port,NULL,NULL);
    BOOL w=WriteFile(port,chrs,bytes2write,&bytes_written,NULL);
    if (!w || bytes_written!=bytes2write)
    {
        DWORD err = GetLastError();
        if (err==0L)
            continue;   
        ...
    }
}

}

4

0 回答 0