#if !defined(AFX_SCANNER_H__98052617_4EA4_11D7_A89C_005004C029CA__INCLUDED_)
#define AFX_SCANNER_H__98052617_4EA4_11D7_A89C_005004C029CA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afx.h>

// If you will never build this for use in a DLL, you can comment out
// this #if block.
#ifdef  BUILD_COMMDLL
#define     COMMDLL_DLLCLASS __declspec(dllexport)
#else
#define     COMMDLL_DLLCLASS __declspec(dllimport)
#endif

#include "utility.h"

#define COM1                 1
#define COM2                 2
#define COM3                 3
#define COM4                 4
#define COM5                 5
#define COM6                 6
#define COM7                 7
#define COM8                 8

/* Parity Codes */
#define NOPARITY             0
#define ODDPARITY            1
#define EVENPARITY           2
#define MARKPARITY           3
#define SPACEPARITY          4

/* Stop Bit Codes */
#define ONESTOPBIT           0
#define ONE5STOPBITS         1
#define TWOSTOPBITS          2

/* Word Length Codes */
#define WORDLENGTH7          7
#define WORDLENGTH8          8

class CSerialPort;

class COMMDLL_DLLCLASS CScanner
{
public:
    CScanner();
    virtual ~CScanner();

// Operations
public:
    void ResetReceiveBuffer();
    bool SetStopBits(int nStopBits = 0);
    bool SetParity(int nParity = 2);
    bool SetByteSize(int nByteSize = 8);
    bool SetBaudRate(long lBaudRate = 9600);

    CSerialPort *GetCommObject(void);

    BOOL         ClearComm(void);
    virtual void ReceiveComm(const char *cpcBuffer, int nSize) = 0;

    virtual bool Send(const char *cBuffer, int nSize);
    virtual bool Send(const char *s);
    virtual bool Send(const CString &csMsg);

    void         Disconnect();
    virtual bool Connect(int nCommPort = COM1, long lBaudRate = 9600, int nByteSize = 8, int nStopBits = 0, int nParity = 2);
    virtual bool Connect(const CString &csCommPort, long lBaudRate, int nByteSize, double dStopBits, const CString &csParity);

    int GetCommPort() const
    {
        return m_nCommPort;
    }

    long  GetBaudRate() /*const*/
    {
        return m_lBaudRate;
    }

    int GetByteSize() const
    {
        return m_nByteSize;
    }

    int GetParity() const
    {
        return m_nParity;
    }

    int GetStopBits() const
    {
        return m_nStopBits;
    }

    virtual bool IsConnected() const;

    bool m_bTraceData;

protected:
    CString GetReceiveData();
    char m_cReceiveData[4096];
    char m_cMaintReceiveData[4096];

    BOOL m_bDataComplete;
    int  m_nBufferPtr;

// Attributes
private:

    CSerialPort *m_pComm;
    CEvent       m_threadStarted;
    int          m_nCommPort;
    long         m_lBaudRate;
    int          m_nByteSize;
    int          m_nParity;
    int          m_nStopBits;

    bool m_bConnected;

    DWORD               Run();
    static DWORD WINAPI CommThread(void *pvData);
};

#endif // !defined(AFX_SCANNER_H__98052617_4EA4_11D7_A89C_005004C029CA__INCLUDED_)
