开发者

ReadFile only reading one buffer character?

I'm writing a program to communicate to a motor drive through a virtual com port via USB. I'm making use of the windows.h file-io to communicate with it (CreateFile, WriteFile, ReadFile). I can write to the drive perfectly fine, but I run into an issue when trying to read from it. For some reason, the ReadFile only seems to read in one character from the buffer. Here's my code:

#define READ_BUFF_SIZE 500
#define READ_TIMEOUT 5000
void ReadInput(HANDLE hComm)
{
    DWORD dwRead, dwRes;
    BOOL fWaitingOnRead = FALSE;
    OVERLAPPED osReader = {0};
    char lpBuf[READ_BUFF_SIZE] = {0};

    osReader.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
    if(osReader.hEvent==NULL){
        cerr<<"Error creating overlapped handle!";return;}

    if(!fWaitingOnRead){
        if(!ReadFile(hComm, lpBuf, READ_BUFF_SIZE, &dwRead, &osReader)){
            if(GetLastError()!=ERROR_IO_PENDING)
                {cout<<"Error: "<<GetLastError()<<endl;return;}
            else
                fWaitingOnRead = TRUE;
        }
        else {cout<<"a\n";PrintInput(lpBuf,dwRead);}
    }

    if(fWaitingOnRead){
        dwRes = WaitForSingleObject(osReader.hEvent,READ_TIMEOUT);
        cout<<"\n\nAfter WaitForSingleObject."<<endl;
        switch(dwRes)
        {
        case WAIT_OBJECT_0:
            if(!GetOverlappedResult(hComm,&osReader,&dwRead,TRUE))
                cout<<"Error in Comm"<<endl;
            else
                {cout<<"b\n\n";PrintInput(lpBuf,dwRead);}
            fWaitingOnRead = FALSE;
            break;

        case WAIT_TIMEOUT:
            cout<<"wait timeout"<<endl;
            break;

        default:
            cout<<"Default Case"<<endl;
            break;
        }

    }
}

dwRead always seems to be set to 1. I'll get the first character that the drive outputs (an apostrophe), but it won't read the rest. I've found some workarounds in that if I modify it to expect only one character and then loop through the reading, it will perform.

Additionally, I need the query the drive for a response, so before calling this ReadInput function, I have to cal开发者_Go百科l a WriteBuffer to send a character. If I put a delay (~10-50ms) between the read and write calls, it reads the whole thing.

Anyone have any ideas on this? It's not a huge issue since I've found some workarounds, but it has been bothering me for the past few days.

Edit: Clarification for tinman's questions.


You need to call SetCommTimeouts with appropriate settings. Here is some code that I use. You should look at the MSDN documentation for COMMTIMEOUTS structure.

// Only use read interval timeout. Do not use total read/write timeout.
COMMTIMEOUTS commtimeouts = { min((1000 + GetBaudrate() - 1) / GetBaudrate(), 1), 0, 0, 0, 0 };
SetCommTimeouts(m_hFile, &commtimeouts);

Of course you could also provide a total read timeout instead of an interval timeout.

From MSDN: When reading from a communications device, the behavior of ReadFile is determined by the current communication time-out as set and retrieved by using the SetCommTimeouts and GetCommTimeouts functions. Unpredictable results can occur if you fail to set the time-out values. For more information about communication time-outs, see COMMTIMEOUTS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜