本人机器有两个串口,com1,com2,其中com1接了串口线,com2没接,当用户让com2发送
数据时,想给一个提示,没有接串口线。怎么办,有没有api函数,程序怎么写?(我用的是
api函数写的串口程序)

解决方案 »

  1.   

    有API函数,WaitCommEvent;
    BOOL WaitCommEvent(
      HANDLE hFile,                // handle to comm device
      LPDWORD lpEvtMask,           // event type
      LPOVERLAPPED lpOverlapped,   // overlapped structure
    );
    Parameters
    hFile 
    [in] Handle to the communications device. The CreateFile function returns this handle. 
    lpEvtMask 
    [out] Pointer to a variable that receives a mask indicating the type of event that occurred. If an error occurs, the value is zero; otherwise, it is one of the following values. Value Meaning 
    EV_BREAK A break was detected on input. 
    EV_CTS The CTS (clear-to-send) signal changed state. 
    EV_DSR The DSR (data-set-ready) signal changed state. 
    EV_ERR A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY. 
    EV_RING A ring indicator was detected. 
    EV_RLSD The RLSD (receive-line-signal-detect) signal changed state. 
    EV_RXCHAR A character was received and placed in the input buffer. 
    EV_RXFLAG The event character was received and placed in the input buffer. The event character is specified in the device's DCB structure, which is applied to a serial port by using the SetCommState function. 
    EV_TXEMPTY The last character in the output buffer was sent. 
    不过通常串口是只接三根线,很难判断!
      

  2.   

    当OpenFile Com2时,没接就返回失败咯
      

  3.   

    通信超时:在通信中,超时是个很重要的考虑因素,因为如果在数据接收过程中由于某种原因突然中断或停止,如果不采取超时控制机制,将会使得I/O线程被挂起或无限阻塞。串口通信中的超时设置分为两步,首先设置 COMMTIMEOUTS结构的五个变量,然后调用SetcommTimeouts()设置超时值。对于使用异步方式读写的操作,如果操作挂起后,异步成功完成了读写,WaitForSingleObject()或 WaitForMultipleObjects()函数将返回WAIT_OBJECT_0,GetOverlappedResult()返回TRUE。其实还可以用GetCommTimeouts()得到系统初始值。