// CoilWrDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CoilWr.h"
#include "CoilWrDlg.h"

#include "..\..\include\AdamTcpCE.h"

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

#define DEFAULT_PORT 502  // Port for Modbus/TCP
#define  DeviceID       0x01  // 0x01 indicate the 5000/TCP
//-------- default timeout ------
int     iConnectionTimeout=2000;
int     iSendTimeout=2000;
int     iReceiveTimeout=2000;

wchar_t	szwIPAddress[20];

int MbsToWcs (char* mbs, wchar_t* wcs);
int WcsToMbs (wchar_t* wcs, char* mbs);

int MbsToWcs (char* mbs, wchar_t* wcs)
{
	wchar_t g_wcConverter[256];
	size_t len = strlen (mbs);
	memset (g_wcConverter, 0, sizeof(g_wcConverter));
	if (len > 127) return NULL;
	mbstowcs(g_wcConverter, mbs, len);
	wcscpy(wcs, g_wcConverter);
	return len;
}

int WcsToMbs (wchar_t* wcs, char* mbs)
{
	char	g_mbConvert[256];
	size_t len = wcslen (wcs);
	if (len >= sizeof(g_mbConvert)) len = sizeof(g_mbConvert) - 1;
	wcstombs(g_mbConvert, wcs, len);
	strcpy(mbs, g_mbConvert);
	return len;
}

/////////////////////////////////////////////////////////////////////////////
// CCoilWrDlg dialog

CCoilWrDlg::CCoilWrDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCoilWrDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCoilWrDlg)
	m_strDeviceID = _T("");
	m_strDLLVersion = _T("");
	m_strIPAddress = _T("");
	m_strNoOfWrite = _T("");
	m_strStartAddress = _T("");
	m_nStart0 = -1;
	m_nStart1 = -1;
	m_nStart2 = -1;
	m_nStart3 = -1;
	m_nStart4 = -1;
	m_nStart5 = -1;
	m_nStart6 = -1;
	m_nStart7 = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCoilWrDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCoilWrDlg)
	DDX_Text(pDX, IDC_EDIT_DEVICEID, m_strDeviceID);
	DDX_Text(pDX, IDC_EDIT_DLLVERSION, m_strDLLVersion);
	DDX_Text(pDX, IDC_EDIT_IPADDRESS, m_strIPAddress);
	DDX_Text(pDX, IDC_EDIT_NOOFWRITE, m_strNoOfWrite);
	DDX_Text(pDX, IDC_EDIT_STARTADDRESS, m_strStartAddress);
	DDX_Radio(pDX, IDC_RADIO1, m_nStart0);
	DDX_Radio(pDX, IDC_RADIO3, m_nStart1);
	DDX_Radio(pDX, IDC_RADIO5, m_nStart2);
	DDX_Radio(pDX, IDC_RADIO7, m_nStart3);
	DDX_Radio(pDX, IDC_RADIO9, m_nStart4);
	DDX_Radio(pDX, IDC_RADIO11, m_nStart5);
	DDX_Radio(pDX, IDC_RADIO13, m_nStart6);
	DDX_Radio(pDX, IDC_RADIO15, m_nStart7);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCoilWrDlg, CDialog)
	//{{AFX_MSG_MAP(CCoilWrDlg)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_WRITECOIL, OnWritecoil)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCoilWrDlg message handlers

BOOL CCoilWrDlg::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_strIPAddress="172.18.1.193";
	m_strDeviceID="1";
	m_strStartAddress="1";
	m_strNoOfWrite="8";

	SetDlgItemText(IDC_EDIT_IPADDRESS,m_strIPAddress);
	SetDlgItemText(IDC_EDIT_DEVICEID,m_strDeviceID);
	SetDlgItemText(IDC_EDIT_STARTADDRESS,m_strStartAddress);
	SetDlgItemText(IDC_EDIT_NOOFWRITE,m_strNoOfWrite);

	CButton*  pRadioButton;
	int       nID,i;

    nID=IDC_RADIO1;
	for(i=0; i<8; i++)
	{

	    pRadioButton=(CButton *)GetDlgItem(nID+2*i);
		pRadioButton->SetCheck(BST_CHECKED);
	}

	// get the DLL version 
	char     szCh[20];
	//wchar_t  szwCh[20];
    int    iVersion;    
 
	iVersion=ADAMTCP_GetDLLVersion();
	_ltoa( iVersion, szCh, 16 );

	size_t len = strlen (szCh);
	wchar_t  wcConverter[20];

	memset (wcConverter, 0, sizeof(wcConverter));

	mbstowcs(wcConverter, szCh, len);
	//wsprintf(szwCh,"%s",szCh);
	SetDlgItemText(IDC_EDIT_DLLVER, wcConverter);

    //--- To initialize DLL to work ---
    int   iRetVal;

    iRetVal=ADAMTCP_Open();
    if( iRetVal!=0 )
    {
        MessageBox(_T("ADAMTCP_Open() Failure !!!"));
    }	
	
	return TRUE;  // return TRUE  unless you set the focus to a control	
}



void CCoilWrDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
    ADAMTCP_Close();	
	
	// TODO: Add your control notification handler code here
	CDialog::OnOK();	
}

void CCoilWrDlg::OnWritecoil() 
{
	// TODO: Add your control notification handler code here
    TCHAR     strTemp[20];
	TCHAR     szwIPAddress[20];
	int       i,nID,iRetVal;
	WORD      wDeviceID;
    WORD      wStartAddress;
    WORD      wCount;
    WORD      wData[128];  
	int       CoilValue[16];
	CButton*  pRadioButton;
	TCHAR     szDump[80];

    //--- Firstly, initial DLL to working ---
    nID=IDC_RADIO1;
	for(i=0; i<8; i++)
	{
	    pRadioButton=(CButton *)GetDlgItem(nID+2*i);
		if( pRadioButton->GetCheck()==BST_CHECKED )
			CoilValue[i]=0;
		else
			CoilValue[i]=1;
	}

	// How many coils to write
	GetDlgItemText(IDC_EDIT_NOOFWRITE,strTemp, 80*sizeof(TCHAR));
	wCount=(WORD)_wtoi(strTemp);
	if( wCount<0 || wCount>16 )
	{
		MessageBox(_T("The # of Coil must between 1 to 16 !"));
		return;
	}

	// the Device ID
	GetDlgItemText(IDC_EDIT_DEVICEID,strTemp, 80*sizeof(TCHAR));
	wDeviceID = (WORD)_wtoi(strTemp);

	// the start address to write
	GetDlgItemText(IDC_EDIT_STARTADDRESS,strTemp, 80*sizeof(TCHAR));
	wStartAddress = (WORD)_wtoi(strTemp);

    // get the 5000/TCP's IP Address
	GetDlgItemText(IDC_EDIT_IPADDRESS, szwIPAddress, 20);
    
	//--- try to create a connection to 5000/TCP ---
    iRetVal=ADAMTCP_Connect(szwIPAddress,DEFAULT_PORT,
				  iConnectionTimeout, iSendTimeout, iReceiveTimeout);

    if( iRetVal<0 )
    {
        wsprintf(szDump, _T("ADAMTCP_Connect() Failure !!!   code=%d\n"),iRetVal);
        MessageBox(szDump);
        ADAMTCP_Disconnect();
        return;
    }

    //--- prepare data to write to coil ---
	for(i=0; i<wCount; i++)
		wData[i]=CoilValue[i];

    //--- writing the coils ---
	iRetVal=ADAMTCP_WriteCoil(szwIPAddress, wDeviceID, wStartAddress, wCount, wData);
    if( iRetVal )
    {
        wsprintf(szDump,_T("ADAMTCP_WriteCoil() Failure !!!   code=%d\n"),iRetVal);
        MessageBox(szDump);
        ADAMTCP_Disconnect();
        return;
    }

    //--- lastly, disconnet connection to 5000/TCP ---
    ADAMTCP_Disconnect();
	
}
