CSerialPort类源代码分析

来源:互联网 发布:网络阅读利大于弊 编辑:程序博客网 时间:2024/05/13 00:53
前几篇串口编程大致讲述了Windows下串口的大致操作,接下来分析流行的SerialPort类,它把Windows API封装好,方便开发利用

1、Win32下串口大致操作流程
(1)打开串口:CreateFile函数
(2)建立串口通信事件:CreateEvent函数
(3)初始化串口:SetCommState函数
(4)建立监视线程,即读写数据线程,因为我们不知道什么时候数据会到来,这里是一个异步事件
(5)写数据:WriteFile
(6)结束:关闭线程->停止WaitCommEvent->CloseHandle

2.SerialPort类的数据结构
大致了解操作流程后,先看一下SerialPort类,均在代码注释了
数据成员:

01.public:  02.    int     m_nWriteSize;   //要写入串口的数据大小  03.    HANDLE  m_hComm;    //串口句柄  04.protected:  05.    // thread监视线程  06.    CWinThread*         m_Thread;  07.  08.    // synchronisation objects  09.    //临界资源  10.    CRITICAL_SECTION    m_csCommunicationSync;  11.    //监视线程运行标志  12.    BOOL                m_bThreadAlive;  13.  14.    // handles  15.    /*事件句柄*/  16.    HANDLE              m_hWriteEvent;  17.    HANDLE              m_hShutdownEvent;  18.  19.    // There is a general shutdown when the port is closed.   20.    //事件数组,包括一个写事件,接收事件,关闭事件  21.    HANDLE              m_hEventArray[3];  22.  23.    // structures  24.    OVERLAPPED          m_ov;   //异步I/O模型  25.    COMMTIMEOUTS        m_CommTimeouts; //超时设置  26.    DCB         m_dcb;      //设备控制块  27.  28.    // owner window  29.    CWnd*               m_pOwner;  30.  31.    // misc  32.    UINT                m_nPortNr;  33.    char*               m_szWriteBuffer;        //写缓冲区  34.    DWORD               m_dwCommEvents;         //  35.    DWORD               m_nWriteBufferSize;     //写缓冲大小  

函数成员:

01.public:  02.    /*******************Port Operation***********************/  03.  04.    // port initialisation        05.    /*初始化串口*/  06.    BOOL        InitPort(CWnd* pPortOwner,   07.                UINT portnr = 1,  08.                UINT baud = 19200,   09.                char parity = 'N',   10.                UINT databits = 8,   11.                UINT stopbits = 1,   12.                DWORD dwCommEvents = EV_RXCHAR,   13.                UINT writebuffersize = 1024);  14.    //关闭端口  15.    void ClosePort();  16.    // start/stop comm watching  17.    //控制串口监视线程  18.    BOOL        StartMonitoring();  //开启  19.    BOOL        RestartMonitoring();    //复位  20.    BOOL        StopMonitoring();   //停止  21.  22.    DWORD       GetWriteBufferSize();//获取写缓冲大小  23.    DWORD       GetCommEvents();    //获取事件  24.    DCB         GetDCB();   //获取DCB  25.    //写数据到串口  26.    void        WriteToPort(char* string);  27.    void        WriteToPort(char* string,int n);  28.    void        WriteToPort(LPCTSTR string);  29.    void        WriteToPort(LPCTSTR string,int n);  30.protected:  31./***************** protected memberfunctions **********************/  32.    void        ProcessErrorMessage(char* ErrorText);  33.    //线程函数  34.    static UINT CommThread(LPVOID pParam);  35.    //接收字符  36.    static void ReceiveChar(CSerialPort* port, COMSTAT comstat);  37.    //写字符  38.    static void WriteChar(CSerialPort* port);  


 

3.串口操作

(1)初始化串口

流程:检查参数-->检测线程-->创建事件(监视线程)-->打开端口-->设置异步IO结构参数,详细见代码:

01./*初始化串口*/  02.BOOL CSerialPort::InitPort(CWnd* pPortOwner,    // the owner (CWnd) of the port (receives message)  03.                           UINT  portnr,        // portnumber (1..4)  04.                           UINT  baud,          // baudrate  05.                           char  parity,        // parity   06.                           UINT  databits,      // databits   07.                           UINT  stopbits,      // stopbits   08.                           DWORD dwCommEvents,  // EV_RXCHAR, EV_CTS etc  09.                           UINT  writebuffersize)   // size to the writebuffer  10.{  11.    assert(portnr > 0 && portnr < 5);  12.    assert(pPortOwner != NULL);  13.  14.    // if the thread is alive: Kill  15.    //线程在的话关断它  16.    if (m_bThreadAlive)  17.    {  18.        do  19.        {  20.            SetEvent(m_hShutdownEvent);  21.        } while (m_bThreadAlive);  22.        TRACE("Thread ended/n");  23.    }  24.  25.    // create events  26.    //创建事件  27.    if (m_ov.hEvent != NULL)  28.        ResetEvent(m_ov.hEvent);  29.    else  30.        m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);  31.  32.    if (m_hWriteEvent != NULL)  33.        ResetEvent(m_hWriteEvent);  34.    else  35.        m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);  36.      37.    if (m_hShutdownEvent != NULL)  38.        ResetEvent(m_hShutdownEvent);  39.    else  40.        m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);  41.  42.    // initialize the event objects  43.    //事件数组初始化,设定优先级别  44.    m_hEventArray[0] = m_hShutdownEvent;    // highest priority  45.    m_hEventArray[1] = m_ov.hEvent;  46.    m_hEventArray[2] = m_hWriteEvent;  47.  48.    // initialize critical section  49.    //初始化一个临界资源对象  50.    InitializeCriticalSection(&m_csCommunicationSync);  51.      52.    // set buffersize for writing and save the owner  53.    m_pOwner = pPortOwner;  54.  55.    if (m_szWriteBuffer != NULL)  56.        delete [] m_szWriteBuffer;  57.    m_szWriteBuffer = new char[writebuffersize];  58.  59.    m_nPortNr = portnr;  60.  61.    m_nWriteBufferSize = writebuffersize;  62.    m_dwCommEvents = dwCommEvents;  63.  64.    BOOL bResult = FALSE;  65.    char *szPort = new char[50];  66.    char *szBaud = new char[50];  67.  68.    // now it critical!  69.    /********************************************* 70.    多个线程操作相同的数据时,一般是需要按顺序访问的,否则会引导数据错乱, 71.    无法控制数据,变成随机变量。为解决这个问题,就需要引入互斥变量,让每个 72.    线程都按顺序地访问变量。这样就需要使用EnterCriticalSection和LeaveCriticalSection函数。 73.    **********************************************************************/  74.    //进入临界区  75.    EnterCriticalSection(&m_csCommunicationSync);  76.  77.    // if the port is already opened: close it  78.    //端口已经打开的就关闭它  79.    if (m_hComm != NULL)  80.    {  81.        CloseHandle(m_hComm);  82.        m_hComm = NULL;  83.    }  84.  85.    // prepare port strings  86.    //串口参数  87.    sprintf(szPort, "COM%d", portnr);  88.    sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);  89.  90.    // get a handle to the port  91.    /**************************************************************** 92.    *通信程序在CreateFile处指定串口设备及相关的操作属性,再返回一个句柄, 93.    *该句柄将被用于后续的通信操作,并贯穿整个通信过程串口打开后,其属性 94.    *被设置为默认值,根据具体需要,通过调用GetCommState(hComm,&&dcb)读取 95.    *当前串口设备控制块DCB设置,修改后通过SetCommState(hComm,&&dcb)将其写 96.    *入。运用ReadFile()与WriteFile()这两个API函数实现串口读写操作,若为异 97.    *步通信方式,两函数中最后一个参数为指向OVERLAPPED结构的非空指针,在读 98.    *写函数返回值为FALSE的情况下,调用GetLastError()函数,返回值为ERROR_IO_PENDING, 99.    *表明I/O操作悬挂,即操作转入后台继续执行。此时,可以用WaitForSingleObject() 100.    *来等待结束信号并设置最长等待时间 101.    *****************************************************************/  102.    m_hComm = CreateFile(szPort,                        // communication port string (COMX)串口号  103.                         GENERIC_READ | GENERIC_WRITE,  // read/write types 可以读写  104.                         0,                             // comm devices must be opened with exclusive access独占方式打开串口  105.                         NULL,                          // no security attributes 无安全属性  106.                         OPEN_EXISTING,                 // comm devices must use OPEN_EXISTING  107.                         FILE_FLAG_OVERLAPPED,          // Async I/O 异步I/O  108.                         0);                            // template must be 0 for comm devices  109.  110.    //如果创建不成功,错误处理  111.    if (m_hComm == INVALID_HANDLE_VALUE)  112.    {  113.        // port not found  114.        delete [] szPort;  115.        delete [] szBaud;  116.  117.        return FALSE;  118.    }  119.  120.    // set the timeout values  121.    //设置超时上限(异步IO)  122.    m_CommTimeouts.ReadIntervalTimeout = 1000;  123.    m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;  124.    m_CommTimeouts.ReadTotalTimeoutConstant = 1000;  125.    m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;  126.    m_CommTimeouts.WriteTotalTimeoutConstant = 1000;  127.  128.    // configure  129.    /*分别调用Windows API设置串口参数*/  130.    if (SetCommTimeouts(m_hComm, &m_CommTimeouts))          //设置超时  131.    {     132.        /******************************************************* 133.        若对端口数据的响应时间要求较严格,可采用事件驱动方式。 134.        事件驱动方式通过设置事件通知,当所希望的事件发生时,Windows 135.        发出该事件已发生的通知,这与DOS环境下的中断方式很相似。Windows 136.        定义了9种串口通信事件,较常用的有以下三种:  137. 138.        EV_RXCHAR:接收到一个字节,并放入输入缓冲区;  139. 140.        EV_TXEMPTY:输出缓冲区中的最后一个字符,发送出去;  141. 142.        EV_RXFLAG:接收到事件字符(DCB结构中EvtChar成员),放入输入缓冲区 143. 144.        在用SetCommMask()指定了有用的事件后,应用程序可调用WaitCommEvent()来等待事 145.        件的发生。SetCommMask(hComm,0)可使WaitCommEvent()中止。 146.        **************************************************************/              147.        if (SetCommMask(m_hComm, dwCommEvents))             //设置通信事件   148.        {  149.            if (GetCommState(m_hComm, &m_dcb))              //获取当前DCB参数  150.            {  151.                m_dcb.EvtChar = 'q';                    //设置字件字符  152.                m_dcb.fRtsControl = RTS_CONTROL_ENABLE;     // set RTS bit high!  153.                if (BuildCommDCB(szBaud, &m_dcb))       //填写DCB结构  154.                {  155.                    if (SetCommState(m_hComm, &m_dcb))  //配置DCB  156.                        ; // normal operation... continue  157.                    else  158.                        ProcessErrorMessage("SetCommState()");  159.                }  160.                else  161.                    ProcessErrorMessage("BuildCommDCB()");  162.            }  163.            else  164.                ProcessErrorMessage("GetCommState()");  165.        }  166.        else  167.            ProcessErrorMessage("SetCommMask()");  168.    }  169.    else  170.        ProcessErrorMessage("SetCommTimeouts()");  171.  172.    delete [] szPort;  173.    delete [] szBaud;  174.  175.    // flush the port  176.    //终止读写并清空接收和发送  177.    PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);  178.  179.    // release critical section  180.    //出临界区  181.    LeaveCriticalSection(&m_csCommunicationSync);  182.  183.    TRACE("Initialisation for communicationport %d completed./nUse Startmonitor to communicate./n", portnr);  184.  185.    return TRUE;  186.}  


 

(2)监视线程的控制

先看比较简单的线程控制吧,主要有开启线程,复位和停止

01.//开始监视串口  02.BOOL CSerialPort::StartMonitoring()  03.{  04.    if (!(m_Thread = AfxBeginThread(CommThread, this)))  05.        return FALSE;  06.    TRACE("Thread started/n");  07.    return TRUE;      08.}  09.  10.//  11.// Restart the comm thread  12.//  13.//重启监视线程(挂起重启)  14.BOOL CSerialPort::RestartMonitoring()  15.{  16.    TRACE("Thread resumed/n");  17.    m_Thread->ResumeThread();  18.    return TRUE;      19.}  20.  21.  22.//  23.// Suspend the comm thread  24.//  25.//挂起监视线程  26.BOOL CSerialPort::StopMonitoring()  27.{  28.    TRACE("Thread suspended/n");  29.    m_Thread->SuspendThread();   30.    return TRUE;      31.}  


 

(3)监视线程

我们把读写串口的操作全部交给监视线程,现在简单看一下监视线程的大致流程:

检查串口-->进入循环{WaitCommEvent(不阻塞询问)询问事件-->如果有事件来到-->到相应处理(关闭/读/写)}

详细代码如下:

01.//监视串口函数  02.UINT CSerialPort::CommThread(LPVOID pParam)  03.{  04.    // Cast the void pointer passed to the thread back to  05.    // a pointer of CSerialPort class  06.    CSerialPort *port = (CSerialPort*)pParam;  07.      08.    // Set the status variable in the dialog class to  09.    // TRUE to indicate the thread is running.  10.    port->m_bThreadAlive = TRUE;   11.          12.    // Misc. variables  13.    DWORD BytesTransfered = 0;   14.    DWORD Event = 0;  15.    DWORD CommEvent = 0;  16.    DWORD dwError = 0;  17.    COMSTAT comstat;  18.    BOOL  bResult = TRUE;  19.          20.    // Clear comm buffers at startup  21.    //检查串口是否打开  22.    if (port->m_hComm)       // check if the port is opened  23.        PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);  24.  25.    // begin forever loop.  This loop will run as long as the thread is alive.  26.    //不断读取数据  27.    for (;;)   28.    {   29.  30.        // Make a call to WaitCommEvent().  This call will return immediatly  31.        // because our port was created as an async port (FILE_FLAG_OVERLAPPED  32.        // and an m_OverlappedStructerlapped structure specified).  This call will cause the   33.        // m_OverlappedStructerlapped element m_OverlappedStruct.hEvent, which is part of the m_hEventArray to   34.        // be placed in a non-signeled state if there are no bytes available to be read,  35.        // or to a signeled state if there are bytes available.  If this event handle   36.        // is set to the non-signeled state, it will be set to signeled when a   37.        // character arrives at the port.  38.  39.        // we do this for each port!  40.        /******************************************************************** 41.        WaitCommEvent函数第3个参数1pOverlapped可以是一个OVERLAPPED结构的变量指针 42.        ,也可以是NULL,当用NULL时,表示该函数是同步的,否则表示该函数是异步的。 43.        调用WaitCommEvent时,如果异步操作不能立即完成,会立即返回FALSE,系统在 44.        WaitCommEvent返回前将OVERLAPPED结构成员hEvent设为无信号状态,等到产生通信 45.        事件时,系统将其置有信号 46.        ***********************************************************************/  47.        bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);//表示该函数是异步的   48.  49.          50.        if (!bResult)    51.        {   52.            //如果WaitCommEvent返回Error为NULL,则查询错误信息  53.            // If WaitCommEvent() returns FALSE, process the last error to determin  54.            // the reason..  55.            switch (dwError = GetLastError())   56.            {   57.            case ERROR_IO_PENDING:      //正常情况,没有字符可读  58.                {   59.                    // This is a normal return value if there are no bytes  60.                    // to read at the port.  61.                    // Do nothing and continue  62.                    break;  63.                }  64.            case 87:                    //系统错误  65.                {  66.                    // Under Windows NT, this value is returned for some reason.  67.                    // I have not investigated why, but it is also a valid reply  68.                    // Also do nothing and continue.  69.                    break;  70.                }  71.            default:                    //发生其他错误  72.                {  73.                    // All other error codes indicate a serious error has  74.                    // occured.  Process this error.  75.                    port->ProcessErrorMessage("WaitCommEvent()");  76.                    break;  77.                }  78.            }  79.        }  80.        else                //WaitCommEvent()能正确返回  81.        {  82.            // If WaitCommEvent() returns TRUE, check to be sure there are  83.            // actually bytes in the buffer to read.    84.            //  85.            // If you are reading more than one byte at a time from the buffer   86.            // (which this program does not do) you will have the situation occur   87.            // where the first byte to arrive will cause the WaitForMultipleObjects()   88.            // function to stop waiting.  The WaitForMultipleObjects() function   89.            // resets the event handle in m_OverlappedStruct.hEvent to the non-signelead state  90.            // as it returns.    91.            //  92.            // If in the time between the reset of this event and the call to   93.            // ReadFile() more bytes arrive, the m_OverlappedStruct.hEvent handle will be set again  94.            // to the signeled state. When the call to ReadFile() occurs, it will   95.            // read all of the bytes from the buffer, and the program will  96.            // loop back around to WaitCommEvent().  97.            //   98.            // At this point you will be in the situation where m_OverlappedStruct.hEvent is set,  99.            // but there are no bytes available to read.  If you proceed and call  100.            // ReadFile(), it will return immediatly due to the async port setup, but  101.            // GetOverlappedResults() will not return until the next character arrives.  102.            //  103.            // It is not desirable for the GetOverlappedResults() function to be in   104.            // this state.  The thread shutdown event (event 0) and the WriteFile()  105.            // event (Event2) will not work if the thread is blocked by GetOverlappedResults().  106.            //  107.            // The solution to this is to check the buffer with a call to ClearCommError().  108.            // This call will reset the event handle, and if there are no bytes to read  109.            // we can loop back through WaitCommEvent() again, then proceed.  110.            // If there are really bytes to read, do nothing and proceed.  111.          112.            bResult = ClearCommError(port->m_hComm, &dwError, &comstat);  113.  114.            if (comstat.cbInQue == 0)  115.                continue;  116.        }   // end if bResult  117.          118.        //主等待函数,会阻塞线程  119.        // Main wait function.  This function will normally block the thread  120.        // until one of nine events occur that require action.  121.        //等待3个事件:关断/读/写,有一个事件发生就返回  122.        Event = WaitForMultipleObjects(3,               //3个事件  123.                                    port->m_hEventArray,//事件数组  124.                                    FALSE,              //有一个事件发生就返回  125.                                    INFINITE);          //超时时间  126.  127.        switch (Event)  128.        {  129.        case 0:  130.            {  131.                // Shutdown event.  This is event zero so it will be  132.                // the higest priority and be serviced first.  133.                //关断事件,关闭串口  134.                CloseHandle(port->m_hComm);  135.                port->m_hComm=NULL;  136.                port->m_bThreadAlive = FALSE;  137.                  138.                // Kill this thread.  break is not needed, but makes me feel better.  139.                AfxEndThread(100);  140.  141.                break;  142.            }  143.        case 1: // read event   将定义的各种消息发送出去  144.            {  145.                //接收  146.                GetCommMask(port->m_hComm, &CommEvent);  147.                if (CommEvent & EV_RXCHAR)  148.                    // Receive character event from port.  149.                    ReceiveChar(port, comstat);  150.                if (CommEvent & EV_CTS)  151.                    ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);  152.                if (CommEvent & EV_BREAK)  153.                    ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);  154.                if (CommEvent & EV_ERR)  155.                    ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);  156.                if (CommEvent & EV_RING)  157.                    ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);  158.                  159.                if (CommEvent & EV_RXFLAG)  160.                    ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED, (WPARAM) 0, (LPARAM) port->m_nPortNr);  161.                      162.                break;  163.            }    164.        case 2: // write event发送数据  165.            {  166.                // Write character event from port  167.                //写  168.                WriteChar(port);  169.                break;  170.            }  171.  172.        } // end switch  173.  174.    } // close forever loop  175.  176.    return 0;  177.}  


 

(4)读取数据操作

读取数据是一个异步操作,当有数据发来时,会触发读事件m_ov.hEvent,监视线程捕捉到事件后并获知是读事件,进入相关读处理,这里调用函数ReceiveChar,ReceiveChar中调用ReadFile函数将串口数据读到Buffer缓冲中,相关代码如下:

01.//接收数据  02.void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)  03.{  04.    BOOL  bRead = TRUE;   05.    BOOL  bResult = TRUE;  06.    DWORD dwError = 0;  07.    DWORD BytesRead = 0;  08.    unsigned char RXBuff;  09.  10.    for (;;)   11.    {   12.        // Gain ownership of the comm port critical section.  13.        // This process guarantees no other part of this program   14.        // is using the port object.   15.          16.        EnterCriticalSection(&port->m_csCommunicationSync);  17.  18.        // ClearCommError() will update the COMSTAT structure and  19.        // clear any other errors.  20.        //更新COMSTAT  21.        bResult = ClearCommError(port->m_hComm, &dwError, &comstat);  22.  23.        LeaveCriticalSection(&port->m_csCommunicationSync);  24.  25.        // start forever loop.  I use this type of loop because I  26.        // do not know at runtime how many loops this will have to  27.        // run. My solution is to start a forever loop and to  28.        // break out of it when I have processed all of the  29.        // data available.  Be careful with this approach and  30.        // be sure your loop will exit.  31.        // My reasons for this are not as clear in this sample   32.        // as it is in my production code, but I have found this   33.        // solutiion to be the most efficient way to do this.  34.        //所有字符均被读出,中断循环  35.        if (comstat.cbInQue == 0)  36.        {  37.            // break out when all bytes have been read  38.            break;  39.        }  40.                          41.        EnterCriticalSection(&port->m_csCommunicationSync);  42.  43.        if (bRead)  44.        {  45.            //串口读出,读出缓冲区中字节  46.            bResult = ReadFile(port->m_hComm,        // Handle to COMM port   47.                               &RXBuff,             // RX Buffer Pointer  48.                               1,                   // Read one byte  49.                               &BytesRead,          // Stores number of bytes read  50.                               &port->m_ov);     // pointer to the m_ov structure  51.            // deal with the error code   52.            //若返回错误,错误处理  53.            if (!bResult)    54.            {   55.                switch (dwError = GetLastError())   56.                {   57.                    case ERROR_IO_PENDING:    58.                        {   59.                            // asynchronous i/o is still in progress   60.                            // Proceed on to GetOverlappedResults();  61.                            //异步IO仍在进行  62.                            bRead = FALSE;  63.                            break;  64.                        }  65.                    default:  66.                        {   //其他错误处理  67.                            // Another error has occured.  Process this error.  68.                            port->ProcessErrorMessage("ReadFile()");  69.                            break;  70.                        }   71.                }  72.            }  73.            else  74.            {   //ReadFile返回TRUE  75.                // ReadFile() returned complete. It is not necessary to call GetOverlappedResults()  76.                bRead = TRUE;  77.            }  78.        }  // close if (bRead)  79.        //异步IO操作仍在进行,需要调用GetOverlappedResult查询  80.        if (!bRead)  81.        {  82.            bRead = TRUE;  83.            bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port   84.                                          &port->m_ov,       // Overlapped structure  85.                                          &BytesRead,       // Stores number of bytes read  86.                                          TRUE);            // Wait flag  87.  88.            // deal with the error code   89.            if (!bResult)    90.            {  91.                port->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");  92.            }     93.        }  // close if (!bRead)  94.                  95.        LeaveCriticalSection(&port->m_csCommunicationSync);  96.  97.        // notify parent that a byte was received  98.        ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) port->m_nPortNr);  99.    } // end forever loop  100.  101.}  


 

(5)写数据

也是由监视线程操作,不过触发事件交给主线程来触发,函数是WriteToPort

01.//  02.// Write a string to the port  03.//  04.void CSerialPort::WriteToPort(char* string)  05.{         06.    assert(m_hComm != 0);  07.    //写进写缓冲区  08.    memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));  09.    strcpy(m_szWriteBuffer, string);  10.    m_nWriteSize=strlen(string);  11.  12.    // set event for write  13.    SetEvent(m_hWriteEvent);  14.}  


 

线程调用的函数WriteChar,把缓冲里的数据写到串口中,期间调用WriteFile 详细代码:

01.//写数据  02.void CSerialPort::WriteChar(CSerialPort* port)  03.{  04.    BOOL bWrite = TRUE;  05.    BOOL bResult = TRUE;  06.  07.    DWORD BytesSent = 0;  08.  09.    ResetEvent(port->m_hWriteEvent);     //复位写事件句柄  10.  11.    // Gain ownership of the critical section  12.    EnterCriticalSection(&port->m_csCommunicationSync);  13.  14.    if (bWrite)  15.    {  16.        // Initailize variables  17.        port->m_ov.Offset = 0;  18.        port->m_ov.OffsetHigh = 0;  19.  20.        // Clear buffer  21.        PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);  22.  23.        //串口写入  24.        bResult = WriteFile(port->m_hComm,                           // Handle to COMM Port  25.                            port->m_szWriteBuffer,                   // Pointer to message buffer in calling finction  26.//                          strlen((char*)port->m_szWriteBuffer),    // Length of message to send  27.                            port->m_nWriteSize,  // Length of message to send  28.                            &BytesSent,                             // Where to store the number of bytes sent  29.                            &port->m_ov);                            // Overlapped structure  30.  31.        // deal with any error codes  32.        if (!bResult)    33.        {  34.            DWORD dwError = GetLastError();  35.            switch (dwError)  36.            {  37.                case ERROR_IO_PENDING:  38.                    {  39.                        // continue to GetOverlappedResults()  40.                        BytesSent = 0;  41.                        bWrite = FALSE;  42.                        break;  43.                    }  44.                default:  45.                    {  46.                        // all other error codes  47.                        port->ProcessErrorMessage("WriteFile()");  48.                    }  49.            }  50.        }   51.        else  52.        {  53.            LeaveCriticalSection(&port->m_csCommunicationSync);  54.        }  55.    } // end if(bWrite)  56.  57.    if (!bWrite)  58.    {  59.        bWrite = TRUE;  60.      61.        bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port   62.                                      &port->m_ov,       // Overlapped structure  63.                                      &BytesSent,       // Stores number of bytes sent  64.                                      TRUE);            // Wait flag  65.  66.        LeaveCriticalSection(&port->m_csCommunicationSync);  67.  68.        // deal with the error code   69.//      if (!bResult)    70.        {  71.//          port->ProcessErrorMessage("GetOverlappedResults() in WriteFile()");  72.        }     73.    } // end if (!bWrite)  74.  75.    //Verify that the data size send equals what we tried to send  76.    if (BytesSent != port->m_nWriteSize) // Length of message to send)  77.    {  78.        TRACE("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d/n", BytesSent, strlen((char*)port->m_szWriteBuffer));  79.    }  80.//  ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_TXEMPTY_DETECTED, (WPARAM) RXBuff, (LPARAM) port->m_nPortNr);  81.//  ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_TXEMPTY_DETECTED,0,(LPARAM) port->m_nPortNr);  82.}  

 

(6)其他操作

其他比如获取DCB,关闭等,比较简单,不做分析  代码如下:

01.//  02.// Return the device control block  03.//  04.DCB CSerialPort::GetDCB()  05.{  06.    return m_dcb;  07.}  08.  09.//  10.// Return the communication event masks  11.//  12.DWORD CSerialPort::GetCommEvents()  13.{  14.    return m_dwCommEvents;  15.}  16.  17.//  18.// Return the output buffer size  19.//  20.DWORD CSerialPort::GetWriteBufferSize()  21.{  22.    return m_nWriteBufferSize;  23.}  24.  25.  26.void CSerialPort::ClosePort()  27.{  28.        SetEvent(m_hShutdownEvent);  29.}