这菜真辣 2015-12-07 11:09 采纳率: 0%
浏览 1545

VC++串口编程求助。。。。。

非常著名的serialport类中有这么一段代码:
bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);

    if (!bResult)  
    { 
        // If WaitCommEvent() returns FALSE, process the last error to determin
        switch (dwError = GetLastError()) 
        { 
        case ERROR_IO_PENDING:     
            { 
                break;
            }
        case 87:
            {
                break;
            }
        default:
            {
                port->ProcessErrorMessage("WaitCommEvent()");
                break;
            }
        }
    }
    else
    {

        bResult = ClearCommError(port->m_hComm, &dwError, &comstat);

        if (comstat.cbInQue == 0)
            continue;
    }    // end if bResult

        //m_hEventArray[0] = m_hShutdownEvent;    // highest priority
        //m_hEventArray[1] = m_ov.hEvent;
        //m_hEventArray[2] = m_hWriteEvent;
    Event = WaitForMultipleObjects(3, port->m_hEventArray, FALSE, INFINITE);

    switch (Event)
    {
    case 0:
        {
            // Shutdown event.  This is event zero so it will be// the higest priority and be serviced first.                
            CloseHandle(port->m_hComm);
            port->m_hComm=NULL;
            port->m_bThreadAlive = FALSE;                
            // Kill this thread.  break is not needed, but makes me feel better.
            AfxEndThread(100);

            break;
        }
    case 1:    // read event
        {
            GetCommMask(port->m_hComm, &CommEvent);
            if (CommEvent & EV_RXCHAR)
                // Receive character event from port.
                ReceiveChar(port, comstat);
            if (CommEvent & EV_CTS)
                ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
            if (CommEvent & EV_BREAK)
                ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
            if (CommEvent & EV_ERR)
                ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);
            if (CommEvent & EV_RING)
                ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);

            if (CommEvent & EV_RXFLAG)
                ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);

            break;
        }  
    case 2: // write event
        {
            // Write character event from port
            WriteChar(port);
            break;
        }

    } // end switch

} // close forever loop
    小弟不太明白overlapped中的hEvent的置位和复位时如何实现的呢?是人工置位复位还是自动置位复位呢 求指教
  • 写回答

1条回答 默认 最新

  • Robot-C 2015-12-07 11:13
    关注

    用vc++编写上位机:
     
    一是利用Windows API通信函数;二是利用VC的标准通信
    函数inp、inpw、inpd、outp、outpw、outpd等直接对串口进行操作;三是使用Microsoft Visual C++的通信控件(MSComm);
    四是利用第三方编写的通信类。以上几种方法中第一种使用面较广,但由于比较复杂,专业化程度较高,使用较困难;第二种需要了
    ......
    答案就在这里:vc++ 串口编程
    ----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    评论

报告相同问题?

悬赏问题

  • ¥15 依据报错在原代吗格式的基础上解决问题
  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
  • ¥25 LT码在高斯信道下的误码率仿真
  • ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
  • ¥15 yolov5目标检测并显示目标出现的时间或视频帧
  • ¥15 电视版的优酷可以设置电影连续播放吗?