0

Im trying to use the readfile function to read data from a CDC device in the WinCE environment.

BOOL WINAPI ReadFile(
  _In_         HANDLE hFile,
  _Out_        LPVOID lpBuffer,
  _In_         DWORD nNumberOfBytesToRead,
  _Out_opt_    LPDWORD lpNumberOfBytesRead,
  _Inout_opt_  LPOVERLAPPED lpOverlapped
);

I would like to know what is the size limit for the readbuf. I am sending the block of 256 bytes of data for 100 times, the parameter lpNumberOfBytesRead from the ReadFile returns 173 bytes on the first try, and 0 all other 99 times. Please let me know what I am missing. Also what is the size limit for the 'writebuf'? below is code for the port open

    BOOL PortOpen()
    {
    Close();

ComPort = INVALID_HANDLE_VALUE;
    COMMTIMEOUTS ct;




ComPort=CreateFile (TEXT("COM1:"), GENERIC_READ | GENERIC_WRITE,
                     0, NULL, OPEN_EXISTING, 0, NULL);



if (ComPort != INVALID_HANDLE_VALUE) {

    dcb.DCBlength = sizeof (dcb);
    GetCommState (hLocal, &dcb);
    dcb.BaudRate = 115200;
    dcb.fParity = FALSE;
    dcb.fNull = FALSE;
    dcb.StopBits = ONESTOPBIT;
    dcb.Parity = NOPARITY;
    dcb.ByteSize = 8;
    SetCommState (ComPort, &dcb);


    ct.ReadIntervalTimeout = 0;
    ct.ReadTotalTimeoutMultiplier = 0;
    ct.ReadTotalTimeoutConstant = 0;
    ct.WriteTotalTimeoutMultiplier = 0;
    ct.WriteTotalTimeoutConstant = 0;
    SetCommTimeouts (hLocal, &ct);

}

Liang

Christophe Vu-Brugier
  • 2,645
  • 26
  • 21
user3068597
  • 13
  • 1
  • 5

2 Answers2

0

Timeouts for the ReadFile operation can be configured using SetCommTimeouts API, are you sure that your code is not calling it? In that case the number of bytes returned can vary, depending on the different timeout parameters you set.

Valter Minute
  • 2,177
  • 1
  • 11
  • 13
  • I think the problem is with the write, I am writing 256 bytes, there should be four 64-byte packets, I am getting ack for the first two packets, the problem starts with third packet, I am keep getting nake for 30 times than I am getting the ack on the 31st trys. – user3068597 Mar 04 '14 at 02:40
0

Thank you for the reply! my problem has been resolved. WindowCE does not support the overlapped I/O, the read and write needed to be in the separated thread. Also, to avoid the race condition, I used semaphore.

Liang

user3068597
  • 13
  • 1
  • 5