title: How to get the output with console app and display it to MFC Dialog StaticText
body: <p>I am working on a MFC app that need to get the same output with the console app. The console app can display the input data from serial port. I want to get the input with the MFC dialog app and display it into edit control box. Since I am still new to C++, it seems quite challenging to me. Any suggestions for the workaround? Thanks in advance!</p>
  
  <p>The console app code: </p>
  
  <pre><code>enum { EOF_Char = 27 };
  
  int ShowError (LONG lError, LPCTSTR lptszMessage)
  {
      // Generate a message text
      TCHAR tszMessage[256];
      wsprintf(tszMessage,_T("%s\n(error code %d)"), lptszMessage, lError);
      return 1;
  }
  
  int __cdecl _tmain (int /*argc*/, char** /*argv*/)
  {
      CSerial serial;
      LONG    lLastError = ERROR_SUCCESS;
      lLastError = serial.Open(_T("COM1"), 0, 0, false);
      lLastError = serial.Setup(CSerial::EBaud9600, CSerial::EData8,CSerial::EParNone, CSerial::EStop1);
      lLastError = serial.SetupHandshaking(CSerial::EHandshakeHardware);
  
      // ** The query command to get input data **
      lLastError = serial.Write(":MEAS:FREQuency?\n");
  
      // Register only for the receive event
      lLastError = serial.SetMask(CSerial::EEventBreak |
                                  CSerial::EEventCTS   |
                                  CSerial::EEventDSR   |
                                  CSerial::EEventError |
                                  CSerial::EEventRLSD  |
                                  CSerial::EEventRecv);
      if (lLastError != ERROR_SUCCESS)
           return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port event mask"));
  
      // Use 'non-blocking' reads,cuz we don't know how many bytes will be received. 
      lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);
  
      if (lLastError != ERROR_SUCCESS)
          return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port read timeout."));
  
      // Keep reading data, until an EOF (CTRL-Z) has been received
      bool fContinue = true;
  
      do
      {
          // Wait for an event
          lLastError = serial.WaitEvent();
  
          if (lLastError != ERROR_SUCCESS)
              return ::ShowError(serial.GetLastError(), _T("Unable to wait for a COM-port event."));
  
          // Save event
          const CSerial::EEvent eEvent = serial.GetEventType();
  
      // Handle break event
      if (eEvent &amp; CSerial::EEventBreak)
      {   printf("\n### BREAK received ###\n");  }
      // Handle CTS event
      if (eEvent &amp; CSerial::EEventCTS)
      {   printf("\n### Clear to send %s ###\n", serial.GetCTS()?"on":"off");           }
      // Handle error event
      if (eEvent &amp; CSerial::EEventError)
      {
          printf("\n### ERROR: ");
          switch (serial.GetError())
          {
          case CSerial::EErrorBreak:      printf("Break condition");          break;
          case CSerial::EErrorMode:       printf("Unsupported mode");         break;
          case CSerial::EErrorOverrun:    printf("Buffer overrun");           break;
          case CSerial::EErrorTxFull:     printf("Output buffer full");       break;
          default:                        printf("Unknown");                  break;
          }
          printf(" ###\n");
      }
      // Handle RLSD/CD event
      if (eEvent &amp; CSerial::EEventRLSD)
      {   printf("\n### RLSD/CD %s ###\n", serial.GetRLSD()?"on":"off"); }
  
      // Handle data receive event
      if (eEvent &amp; CSerial::EEventRecv)
      {
          // Read data, until there is nothing left
          DWORD dwBytesRead = 0;
          char szBuffer[101];
          do
          {
              // Read data from the COM-port
              lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&amp;dwBytesRead);
              if (lLastError != ERROR_SUCCESS)
                  return ::ShowError(serial.GetLastError(), _T("Unable to read from COM-port."));
  
              if (dwBytesRead &gt; 0)
              {
                  // Finalize the data, so it is a valid string
                  szBuffer[dwBytesRead] = '\0';
  
                  // **Display the data/Need the same output for EditBox**
                  printf("%s", szBuffer);
                  // Check if EOF (CTRL+'[') has been specified
                  if (strchr(szBuffer,EOF_Char))
                      fContinue = false;
              }
          }
          while (dwBytesRead == sizeof(szBuffer)-1);
      }
  }
  while (fContinue);
  
  // Close the port again
  serial.Close();
  return 0;
  }
  </code></pre>