none
串口接收到的数据如何显示在MFC窗口上? RRS feed

全部回复

  • GT_MDRG 你好:

    根据你的描述,这里我给你找了些代码,你可以参考。

    BOOL CTryDlg::OnInitDialog() 
    { 
    CDialog::OnInitDialog(); 
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
    ASSERT(IDM_ABOUTBOX < 0xF000); 
    CMenu* pSysMenu = GetSystemMenu(FALSE); 
    if (pSysMenu != NULL) 
    { 
      CString strAboutMenu; 
      strAboutMenu.LoadString(IDS_ABOUTBOX); 
      if (!strAboutMenu.IsEmpty()) 
      { 
       pSysMenu->AppendMenu(MF_SEPARATOR); 
       pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
      } 
    } 
    // Set the icon for this dialog.  The framework does this automatically 
    //  when the application's main window is not a dialog 
    
    m_ctrlComm.SetCommPort(3); //选择COM3 
        m_ctrlComm.SetInputMode(1); //输入方式为二进制方式 
        m_ctrlComm.SetInBufferSize(1024); //设置输入缓冲区大小 
    m_ctrlComm.SetOutBufferSize(512); //设置输出缓冲区大小 
    //波特率9600,无校验,8个数据位,1个停止位 
    m_ctrlComm.SetSettings("9600,n,8,1"); 
    //参数1表示当串口接收缓冲区中有多于或者等于1个字符时将引发一个接收数据的OnOnCommMscomm1事件 
    if(!m_ctrlComm.GetPortOpen()) 
      m_ctrlComm.SetPortOpen(1); 
    m_ctrlComm.SetRThreshold(1); 
    m_ctrlComm.SetInputLen(0); //设置当前接受区长度为0 
    m_ctrlComm.GetInput(); //先预读缓冲区以清除残留数据 
    return TRUE;  // return TRUE  unless you set the focus to a control 
    } 
    (2)void CTryDlg::OnOnCommMscomm1() //从串口接收数据并显示在接受编辑框中 
    { 
      VARIANT variant_inp; 
      COleSafeArray safearray_inp; 
      LONG len,k; 
      BYTE rxdata[2048]; 
      CString strtemp; 
      if(m_ctrlComm.GetCommEvent() == 2) //如果接收缓冲区内有字符 
      { 
        variant_inp = m_ctrlComm.GetInput(); //读缓冲区 
        safearray_inp = variant_inp; 
    len = safearray_inp.GetOneDimSize(); //得到有效数据长度 
    for(k=0;k<len;k++) 
    { 
         safearray_inp.GetElement(&k,rxdata+k); //转换为BYTE型数组 
    } 
    for(k=0;k<len;k++) //将数组转换为CString型数组 
    { 
         BYTE bt=*(char*)(rxdata+k); //字符型 
      strtemp.Format("%c",bt); 
      m_strEditRXDATA += strtemp; //加入接受编辑框对应字符 
    } 
      } 
      UpdateData(0); 
    } 
    (3)void CAaaaDlg::OnOK() 
    { 
    // TODO: Add extra validation here 
    
    UpdateData(1); //读取编辑框内容 
    //OnOnCommMscomm1(); 
    m_ctrlComm.SetOutput(COleVariant(m_strEditTXDATA)); //发送数据 
        UpdateData(0); 
    UpdateData(1); 
    if(IDOK!=MessageBox("请稍等.....您的数据还正在传输中.....","请稍后",MB_OK|MB_ICONINFORMATION)) return; 
    //for(int x=0;x<=5;x++) 
    OnOnCommMscomm1();
    }

    希望可以帮助到你。


    Sunny Cao
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年8月28日 12:50
    版主
  • GT_MDRG 你好:

    根据你的描述,这里我给你找了些代码,你可以参考。

    BOOL CTryDlg::OnInitDialog() 
    { 
    CDialog::OnInitDialog(); 
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
    ASSERT(IDM_ABOUTBOX < 0xF000); 
    CMenu* pSysMenu = GetSystemMenu(FALSE); 
    if (pSysMenu != NULL) 
    { 
      CString strAboutMenu; 
      strAboutMenu.LoadString(IDS_ABOUTBOX); 
      if (!strAboutMenu.IsEmpty()) 
      { 
       pSysMenu->AppendMenu(MF_SEPARATOR); 
       pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
      } 
    } 
    // Set the icon for this dialog.  The framework does this automatically 
    //  when the application's main window is not a dialog 
    
    m_ctrlComm.SetCommPort(3); //选择COM3 
        m_ctrlComm.SetInputMode(1); //输入方式为二进制方式 
        m_ctrlComm.SetInBufferSize(1024); //设置输入缓冲区大小 
    m_ctrlComm.SetOutBufferSize(512); //设置输出缓冲区大小 
    //波特率9600,无校验,8个数据位,1个停止位 
    m_ctrlComm.SetSettings("9600,n,8,1"); 
    //参数1表示当串口接收缓冲区中有多于或者等于1个字符时将引发一个接收数据的OnOnCommMscomm1事件 
    if(!m_ctrlComm.GetPortOpen()) 
      m_ctrlComm.SetPortOpen(1); 
    m_ctrlComm.SetRThreshold(1); 
    m_ctrlComm.SetInputLen(0); //设置当前接受区长度为0 
    m_ctrlComm.GetInput(); //先预读缓冲区以清除残留数据 
    return TRUE;  // return TRUE  unless you set the focus to a control 
    } 
    (2)void CTryDlg::OnOnCommMscomm1() //从串口接收数据并显示在接受编辑框中 
    { 
      VARIANT variant_inp; 
      COleSafeArray safearray_inp; 
      LONG len,k; 
      BYTE rxdata[2048]; 
      CString strtemp; 
      if(m_ctrlComm.GetCommEvent() == 2) //如果接收缓冲区内有字符 
      { 
        variant_inp = m_ctrlComm.GetInput(); //读缓冲区 
        safearray_inp = variant_inp; 
    len = safearray_inp.GetOneDimSize(); //得到有效数据长度 
    for(k=0;k<len;k++) 
    { 
         safearray_inp.GetElement(&k,rxdata+k); //转换为BYTE型数组 
    } 
    for(k=0;k<len;k++) //将数组转换为CString型数组 
    { 
         BYTE bt=*(char*)(rxdata+k); //字符型 
      strtemp.Format("%c",bt); 
      m_strEditRXDATA += strtemp; //加入接受编辑框对应字符 
    } 
      } 
      UpdateData(0); 
    } 
    (3)void CAaaaDlg::OnOK() 
    { 
    // TODO: Add extra validation here 
    
    UpdateData(1); //读取编辑框内容 
    //OnOnCommMscomm1(); 
    m_ctrlComm.SetOutput(COleVariant(m_strEditTXDATA)); //发送数据 
        UpdateData(0); 
    UpdateData(1); 
    if(IDOK!=MessageBox("请稍等.....您的数据还正在传输中.....","请稍后",MB_OK|MB_ICONINFORMATION)) return; 
    //for(int x=0;x<=5;x++) 
    OnOnCommMscomm1();
    }

    希望可以帮助到你。


    Sunny Cao
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    非常感谢你的回答,但……我的程序有一个窗口类,还有一个串口类,在串口类里面调用updatedata()会提示找不到标示符,这个问题如何解决呢?是否可以在串口类里面获取主窗口句柄,然后给主窗口发消息?
    2013年8月29日 2:09
  • AfxGetMainWnd()可以得到主对话框类的CWnd*指针,这样就可以PostMessage/SendMessage给主窗口了。

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2013年8月29日 2:36
    版主