Góp ý việc lấy output data từ 1 console app và hiện thị output tương tự lên MFC edit control box

Chào mọi người, mình đang làm 1 MFC project cần lấy dữ liệu output giống với 1 console app. Console app có thể hiện hiện thị dữ liệu đến từ serial port. Mình muốn dữ liệu tương tự được hiện thị output giống như console app lên Dialog edit control box. Việc này khá khó so với người mới làm quen với C++ như mình. Mong mọi người góp ý phương án . Thanks in advance !!

The console app 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 & CSerial::EEventBreak)
    {   printf("\n### BREAK received ###\n");  }
    // Handle CTS event
    if (eEvent & CSerial::EEventCTS)
    {   printf("\n### Clear to send %s ###\n", serial.GetCTS()?"on":"off");           }
    // Handle error event
    if (eEvent & 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 & CSerial::EEventRLSD)
    {   printf("\n### RLSD/CD %s ###\n", serial.GetRLSD()?"on":"off"); }

    // Handle data receive event
    if (eEvent & 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,&dwBytesRead);
            if (lLastError != ERROR_SUCCESS)
                return ::ShowError(serial.GetLastError(), _T("Unable to read from COM-port."));

            if (dwBytesRead > 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;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?