// SendReceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SendRece.h"
#include "SendReceDlg.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;
}

/////////////////////////////////////////////////////////////////////////////
// CSendReceDlg dialog

CSendReceDlg::CSendReceDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSendReceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSendReceDlg)
	m_strIPAddress = _T("");
	m_strSend = _T("");
	m_strResponse = _T("");
	m_strModbusResponse = _T("");
	m_strModbusSend = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSendReceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSendReceDlg)
	DDX_Text(pDX, IDC_EDIT_IPADDRESS, m_strIPAddress);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
	DDX_Text(pDX, IDC_EDIT_RESPONSE, m_strResponse);
	DDX_Text(pDX, IDC_EDIT_MODBUSRESPONSE, m_strModbusResponse);
	DDX_Text(pDX, IDC_EDIT_MODBUSSEND, m_strModbusSend);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSendReceDlg, CDialog)
	//{{AFX_MSG_MAP(CSendReceDlg)
	ON_BN_CLICKED(IDC_SENDCOMMAND, OnSendcommand)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendReceDlg message handlers

BOOL CSendReceDlg::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.16.2.200";
	m_strSend="$01F";

	SetDlgItemText(IDC_EDIT_IPADDRESS,m_strIPAddress);
	SetDlgItemText(IDC_EDIT_SEND,m_strSend);

	// 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 CSendReceDlg::OnSendcommand() 
{
	// TODO: Add your control notification handler code here
    CString   strText,strTemp;
	wchar_t   szwCmd[80],szwResponse[80];
	wchar_t   szwModbusCmd[150],szwModbusResponse[150];
	wchar_t   szwDump[80];
	int       iRetVal;


    //--- 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(szwDump,_T("ADAMTCP_Connect() Failure !!!   code=%d"),iRetVal);
        MessageBox(szwDump);
        ADAMTCP_Disconnect();
        return;
    }

	//--- Get the command send to 5000/TCP ---
	GetDlgItemText(IDC_EDIT_SEND,szwCmd,20);
    
    //--- send a command to 5000/TCP and receive response ---        
    iRetVal=ADAMTCP_SendReceive5KTCPCmd(szwIPAddress, szwCmd, szwResponse,
                                              szwModbusCmd, szwModbusResponse);
    if( iRetVal )
    {
        wsprintf(szwDump,_T("ADAMTCP_SendReceive5KTCPCmd() Failure !!!   code=%d"),iRetVal);
        MessageBox(szwDump);
        ADAMTCP_Disconnect();
        return;
    }

    //--- display various message ---
	SetDlgItemText(IDC_EDIT_RESPONSE,szwResponse);
	SetDlgItemText(IDC_EDIT_MODBUSSEND,szwModbusCmd);
	SetDlgItemText(IDC_EDIT_MODBUSRESPONSE,szwModbusResponse);

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

void CSendReceDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
    //--- Finally, calling ADAMTCP_Close() to release all allocated resource  ---
    ADAMTCP_Close();

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