// SendRece4KDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SendRece4K.h"
#include "SendRece4KDlg.h"

#include "..\..\include\AdamSerCE.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSendRece4KDlg dialog

CSendRece4KDlg::CSendRece4KDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSendRece4KDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSendRece4KDlg)
	m_strSend = _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 CSendRece4KDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSendRece4KDlg)
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
	DDX_CBString(pDX, IDC_COMPORT, m_strCOMPort);
	//}}AFX_DATA_MAP
}

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

/////////////////////////////////////////////////////////////////////////////
// CSendRece4KDlg message handlers

BOOL CSendRece4KDlg::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";
	SetDlgItemText(IDC_COMPORT,m_strCOMPort);
	m_strSend="$01F";
	SetDlgItemText(IDC_EDIT_SEND,m_strSend);

	// get the DLL version 
	char     szCh[20];
    int     iVersion;    
 
	iVersion=ADAMSER_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 CSendRece4KDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();	
}

void CSendRece4KDlg::OnSendcommand() 
{
	// TODO: Add your control notification handler code here
	int            iRetVal;
	TCHAR          szwSend[80], szwReceive[80], szwTemp[80];
    CString        strText; 
    int            iComPort,iBaudRate,iTimeout;
    long           lPortHandle;

    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 = ADAMSER_Open(iComPort);
    if (lPortHandle == (long)INVALID_HANDLE_VALUE)
    {
        MessageBox(_T("ADAM_Open() Failure !!!"));
        return;            
    }        
	
    iRetVal=ADAMSER_SetCommState(lPortHandle, iBaudRate, 
                                 ADAMSER_PARITY_NO, DATEBITS_8, STOPBIT_1);
	if( iRetVal!=0 )
	{
        MessageBox(_T("ADAM_SetCoommState() Failure !!!"));
        ADAMSER_Close(lPortHandle);		
		return;
	}

    // get the command to send
	GetDlgItemText(IDC_EDIT_SEND, szwSend, 80);

	// sending command to 5000/TCP and receiving response from 5000/TCP
	iRetVal=ADAMSER_AdamCommand(lPortHandle, szwSend, szwReceive, iTimeout);
    if( iRetVal<0 )
    {
        MessageBox( _T("ADAMSER_AdamCommand() Error !") );
        ADAMSER_Close(lPortHandle);		
		return;
    }

	((CListBox*)GetDlgItem(IDC_LIST1))->InsertString( -1, szwSend );
	((CListBox*)GetDlgItem(IDC_LIST1))->InsertString( -1, szwReceive );

	//--- releasing UDP resource ---
    ADAMSER_Close(lPortHandle);		
}
