// RegWriteDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RegWrite.h"
#include "RegWriteDlg.h"

#include "..\..\include\AdamRtuCE.h"

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

/////////////////////////////////////////////////////////////////////////////
// CRegWriteDlg dialog

CRegWriteDlg::CRegWriteDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRegWriteDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRegWriteDlg)
	m_strData0 = _T("");
	m_strData1 = _T("");
	m_strData2 = _T("");
	m_strData3 = _T("");
	m_strData4 = _T("");
	m_strData5 = _T("");
	m_strData6 = _T("");
	m_strData7 = _T("");
	m_strDeviceID = _T("");
	m_strDLLVersion = _T("");
	m_strNoOfWrite = _T("");
	m_strStartAddress = _T("");
	m_strCOMPort = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRegWriteDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRegWriteDlg)
	DDX_Text(pDX, IDC_EDIT_DATA0, m_strData0);
	DDX_Text(pDX, IDC_EDIT_DATA1, m_strData1);
	DDX_Text(pDX, IDC_EDIT_DATA2, m_strData2);
	DDX_Text(pDX, IDC_EDIT_DATA3, m_strData3);
	DDX_Text(pDX, IDC_EDIT_DATA4, m_strData4);
	DDX_Text(pDX, IDC_EDIT_DATA5, m_strData5);
	DDX_Text(pDX, IDC_EDIT_DATA6, m_strData6);
	DDX_Text(pDX, IDC_EDIT_DATA7, m_strData7);
	DDX_Text(pDX, IDC_EDIT_DEVICEID, m_strDeviceID);
	DDX_Text(pDX, IDC_EDIT_DLLVER, m_strDLLVersion);
	DDX_Text(pDX, IDC_EDIT_NOOFWRITE, m_strNoOfWrite);
	DDX_Text(pDX, IDC_EDIT_STARTADDRESS, m_strStartAddress);
	DDX_CBString(pDX, IDC_COMPORT, m_strCOMPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRegWriteDlg, CDialog)
	//{{AFX_MSG_MAP(CRegWriteDlg)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_WRITEREGISTER, OnWriteregister)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRegWriteDlg message handlers

BOOL CRegWriteDlg::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_strCOMPort="1";
	m_strDeviceID="1";
	m_strStartAddress="1";
	m_strNoOfWrite="8";

	SetDlgItemText(IDC_COMPORT,m_strCOMPort);
	SetDlgItemText(IDC_EDIT_DEVICEID,m_strDeviceID);
	SetDlgItemText(IDC_EDIT_STARTADDRESS,m_strStartAddress);
	SetDlgItemText(IDC_EDIT_NOOFWRITE,m_strNoOfWrite);

	int nID=IDC_EDIT_DATA0;
	for(int i=0; i<8; i++)
	{
		SetDlgItemText( nID+i,_T("123") );
    }
	
	// get the DLL version 
	char     szCh[20];
    int     iVersion;    
 
	iVersion=ADAMRTU_GetDLLVersion();
	_ltoa( iVersion, szCh, 16 );

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

	memset (wcConverter, 0, sizeof(wcConverter));
	mbstowcs(wcConverter, szCh, len);
	SetDlgItemText(IDC_EDIT_DLLVER, wcConverter);  

	return TRUE;  // return TRUE  unless you set the focus to a control
}



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

void CRegWriteDlg::OnWriteregister() 
{
	// TODO: Add your control notification handler code here
    CString   strText,strTemp;
	char      *stopstring;
	int       i,iRetVal,nID;
    WORD      wDeviceID;
	WORD      wStartAddress;
    WORD      wCount;
    WORD      wData[64];  
	TCHAR	  szwTemp[80];
	char      szTemp[80];
	int       iComPort,iBaudRate,iTimeout;
    long      lPortHandle;

    //iComPort=2;
    iBaudRate=9600;
	iTimeout=1000;   // timeout interval: 1000 mSec

	// which COM port to use 
	GetDlgItemText(IDC_COMPORT,szwTemp, 80*sizeof(TCHAR));
	iComPort = (unsigned short)_wtoi(szwTemp);

    lPortHandle = ADAMRTU_Open(iComPort);
    if (lPortHandle == (long)INVALID_HANDLE_VALUE)
    {
        MessageBox(_T("ADAM_Open() Failure !!!"));
        return;            
    }      

    iRetVal=ADAMRTU_SetCommState(lPortHandle, iBaudRate, 
                                 ADAMRTU_PARITY_NO, DATEBITS_8, STOPBIT_1);
	if( iRetVal!=0 )
	{
        MessageBox(_T("ADAM_SetCoommState() Failure !!!"));
        ADAMRTU_Close(lPortHandle);		
		return;
	}

	// How many registers to write
	GetDlgItemText(IDC_EDIT_NOOFWRITE,szwTemp, 80*sizeof(TCHAR));	
	wCount = (unsigned short)_wtoi(szwTemp);
	if( wCount<0 || wCount>8 )
	{
		MessageBox( _T("The # of register must between 1 to 8 !") );
		return;
	}

	// the device ID
	GetDlgItemText(IDC_EDIT_DEVICEID,szwTemp, 80*sizeof(TCHAR));
	wDeviceID = (unsigned short)_wtoi(szwTemp);

	// the start address to read
	//--- Notice: the address start at 1 instead of 40001
	GetDlgItemText(IDC_EDIT_STARTADDRESS,szwTemp, 80*sizeof(TCHAR));	
	wStartAddress = (unsigned short)_wtoi(szwTemp);
   
    //--- prepare the data for writing to 5000/TCP ---
	nID=IDC_EDIT_DATA0;
	for(i=0; i<8; i++)
	{
		GetDlgItemText(nID+i,szwTemp, 80*sizeof(TCHAR));

        WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
	        szwTemp, -1, szTemp, 80, NULL, NULL);

		wData[i]=(WORD)strtol(szTemp, &stopstring, 16);
	}
    
	//--- writng the registers ---
    iRetVal=ADAMRTU_WriteHoldingRegister(lPortHandle, wDeviceID, wStartAddress, wCount, wData, iTimeout);

	iRetVal=ADAMRTU_Close(lPortHandle);
    if( iRetVal )
    {
	   MessageBox( _T("Failure to ADAMRTU_Close()") );
	   return;
    }
}
