본문 바로가기

Windows/MFC

CSerial

나는 -_- 순차적으로 Serial Data를 받아야 하기 땜시로 -_-;
조금 개조를 해버렸3...
GPL 문서에 따라서 +_+ 수정한 부분 공개 훗.

[code]

//    Listener.cpp - Sample application for CSerial
//
//    Copyright (C) 1999-2003 Ramon de Klein (Ramon.de.Klein@ict.nl)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#define STRICT
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "Serial.h"

enum { EOF_Char = 27 };

int ShowError (LONG lError, LPCTSTR lptszMessage)
{
    // Generate a message text
    TCHAR tszMessage[256];
    wsprintf(tszMessage,_T("%s\n(error code %d)"), lptszMessage, lError);

    // Display message-box and return with an error-code
    ::MessageBox(0,tszMessage,_T("Listener"), MB_ICONSTOP|MB_OK);
    return 1;
}

char szBuffer[401];


int ReadingSerial(CSerial* serial)
{
    LONG    lLastError = ERROR_SUCCESS;

    // Keep reading data, until an EOF (CTRL-Z) has been received
    bool fContinue = true;
    do
    {
        // Wait for an event
        lLastError = (*serial).WaitEvent();
        if (lLastError != ERROR_SUCCESS)
            return ::ShowError((*serial).GetLastError(), _T("Unable to wait for a COM-port event."));

        // Save event
        const CSerial::EEvent eEvent = (*serial).GetEventType();

        // Handle break event
        if (eEvent & CSerial::EEventBreak)
        {
            printf("\n### BREAK received ###\n");
        }

        // Handle CTS event
        if (eEvent & CSerial::EEventCTS)
        {
            printf("\n### Clear to send %s ###\n", (*serial).GetCTS()?"on":"off");
        }

        // Handle DSR event
        if (eEvent & CSerial::EEventDSR)
        {
            printf("\n### Data set ready %s ###\n", (*serial).GetDSR()?"on":"off");
        }

        // Handle error event
        if (eEvent & CSerial::EEventError)
        {
            printf("\n### ERROR: ");
            switch ((*serial).GetError())
            {
            case CSerial::EErrorBreak:        printf("Break condition");            break;
            case CSerial::EErrorFrame:        printf("Framing error");            break;
            case CSerial::EErrorIOE:        printf("IO device error");            break;
            case CSerial::EErrorMode:        printf("Unsupported mode");            break;
            case CSerial::EErrorOverrun:    printf("Buffer overrun");            break;
            case CSerial::EErrorRxOver:        printf("Input buffer overflow");    break;
            case CSerial::EErrorParity:        printf("Input parity error");        break;
            case CSerial::EErrorTxFull:        printf("Output buffer full");        break;
            default:                        printf("Unknown");                    break;
            }
            printf(" ###\n");
        }

        // Handle ring event
        if (eEvent & CSerial::EEventRing)
        {
            printf("\n### RING ###\n");
        }

        // Handle RLSD/CD event
        if (eEvent & CSerial::EEventRLSD)
        {
            printf("\n### RLSD/CD %s ###\n", (*serial).GetRLSD()?"on":"off");
        }

        // Handle data receive event
        if (eEvent & CSerial::EEventRecv)
        {
            // Read data, until there is nothing left
            DWORD dwBytesRead = 0;
            do
            {
                // Read data from the COM-port
                lLastError = (*serial).Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead);
                if (lLastError != ERROR_SUCCESS)
                    return ::ShowError((*serial).GetLastError(), _T("Unable to read from COM-port."));

                if (dwBytesRead > 0)
                {
                    // Finalize the data, so it is a valid string
                    szBuffer[dwBytesRead] = '\0';

                    // Display the data
                    //printf(":%s", szBuffer);

                    // Check if EOF (CTRL+'[') has been specified
                    //if (strchr(szBuffer,EOF_Char))

                    printf("====================================\n");
                    fContinue = false;
                }
            }
            while (dwBytesRead == sizeof(szBuffer)-1);
        }
    }
    while (fContinue);

    return true;
}

int __cdecl _tmain (int /*argc*/, char** /*argv*/)
{
    CSerial serial;
    LONG    lLastError = ERROR_SUCCESS;

    // Attempt to open the serial port (COM1)
    lLastError = serial.Open(_T("\\\\.\\COM20"),0,0,false);
    if (lLastError != ERROR_SUCCESS)
        return ::ShowError(serial.GetLastError(), _T("Unable to open COM-port"));

    // Setup the serial port (9600,8N1, which is the default setting)
    lLastError = serial.Setup(CSerial::EBaud115200,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
    if (lLastError != ERROR_SUCCESS)
        return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port setting"));
    
    if (serial.SetupHandshaking(CSerial::EHandshakeHardware))
        return false;

    // Register only for the receive event
    lLastError = serial.SetMask(CSerial::EEventBreak |
                                CSerial::EEventCTS |
                                CSerial::EEventDSR |
                                CSerial::EEventError |
                                CSerial::EEventRing |
                                CSerial::EEventRLSD |
                                CSerial::EEventRecv);
    if (lLastError != ERROR_SUCCESS)
        return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port event mask"));

    // Use 'non-blocking' reads, because we don't know how many bytes
    // will be received. This is normally the most convenient mode
    // (and also the default mode for reading data).
    lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);
    if (lLastError != ERROR_SUCCESS)
        return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port read timeout."));

    serial.Write("at\r", 3);    
    Sleep(100);

    if(ReadingSerial(&serial))
    {
        printf("0serial : %s\n", szBuffer);
        ZeroMemory(szBuffer, sizeof(szBuffer));
    }
    

    serial.Write("ati\r", 4);
    Sleep(100);

    if(ReadingSerial(&serial))
    {
        printf("1serial : %s\n", szBuffer);
        ZeroMemory(szBuffer, sizeof(szBuffer));
    }
    

    serial.Write("AT+CMGF=1\r", 10);
    Sleep(100);
    if(ReadingSerial(&serial))
    {
        printf("2serial : %s\n", szBuffer);
        ZeroMemory(szBuffer, sizeof(szBuffer));
    }
    

    serial.Write("AT+CMGR=1\r", 10);
    Sleep(100);
    if(ReadingSerial(&serial))
    {
        printf("3serial : %s\n", szBuffer);
        ZeroMemory(szBuffer, sizeof(szBuffer));
    }
    
    

    serial.Write("AT+CMGR=2\r", 10);
    Sleep(100);
    if(ReadingSerial(&serial))
    {
        printf("4serial : %s\n", szBuffer);
        ZeroMemory(szBuffer, sizeof(szBuffer));
    }
    
    


    // Close the port again
    serial.Close();
    return 0;
}

[/code]

'Windows > MFC' 카테고리의 다른 글

Kill Thread  (0) 2013.10.02
CTimer  (0) 2013.10.02
CSerial  (0) 2013.10.02
CSplitterWnd - Multiple FormView  (0) 2013.10.02
VS BUG FIX  (0) 2013.10.02