views:

1259

answers:

3

I am getting an error when I try to run a c file which does some basic writes to a serial port. I am trying to run it asynchronously because the writes sometimes take a long time to transfer. My original version had it running synchronously with WriteFile() commands which worked fine. I am new to using OVERLAPPED and would appreciate and input concerning it.

The error I am getting is:

Debug Assertion Failed! Line: 1317 Expression: _CrtIsValidHeapPointer(pUserData)

when the second write function is called.

in main:

    {
        //initialized port (with overlapped), DBC, and timeouts

        result = write_port(outPortHandle, 128);
        result = write_port(outPortHandle, 131);
    }




static void CALLBACK write_compl(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped) {
        //write completed. check for errors? if so throw an exception maybe?
        printf("write completed--and made it to callback function\n");
    }


int write_port(HANDLE hComm,BYTE* lpBuf) {

   OVERLAPPED osWrite = {0};

   // Create this write operation's OVERLAPPED structure's hEvent.
   osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
   if (osWrite.hEvent == NULL)
      // error creating overlapped event handle
      return 0;

   // Issue write.
   if (!WriteFileEx(hComm, &lpBuf, 1, &osWrite, &write_compl )) {
      if (GetLastError() != ERROR_IO_PENDING) { 
         // WriteFile failed, but isn't delayed. Report error and abort.
       printf("last error: %ld",GetLastError());
       return 0; //failed, return false;
      }
      else {
         // Write is pending.
         WaitForSingleObjectEx(osWrite.hEvent, 50, TRUE);   //50 ms timeout

     return -1; //pending
      }
   }
   else {
     return 1; //finished
   }
}
A: 
    result = write_port(outPortHandle, 128);
    result = write_port(outPortHandle, 131);

The lpBuf argument have to be pointers to buffers, not constants.

e.g.

char buffer;
buffer = 128;
result = write_port(outPortHandle, &buffer);
buffer = 131;
result = write_port(outPortHandle, &buffer);

What you really want to do is also pass a buffer length.

e.g.

    char buffer[]  = { 128, 131 };
    result = write_port(outPortHandle, &buffer, sizeof(buffer));

int write_port(HANDLE hComm,BYTE* lpBuf, size_t length) {

   ...

   // Issue write.
   if (!WriteFileEx(hComm, &lpBuf, length, &osWrite, &write_compl )) {
   ...
Shane Powell
Added a 'be' on that first sentence and a comma to clarify - but yeah, 128 is never a valid pointer.
Paul Betts
I'm using single bytes in each write, so the size is always 1.
+2  A: 
Cwan
Could you possibly go into detail about how to use the offset in the overlapped structure to access a single BYTE which would be allocated as the buffer result of a ReadFileEx() command, so I can handle the BYTE from the completion function? I'm not too familiar with using offsets to access other data structures.And also would there be any potential problems with having an INFINITE timeout on the WaitForSingleObjectEx()?
See edit above. Hope it helps.
Cwan
A: 

That was not the full code, sorry. I was using an array of BYTEs as well, not constants. But system("pause")'s were causing my debug assertion failed errors, and after carefully looking through my code, when the WriteFileEx() was successful, it was never setting an alert/timeout on the event in the overlapped structure, so the callback function would never get called. I fixed these problems though.

I just need help with the handling/accessing a single BYTE in a structure which is allocated when a ReadFileEx() function is called (for storing the BYTE that is read so it can be handled). I need to know how to access that BYTE storage using an offset and make the overlapped structure null. Would making the overlapped structure null be as simple as setting the handle in it to INVALID_HANDLE_VALUE?