8 years, 10 months ago.

Serial communication value cannot retrieve via serial monitor

I’m new to c++ and serial communication. I followed the Microsoft article https://msdn.microsoft.com/en-us/library/ff802693.aspx. I want to pass the value via serial value PC to mbed. In order to do that I use following code. But I’m having exception. – Run-time check failure #3- The variable ‘dwRes’ is being used without being initialized. The intended program I pass a character value ‘u’ from PC to mbed. The mbed needs to read the value. Any help is appreciated.

dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE); not return unsigned value. i think problem is here.

PC code i used./ Visual studio 2013

BOOL WriteBuffer(char * lpBuf, DWORD dwToWrite)
{
   OVERLAPPED osWrite = {0};
   DWORD dwWritten;
   DWORD dwRes;
   BOOL fRes;

   // 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 FALSE;

   // Issue write.
   if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
      if (GetLastError() != ERROR_IO_PENDING) { 
         // WriteFile failed, but isn't delayed. Report error and abort.
         fRes = FALSE;
      }
      else
         // Write is pending.
         dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
         switch(dwRes)
         {
            // OVERLAPPED structure's event has been signaled. 
            case WAIT_OBJECT_0:
                 if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, FALSE))
                       fRes = FALSE;
                 else
                  // Write operation completed successfully.
                  fRes = TRUE;
                 break;

            default:
                 // An error has occurred in WaitForSingleObject.
                 // This usually indicates a problem with the
                // OVERLAPPED structure's event handle.
                 fRes = FALSE;
                 break;
         }
      }
   }
   else
      // WriteFile completed immediately.
      fRes = TRUE;

   CloseHandle(osWrite.hEvent);
   return fRes;
}

int _tmain(int argc, _TCHAR* argv[])
{
    if (WriteBuffer("u", 12345))
        printf("value passed");
    else
        printf("value not passed");

    system("pause");
    return 0;
}

mbed code

#include "mbed.h"
#include <string>
Serial pc(USBTX,USBRX); // tx rx
int main() {

       while(1)
       {
           char c=pc.getc();
           if(c=='u')
           {
               printf("charator read sucessfull");
           }
           else
           printf("charactor read fail");

        }

}
Be the first to answer this question.