如何使用控制台应用程序获取输出并将其显示到MFC Dialog StaticText

时间:2016-09-20 06:22:39

标签: visual-c++ mfc console-application

我正在开发一个MFC应用程序,需要使用控制台应用程序获得相同的输出。控制台应用程序可以显示串行端口的输入数据。我想通过MFC对话框应用程序获取输入并将其显示在编辑控制框中。由于我还是C ++的新手,对我来说这似乎很难。有关解决方法的任何建议吗?提前谢谢!

控制台应用代码:

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;
}

1 个答案:

答案 0 :(得分:0)

由于我对C ++的了解不够好,所以我不知道我的问题是如何在上面的代码中打破循环。 char数组和CString之间的转换。这是我的答案:

 void Singlemode::OnBnClickedButton3()
 {
     CString str3;
    // TODO: Add your control notification handler code here
    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);
    lLastError = serial.Write(":MEAS:FREQuency?\n");

    // Register only for the receive event

    // Use 'non-blocking' reads, because we don't know how many bytes
    // will be received. This is normally the most convenient mode
    // (and also the default mode for reading data).
  bool fContinue = true;
   do
    {
        // Wait for an event
       lLastError = serial.WaitEvent();
       // Save event
      const CSerial::EEvent eEvent = serial.GetEventType();
      // 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 (dwBytesRead > 0)
              {
                // Finalize the data, so it is a valid string
                szBuffer[dwBytesRead] = '\0';

                // Display the data
                str3 = CString(szBuffer); // conversion to display
                show_result.SetWindowText(str3);
                fContinue = false;   // break the loop
                }
            } while (dwBytesRead == sizeof(szBuffer) - 1);
         }
    } while (fContinue);
  }