// CoilRdDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CoilRd.h"
#include "CoilRdDlg.h"

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

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

/////////////////////////////////////////////////////////////////////////////
// CCoilRdDlg dialog

CCoilRdDlg::CCoilRdDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCoilRdDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCoilRdDlg)
	m_strDeviceID = _T("");
	m_strNoOfRead = _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 CCoilRdDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCoilRdDlg)
	DDX_Text(pDX, IDC_EDIT_DEVICEID, m_strDeviceID);
	DDX_Text(pDX, IDC_EDIT_NO, m_strNoOfRead);
	DDX_Text(pDX, IDC_EDIT_STARTADDRESS, m_strStartAddress);
	DDX_CBString(pDX, IDC_COMPORT, m_strCOMPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCoilRdDlg, CDialog)
	//{{AFX_MSG_MAP(CCoilRdDlg)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_READCOIL, OnReadcoil)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCoilRdDlg message handlers

BOOL CCoilRdDlg::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_strNoOfRead="8";
	
	SetDlgItemText(IDC_COMPORT,m_strCOMPort);
	SetDlgItemText(IDC_EDIT_DEVICEID,m_strDeviceID);
	SetDlgItemText(IDC_EDIT_STARTADDRESS,m_strStartAddress);
	SetDlgItemText(IDC_EDIT_NO,m_strNoOfRead);

	// 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);  
 
	//--- reset the content in CListBox ---
	((CListBox*)GetDlgItem(IDC_LIST_COILVALUE))->ResetContent();
	return TRUE;  // return TRUE  unless you set the focus to a control
}



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

void CCoilRdDlg::OnReadcoil() 
{
	// TODO: Add your control notification handler code here
    CString   strText,strTemp;
	int       i,j,iRetVal;
	WORD      wDeviceID;
    WORD      wStartAddress;
    WORD      wCount;
    WORD      wData[128];
	TCHAR	  szwTemp[80];
	int       iComPort,iBaudRate,iTimeout;
    long      lPortHandle;
  
    iBaudRate=9600;
	iTimeout=1000;   // the timeout interval:1000 mSec

	// Which COM Port 
	GetDlgItemText(IDC_COMPORT, szwTemp, 80*sizeof(TCHAR));
    iComPort=_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 coils to read
	GetDlgItemText(IDC_EDIT_NO,szwTemp, 80*sizeof(TCHAR));
	wCount = (unsigned short)_wtoi(szwTemp);
	if( wCount<0 || wCount>128 )
	{
		MessageBox( _T("The # of Coil must between 1 to 128 !") );
		return;
	}

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

	// the start address to read
	GetDlgItemText(IDC_EDIT_STARTADDRESS,szwTemp, 80*sizeof(TCHAR));
	wStartAddress = (unsigned short)_wtoi(szwTemp);

    //--- reading the coils ---
    iRetVal=ADAMRTU_ReadCoil(lPortHandle, wDeviceID, wStartAddress, wCount, wData, iTimeout);
    if( iRetVal )
    {
		ADAMRTU_Close(lPortHandle);
		MessageBox( _T("ADAMRTU_ReadCoil() Failure !!!") );
        return;
    }

	((CListBox*)GetDlgItem(IDC_LIST_COILVALUE))->ResetContent();
	for(i=0,j=wStartAddress; i<wCount; i++,j++)
	{
		strTemp.Format( _T("Address:%d   Value:%d"),j,wData[i]);
   	    ((CListBox*)GetDlgItem(IDC_LIST_COILVALUE))->InsertString( -1, strTemp );
	}

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