// MBOPC_eVC_DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MBOPC_eVC_Demo.h"
#include "MBOPC_eVC_DemoDlg.h"
#include "WinConAgent.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMBOPC_eVC_DemoDlg dialog

CMBOPC_eVC_DemoDlg::CMBOPC_eVC_DemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMBOPC_eVC_DemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMBOPC_eVC_DemoDlg)
	m_MBGAAddr = 0;
	m_MBGAVal = 0;
	m_MBGDAddr = 0;
	m_MBGDVal = 0;
	m_MBSAAddr = 0;
	m_MBSAVal = 0;
	m_MBSDAddr = 0;
	m_MBSDVal = 0;
	m_UGAAddrSht = 0;
	m_UGAValSht = 0;
	m_UGDAddr = 0;
	m_UGDVal = 0;
	m_USAAddrSht = 0;
	m_USAValSht = 0;
	m_USDAddr = 0;
	m_USDVal = 0;
	m_GAAddr = 0;
	m_GDAddr = 0;
	m_SAAddr = 0;
	m_SDAddr = 0;
	m_SDVal = 0;
	m_SAVal = 0.0f;
	m_GAVal = 0.0f;
	m_GDVal = 0;
	m_UGAAddrFlt = 0;
	m_UGAAddrStr = 0;
	m_UGAValFlt = 0.0f;
	m_USAAddrFlt = 0;
	m_USAAddrStr = 0;
	m_USAValFlt = 0.0f;
	m_USAValStr = _T("");
	m_UGAValStr = _T("");
	m_UGAAddrLon = 0;
	m_UGAValLon = 0;
	m_USAAddrLon = 0;
	m_USAValLon = 0;
	m_GAAddrW = 0;
	m_SAAddrW = 0;
	m_SAValW = 0;
	m_GAValW = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMBOPC_eVC_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMBOPC_eVC_DemoDlg)
	DDX_Text(pDX, IDC_MBGAAddr, m_MBGAAddr);
	DDX_Text(pDX, IDC_MBGAVal, m_MBGAVal);
	DDX_Text(pDX, IDC_MBGDAddr, m_MBGDAddr);
	DDX_Text(pDX, IDC_MBGDVal, m_MBGDVal);
	DDX_Text(pDX, IDC_MBSAAddr, m_MBSAAddr);
	DDX_Text(pDX, IDC_MBSAVal, m_MBSAVal);
	DDX_Text(pDX, IDC_MBSDAddr, m_MBSDAddr);
	DDX_Text(pDX, IDC_MBSDVal, m_MBSDVal);
	DDX_Text(pDX, IDC_UGAAddrSht, m_UGAAddrSht);
	DDX_Text(pDX, IDC_UGAValSht, m_UGAValSht);
	DDX_Text(pDX, IDC_UGDAddr, m_UGDAddr);
	DDX_Text(pDX, IDC_UGDVal, m_UGDVal);
	DDX_Text(pDX, IDC_USAAddrSht, m_USAAddrSht);
	DDX_Text(pDX, IDC_USAValSht, m_USAValSht);
	DDX_Text(pDX, IDC_USDAddr, m_USDAddr);
	DDX_Text(pDX, IDC_USDVal, m_USDVal);
	DDX_Text(pDX, IDC_GAAddr, m_GAAddr);
	DDX_Text(pDX, IDC_GDAddr, m_GDAddr);
	DDX_Text(pDX, IDC_SAAddr, m_SAAddr);
	DDX_Text(pDX, IDC_SDAddr, m_SDAddr);
	DDX_Text(pDX, IDC_SDVal, m_SDVal);
	DDX_Text(pDX, IDC_SAVal, m_SAVal);
	DDX_Text(pDX, IDC_GAVal, m_GAVal);
	DDX_Text(pDX, IDC_GDVal, m_GDVal);
	DDX_Text(pDX, IDC_UGAAddrFlt, m_UGAAddrFlt);
	DDX_Text(pDX, IDC_UGAAddrStr, m_UGAAddrStr);
	DDX_Text(pDX, IDC_UGAValFlt, m_UGAValFlt);
	DDX_Text(pDX, IDC_USAAddrFlt, m_USAAddrFlt);
	DDX_Text(pDX, IDC_USAAddrStr, m_USAAddrStr);
	DDX_Text(pDX, IDC_USAValFlt, m_USAValFlt);
	DDX_Text(pDX, IDC_USAValStr, m_USAValStr);
	DDX_Text(pDX, IDC_UGAValStr, m_UGAValStr);
	DDX_Text(pDX, IDC_UGAAddrLon, m_UGAAddrLon);
	DDX_Text(pDX, IDC_UGAValLon, m_UGAValLon);
	DDX_Text(pDX, IDC_USAAddrLon, m_USAAddrLon);
	DDX_Text(pDX, IDC_USAValLon, m_USAValLon);
	DDX_Text(pDX, IDC_GAAddrW, m_GAAddrW);
	DDX_Text(pDX, IDC_SAAddrW, m_SAAddrW);
	DDX_Text(pDX, IDC_SAValW, m_SAValW);
	DDX_Text(pDX, IDC_GAValW, m_GAValW);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMBOPC_eVC_DemoDlg, CDialog)
	//{{AFX_MSG_MAP(CMBOPC_eVC_DemoDlg)
	ON_BN_CLICKED(IDC_SDBT, OnSdbt)
	ON_BN_CLICKED(IDC_GDBT, OnGdbt)
	ON_BN_CLICKED(IDC_SABT, OnSabt)
	ON_BN_CLICKED(IDC_GABT, OnGabt)
	ON_BN_CLICKED(IDC_MBSDBT, OnMbsdbt)
	ON_BN_CLICKED(IDC_MBGDBT, OnMbgdbt)
	ON_BN_CLICKED(IDC_MBSABT, OnMbsabt)
	ON_BN_CLICKED(IDC_MBGABT, OnMbgabt)
	ON_BN_CLICKED(IDC_USDBT, OnUsdbt)
	ON_BN_CLICKED(IDC_UGDBT, OnUgdbt)
	ON_BN_CLICKED(IDC_USABTSht, OnUSABTSht)
	ON_BN_CLICKED(IDC_UGABTSht, OnUGABTSht)
	ON_BN_CLICKED(IDC_USABTFlt, OnUSABTFlt)
	ON_BN_CLICKED(IDC_UGABTFlt, OnUGABTFlt)
	ON_BN_CLICKED(IDC_USABTStr, OnUSABTStr)
	ON_BN_CLICKED(IDC_UGABTStr, OnUGABTStr)
	ON_BN_CLICKED(IDC_STARTMBOPC, OnStartmbopc)
	ON_BN_CLICKED(IDC_STOPMBOPC, OnStopmbopc)
	ON_BN_CLICKED(IDC_USABTLon, OnUSABTLon)
	ON_BN_CLICKED(IDC_UGABTLon, OnUGABTLon)
	ON_BN_CLICKED(IDC_SABTW, OnSabtw)
	ON_BN_CLICKED(IDC_GABTW, OnGabtw)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMBOPC_eVC_DemoDlg message handlers

BOOL CMBOPC_eVC_DemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	m_MBGDAddr = 500;
    m_MBSDAddr = 500;
	m_MBGAAddr = 500;
    m_MBSAAddr = 500;

	m_UGDAddr = 800;
    m_USDAddr = 800;
	m_UGAAddrSht = 800;
    m_USAAddrSht = 800;

	m_GAAddr = 3;
	m_GAAddrW = 7;
	m_GDAddr = 1;
	m_SAAddr = 3;
	m_SAAddrW = 7;
	m_SDAddr = 1;
	
	m_UGAAddrFlt = 99;
	m_USAAddrFlt = 99;
	m_UGAAddrLon = 88;
	m_USAAddrLon = 88;
	m_UGAAddrStr = 199;
	m_USAAddrStr = 199;

	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CMBOPC_eVC_DemoDlg::OnSdbt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	SetDO(m_SDAddr,m_SDVal);
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnGdbt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	unsigned char iStatus;
	GetDIO(m_GDAddr,&iStatus,0);
	m_GDVal = iStatus;
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnSabt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	//For data type:long
	/*
	long longtest = (long)m_SAVal;
	void *pflong = &longtest;
	float flong = *(float*)pflong;
	SetAO(m_SAAddr,flong);
	*/
	//For data type:float

	SetAO_Float(m_SAAddr,&m_SAVal);

	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnGabt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	//For data type:long
	/*
	float iStatus;
	GetAIO(m_GAAddr,&iStatus,0);
	long longtest;
	void *plong = &iStatus;
	longtest = *(long*)plong;
	m_GAVal = (float)longtest;
	*/
	//For data type:float
	
	float iStatus;
	GetAIO_Float(m_GAAddr,&iStatus,0);
	m_GAVal = iStatus;
	
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnMbsdbt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	MBSetCoil(m_MBSDAddr-1,m_MBSDVal,1);
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnMbgdbt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	unsigned char iStatus;
	MBGetCoil(m_MBGDAddr-1,&iStatus,1);
	m_MBGDVal = iStatus;
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnMbsabt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	MBSetReg(m_MBSAAddr-1,m_MBSAVal,1);
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnMbgabt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	short iStatus;
	MBGetReg(m_MBGAAddr-1,&iStatus,1);
	m_MBGAVal = iStatus;
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUsdbt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	UserSetCoil(m_USDAddr,m_USDVal);
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUgdbt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	unsigned char iStatus;
	UserGetCoil(m_UGDAddr,&iStatus);
	m_UGDVal = iStatus;
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUSABTSht() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
    /*** UserSetReg_Sht Test ***/
//	UserSetReg_Sht(m_USAAddrSht,m_USAValSht);
	/*** End-UserSetReg_Sht Test ***/
    
	/*** UserSetReg Test ***/
	void* pshort = &m_USAValSht;
    UserSetReg(m_USAAddrSht,(long*)pshort,0);
	/*** End-UserSetReg Test ***/
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUGABTSht() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
    /*** UserGetReg_Sht Test ***/
//	short iStatus;
//	UserGetReg_Sht(m_UGAAddrSht,&iStatus);
//	m_UGAValSht = iStatus;
    /*** End-UserGetReg_Sht Test ***/

	/*** UserGetReg Test ***/
	short iStatus=0;
	void* pshort = &iStatus;
	UserGetReg(m_UGAAddrSht,(long*)pshort,0);
	m_UGAValSht = *(short*)pshort;
    /*** End-UserGetReg Test ***/
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUSABTFlt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
    /*** UserSetReg_Flt Test ***/
	UserSetReg_Float(m_USAAddrFlt,&m_USAValFlt);
	/*** End-UserSetReg_Flt Test ***/

    /*** UserSetReg Test ***/
//	void* pfloat = &m_USAValFlt;
//  UserSetReg(m_USAAddrFlt,(long*)pfloat,1);
    /*** End-UserSetReg Test ***/
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUGABTFlt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
    /*** UserGetReg_Flt Test ***/
	float iStatus;
	UserGetReg_Float(m_UGAAddrFlt,&iStatus);
	m_UGAValFlt = iStatus;    
	/*** End-UserGetReg_Flt Test ***/

	/*** UserGetReg Test ***/
//	float iStatus=0.0;
//	void* pfloat = &iStatus;
//	UserGetReg(m_UGAAddrFlt,(long*)pfloat,1);
//	m_UGAValFlt = *(float*)pfloat;
	/*** End-UserGetReg Test ***/
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUSABTStr() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
    char *SetString;
	SetString =  (LPSTR)(LPCTSTR)m_USAValStr; 
	UserSetReg_Str(m_USAAddrStr,SetString);
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUGABTStr() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	char iStatus[256];
	UserGetReg_Str(m_UGAAddrStr,iStatus);
	m_UGAValStr = (LPCTSTR)(LPSTR)iStatus;
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnStartmbopc() 
{
	// TODO: Add your control notification handler code here
	StartQuicker(1);
}


void CMBOPC_eVC_DemoDlg::OnStopmbopc() 
{
	// TODO: Add your control notification handler code here
	StopQuicker();
}

void CMBOPC_eVC_DemoDlg::OnUSABTLon() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	long* pfloat = &m_USAValLon;
    UserSetReg(m_USAAddrLon,pfloat,2);
    /*** End-UserSetReg Test ***/
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnUGABTLon() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	/*** UserGetReg Test ***/
	long iStatus=0;
	long* pfloat = &iStatus;
	UserGetReg(m_UGAAddrLon,pfloat,2);
	m_UGAValLon = *pfloat;
	/*** End-UserGetReg Test ***/
	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnSabtw() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	//For data type:long
	/*
	long longtest = (long)m_SAVal;
	void *pflong = &longtest;
	float flong = *(float*)pflong;
	SetAO(m_SAAddr,flong);
	*/
	//For data type:float
	unsigned short m_SAValWtemp = (unsigned short)m_SAValW;
	SetAO_Word(m_SAAddrW,&m_SAValWtemp);

	UpdateData(FALSE);
}

void CMBOPC_eVC_DemoDlg::OnGabtw() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	//For data type:long
	/*
	float iStatus;
	GetAIO(m_GAAddr,&iStatus,0);
	long longtest;
	void *plong = &iStatus;
	longtest = *(long*)plong;
	m_GAVal = (float)longtest;
	*/
	//For data type:float
	
	unsigned short iStatus;
	GetAIO_Word(m_GAAddrW,&iStatus,0);
	m_GAValW = iStatus;
	
	UpdateData(FALSE);
}
