// RunComm.h: interface for the CRunComm class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_RUNCOMM_H__1C835D25_426C_409C_A4F7_8F267635480E__INCLUDED_)
#define AFX_RUNCOMM_H__1C835D25_426C_409C_A4F7_8F267635480E__INCLUDED_

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

#include "RUN.H"

#ifdef  BUILD_MCCTRDLL
#define     MCCTRDLLCLASS   __declspec(dllexport)
#else
#define     MCCTRDLLCLASS   __declspec(dllimport)
#endif

class MCCTRDLLCLASS CRunComm : public CRun
{
public:
    int     GetPortNo(bool bMaint = FALSE, int nWhichOne = 0);
    CString GetIpAddress(bool bMaint = FALSE, int nWhichOne = 0);
    CRunComm(int nID, CString csName, struct stModule *pstModule, int *iSequenceModuleNo = NULL);
    virtual ~CRunComm();

    enum
    {
        MAINT_OPEN_COM        = 0x1,
        MAINT_CLOSE_COM       = 0x2,
        MAINT_SEND_COM        = 0x4,
        MAINT_CONNECT_PORT    = 0x8,
        MAINT_DISCONNECT_PORT = 0x10,
        MAINT_SEND_PACKET     = 0x20,
    };

protected:
    bool    IsMaintenaceCloseCommPort();
    bool    IsMaintenanceSendComm();
    bool    IsMaintenaceOpenCommPort();
    CString GetMaintMsg();
    bool    IsMaintenanceDisconnectPort();
    bool    IsMaintenanceConnectPort();
    bool    IsMaintenanceSendPacket();
    double  GetStopBits(bool bMaint = FALSE, int nWhichOne = 0);
    int     GetDataBits(bool bMaint = FALSE, int nWhichOne = 0);
    long    GetBaudRate(bool bMaint = FALSE, int nWhichOne = 0);
    CString GetParity(bool bMaint = FALSE, int nWhichOne = 0);
    CString GetCommPort(bool bMaint = FALSE, int nWhichOne = 0);
    bool    GetCommPortSetting(CString csIniFile, CString csSectionName, int nWhichOne = 0);
    bool    GetNetworkPortSetting(CString csIniFile, CString csSectionName, int nWhichOne = 0);
//  send com data to modules in maintenance mode
    virtual void OnMaintenanceSendCom(LPCTSTR lpcComPort, int iBaudRate, int iDataBits,
                                      LPCTSTR lpcParity, float fStopBits, LPCTSTR lpcFlowCtrl, LPCTSTR lpcSendMsg);

//  send open com to modules in maintenance mode
    virtual void OnMaintenanceOpenCom(LPCTSTR lpcComPort, int iBaudRate, int iDataBits,
                                      LPCTSTR lpcParity, float fStopBits, LPCTSTR lpcFlowCtrl);

//  send close com to modules in maintenance mode
    virtual void OnMaintenanceCloseCom(LPCTSTR lpcComPort, int iBaudRate, int iDataBits,
                                       LPCTSTR lpcParity, float fStopBits, LPCTSTR lpcFlowCtrl);

//  send packet to modules in maintenance mode
    virtual void OnMaintenanceSendPacket(LPCTSTR IpAddress, int iPortNo, LPCTSTR lpcSendMsg);

//  send connect port to modules in maintenance mode
    virtual void OnMaintenanceConnectPort(LPCTSTR IpAddress, int iPortNo);

//  send connect port to modules in maintenance mode
    virtual void OnMaintenanceDisconnectPort(LPCTSTR IpAddress, int iPortNo);

//  write acknowledge message to text file
    void WriteAckToTextFile(CString csAckMsg);

    virtual void SendPacket(CString csAckMsg = "");
    virtual void DisconnectPort(CString csAckMsg = "");
    virtual void ConnectPort(CString csAckMsg = "");
    virtual void SendComm(CString csAckMsg = "");
    virtual void CloseCommPort(CString csAckMsg = "");
    virtual void OpenCommPort(CString csAckMsg = "");
    void         InitCommSettings(LPCTSTR lpcComPort, long lBaudRate, int iDataBits, LPCTSTR lpcParity, float fStopBits, LPCTSTR lpcFlowCtrl);

private:
// Maintenance use
    char    m_cComCommand;
    CString m_csMaintSendMsg;

// TCP/IP Maintenance variables
    int     m_iMaintPortNo;
    CString m_csIpAddress;

// Serial Comm Settings for maintenance use
// int   m_nCommPort;
    CString m_csCommPort;
    long    m_lBaudRate;
// int m_nParity;
    CString m_csParity;
    int     m_nDataBits;
// int m_nStopBits;
    double m_dStopBits;
//  int m_nFlowCtrl;

// TCP/IP variables for auto run
    CArray <int, int> m_naPortNoSetting;
    CStringArray      m_csaIpAddressSetting;

// Serial Comm Settings for auto run use
    CStringArray            m_csaCommPortSetting;
    CArray <long, long>     m_laBaudRateSetting;
    CStringArray            m_csaParitySetting;
    CArray <int, int>       m_naDataBitsSetting;
    CArray <double, double> m_daStopBitsSetting;
};

#endif // !defined(AFX_RUNCOMM_H__1C835D25_426C_409C_A4F7_8F267635480E__INCLUDED_)
