The use of simplified original tutorial - communication serial port CxComm

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  This is a handy serial category, class, function and variable names are used in Hungarian notation. Lowercase x to represent my initials (Xie Xinneng), personal habits, if identical, is purely coincidental.

The use of CxComm as follows (related to the code in a class called CSomeClass as an example):

A statement object instance, serial port.

CxComm m_xComm;

Two, open the serial port.

Three, the configuration of the serial port (optional). The general configuration of baud rate, parity bits, stop bits.

Four, set the serial port (optional). Usually set the buffer size, the receiving threshold.

Five, set the serial port receive buffer event notification. Message response or callback function, choose one.

Six, listen to the serial port.

BOOL CSomeClass::OpenCommPort(LPCSTR lpszCommPort, DWORD dwBitrate)
    BOOL bRet = m_xComm.Open(lpszCommPort);
    if (bRet)
        DCB dcb = {0};
        dcb.BaudRate = dwBitrate;
        dcb.ByteSize = 8;
        dcb.Parity   = NOPARITY;
        dcb.StopBits = ONESTOPBIT;
        m_xComm.SetBufferSize(DEF_COMM_INPUT_BUFFER, MAX_COMM_BUFFER);
        HWND hwnd = GetSafeHwnd();
        MSG msg = {hwnd, WM_SERIAL_EVENT};
        m_xComm.SetWindowMessage(&msg, NULL);

        char szDebug[MAX_PATH];
        sprintf(szDebug, "Open %s failed. err code [%d].\n",
                    lpszCommPort, ::GetLastError());
    return bRet;

Seven, information in response to treatment or callback function.

1 in response to the news


// CSomeClass message handlers

LRESULT CSomeClass::OnCommEvent(WPARAM wParam, LPARAM lParam)
    LRESULT lRet = 0;
    switch (lParam)
        case EV_RXCHAR:
            lRet = OnCommRead(wParam, lParam);
        default: break;
    return lRet;

LRESULT CSomeClass::OnCommRead(WPARAM wParam, LPARAM lParam)
{// may not be more than SetRThreshold, due to a delay message coming

LRESULT lRet = 0; char szDebug[MAX_PATH], szComm[FIX_SERIALCOMM]; if ((HANDLE)wParam == m_xComm.GetSafeHandle()) { int nSize = sizeof(SOME_DATA_STRUCT); DWORD dwInQue, dwOutQue; if (!m_xComm.GetBufferCount(&dwInQue, &dwOutQue)) return lRet; m_xComm.GetCommPort(szComm); while (dwInQue >= nSize) { sprintf(szDebug, "%s dwInQue=%d, dwOutQue=%d\n", szComm, dwInQue, dwOutQue); OutputDebugString(szDebug); SOME_DATA_STRUCT data = {0}; if (m_xComm.Receive((LPBYTE)&data, nSize))


  //do something with data;


if (!m_xComm.GetBufferCount(&dwInQue, &dwOutQue)) return lRet; } lRet = 1; } return lRet; }

2 the callback function

LRESULT CALLBACK CSomeClass::CommProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    LRESULT lRet = 0;
    CSomeClass* pThis = (CSomeClass*)hwnd;
    if (pThis != NULL)
        lRet = pThis->OnCommEvent(wParam, lParam);

    return lRet;

Note that, in the use of callback functions should set the serial receive buffer event notification callback functions, and try to put the this pointer to set the function, convenient to call member functions in the callback function.

MSG msg = {(HWND)this, WM_SERIAL_EVENT};
m_xComm.SetWindowMessage(&msg, CommProc);

Eight, close the serial port.

BOOL CSomeClass::CloseCommPort()
    return m_xComm.Close();

  As for the serial data transmission, is very simple, in the following function call:

m_xComm.Send((LPBYTE)pData, sizeof(SOME_DATA_STRUCT));

   Concise code does not need to explain, you know. To be continued...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Tamara at November 13, 2013 - 10:38 AM