
// MasterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Master.h"
#include "MasterDlg.h"
#include "afxdialogex.h"

// Including Files ////////////////////////////////////////
#include "Gpibac.h"
#include "SubFunc.h"
///////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/////////////////////////////////////////////////////////////////////////////
long	Ret, Count;					// Return code,Count
DWORD	Myadr, Yradr, Master;		// MyAddress,Address of device,(Master(0) or Slave(1))
DWORD	Delim, Eoi;					// Delimiter
DWORD	Srlen;						// Length of buffers
DWORD	Cmd[32];					// Command of array
DWORD	P_stb, P_eoi;				// StatusByte,EOI
DWORD	Pstb[32];					// StatusByte array
BYTE	srBuffer[10000];            // buffer of send data or receive data
TCHAR	szBuffer[10000];
CString	csBuf;						// string of csBuf
/////////////////////////////////////////////////////////////////////////////

// CMasterDlg dialog



CMasterDlg::CMasterDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMasterDlg::IDD, pParent)
{
	m_YrAdrBox = _T("");
	m_TextRet = _T("");
	m_ReceiveData = _T("");
	m_SendData = _T("");
	m_MyAddress = 0;

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMasterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_DELIMBOX, m_DelimBox);
	DDX_Control(pDX, IDC_YRADRBOX, m_DelYrAdr);
	DDX_CBString(pDX, IDC_YRADRBOX, m_YrAdrBox);
	DDX_Text(pDX, IDC_TEXT_RET, m_TextRet);
	DDX_Text(pDX, IDC_RECEIVEDATA, m_ReceiveData);
	DDX_Text(pDX, IDC_SENDDATA, m_SendData);
	DDX_Text(pDX, IDC_MYADDRESS, m_MyAddress);
}

BEGIN_MESSAGE_MAP(CMasterDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INIT, &CMasterDlg::OnBnClickedInit)
	ON_BN_CLICKED(IDC_POLLING, &CMasterDlg::OnBnClickedPolling)
	ON_BN_CLICKED(IDC_SEND, &CMasterDlg::OnBnClickedSend)
	ON_BN_CLICKED(IDC_RECEIVE, &CMasterDlg::OnBnClickedReceive)
	ON_BN_CLICKED(IDC_GET, &CMasterDlg::OnBnClickedGet)
	ON_BN_CLICKED(IDC_EXT, &CMasterDlg::OnBnClickedExt)
	ON_BN_CLICKED(IDC_DEL_RET, &CMasterDlg::OnBnClickedDelRet)
	ON_BN_CLICKED(IDC_DEL_DATA, &CMasterDlg::OnBnClickedDelData)
	ON_CBN_SELCHANGE(IDC_YRADRBOX, &CMasterDlg::OnCbnSelchangeYradrbox)
	ON_CBN_SELCHANGE(IDC_DELIMBOX, &CMasterDlg::OnCbnSelchangeDelimbox)
END_MESSAGE_MAP()


// CMasterDlg message handlers

BOOL CMasterDlg::OnInitDialog()
{
	CDialogEx::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

	// ****************************************************** Setting ***
	m_SendData = "_ABCDEFGHIJK_ Test Data / 34 Bytes";
	UpdateData(FALSE);
	// ******************************************************************
	m_DelYrAdr.SetCurSel(1);
	Yradr = GetDlgItemInt(IDC_YRADRBOX);
	m_DelimBox.SetCurSel(1);

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

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMasterDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMasterDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

/////////////////////////////////////////////////////////////////////
// This Sample Code is here.
/////////////////////////////////////////////////////////////////////

//////////////////////////////////////////// Initialize ///
void CMasterDlg::OnBnClickedInit()
{
	TCHAR DelMyadr[8];
	GpibInit(&csBuf);								// [GpibInit] Function
	GpBoardsts(0x08, &Myadr);						// Get MyAddress
	GpBoardsts(0x0a, &Master);						// Get Am I Master(=0) or Slave(=1)?

	if (Master == 1){								// A case of Slave
		m_TextRet = _T("This Partner is not Master.");
		UpdateData(FALSE);
		return;
	}
	else{
		GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
		GetDlgItem(IDC_RECEIVE)->EnableWindow(TRUE);
		GetDlgItem(IDC_POLLING)->EnableWindow(TRUE);
		GetDlgItem(IDC_GET)->EnableWindow(TRUE);

		wsprintf(DelMyadr, _T("%d"), Myadr);		// Show MyAddress
		m_MyAddress = Myadr;
		Ret = m_DelYrAdr.FindStringExact(-1, DelMyadr);
		if (Ret >= 0){								// Seaching MyAddress
			m_DelYrAdr.DeleteString(Ret);			// Delete MyAddress 
			Yradr = GetDlgItemInt(IDC_YRADRBOX);
			if (Myadr == Yradr){
				m_DelYrAdr.SetCurSel(0);
			}
		}
		m_TextRet = csBuf;							// Show csBuf
		UpdateData(FALSE);
	}
	Yradr = GetDlgItemInt(IDC_YRADRBOX);

	OnCbnSelchangeDelimbox();
}

//////////////////////////////////////////////// Polling ///
void CMasterDlg::OnBnClickedPolling()
{
	UpdateData(TRUE);

	m_TextRet = _T("Polling...");
	UpdateData(FALSE);
	UpdateWindow();								  	// Redraw
	for (Count = 0; Count < 16; Count++){
		Pstb[Count] = 0x00;						  	// Parameter Initialize
	}
	Cmd[0] = 1;									  	// Number of commands 
	Cmd[1] = Yradr;								  	// Address of device
	Ret = GpPoll(Cmd, Pstb);					  	// Start Polling
	if ((Ret & 0xFF) == 0)						  	// [SRQ] Found
		csBuf.Format(_T("Found [SRQ] Talker Address =[ %x ]:StatusByte =[ %x ]H"), Cmd[Pstb[0]], Pstb[Pstb[0]]);
	else if ((Ret & 0xFF) == 1)						// [SRQ] [EOI] Found
		csBuf.Format(_T("Found [SRQ] Talker Address =[ %x ]:StatusByte =[ %x ]H"), Cmd[Pstb[0]], Pstb[Pstb[0]]);
	else if ((Ret & 0xFF) == 128)				    // [PM]Series is return this code
		csBuf.Format(_T("Found Only [SRQ]"));
	else{										    // Except for 0,1,128
		CheckRet(_T("GpPoll"), Ret, &csBuf);
	}
	m_TextRet = csBuf;								// Show Error or Return	code
	UpdateData(FALSE);
}

////////////////////////////////////////////////// Send ///
void CMasterDlg::OnBnClickedSend()
{
	UpdateData(TRUE);

	GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
	GetDlgItem(IDC_RECEIVE)->EnableWindow(FALSE);
	GetDlgItem(IDC_INIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_POLLING)->EnableWindow(FALSE);
	GetDlgItem(IDC_GET)->EnableWindow(FALSE);

	lstrcpy(szBuffer, m_SendData);					// Make a copy of m_SendData
	Srlen = lstrlen(szBuffer);						// measure of srBuffer
	sprintf_s((char*)srBuffer, Srlen + 1, "%S", szBuffer);
	if (Srlen != 0x0){								// A case of Srlen is 0
		m_TextRet = _T("Data sending...");
		UpdateData(FALSE);
		UpdateWindow();								// Redraw

		Cmd[0] = 2;									// Talker + Listener
		Cmd[1] = Myadr;								// MyAddress
		Cmd[2] = Yradr;								// Address of device
		Ret = GpTalk(Cmd, Srlen, srBuffer);			// Send Data
		CheckRet(_T("GpTalk"), (Ret & 0xFF), &csBuf);// Check of [GpTalk]
		m_TextRet = csBuf;
	}
	else
		m_TextRet = _T("There is no data.");

	GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
	GetDlgItem(IDC_RECEIVE)->EnableWindow(TRUE);
	GetDlgItem(IDC_INIT)->EnableWindow(TRUE);
	GetDlgItem(IDC_POLLING)->EnableWindow(TRUE);
	GetDlgItem(IDC_GET)->EnableWindow(TRUE);
	UpdateData(FALSE);
}

/////////////////////////////////////////////// Receive ///
void CMasterDlg::OnBnClickedReceive()
{
	UpdateData(TRUE);

	GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
	GetDlgItem(IDC_RECEIVE)->EnableWindow(FALSE);
	GetDlgItem(IDC_INIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_POLLING)->EnableWindow(FALSE);
	GetDlgItem(IDC_GET)->EnableWindow(FALSE);

	m_TextRet = _T("Receiving...");
	UpdateData(FALSE);
	UpdateWindow();
	Cmd[0] = 2;										// Talker + Listener
	Cmd[1] = Yradr;									// Address of device
	Cmd[2] = Myadr;									// MyAddress
	Srlen = sizeof(srBuffer);						// srBuffer of measure
	Ret = GpListen(Cmd, &Srlen, srBuffer);			// Receive Data
	if (CheckRet(_T("GpListen"), (Ret & 0xFF), &csBuf) == 0){// Check of [GpListen]
		srBuffer[Srlen] = 0x0;						// Add to [0x0]
		m_ReceiveData = srBuffer;					// Show srBuffer
		csBuf.Format(_T("[ %d ] Bytes Received."), Srlen);
		m_TextRet = csBuf;							// Show Srbuff
	}
	else{
		m_TextRet = csBuf;
	}
	GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
	GetDlgItem(IDC_RECEIVE)->EnableWindow(TRUE);
	GetDlgItem(IDC_INIT)->EnableWindow(TRUE);
	GetDlgItem(IDC_POLLING)->EnableWindow(TRUE);
	GetDlgItem(IDC_GET)->EnableWindow(TRUE);

	UpdateData(FALSE);
}

/////////////////////////////////////////////////// GET ///
void CMasterDlg::OnBnClickedGet()
{
	UpdateData(TRUE);
	Ret = GpGet(Yradr);								// Begin to Trigger
	Ret = CheckRet(_T("GpGet"), Ret, &csBuf);		// Check of [GpGet]
	m_TextRet = csBuf;
	UpdateData(FALSE);
}

////////////////////////////////////////////////// Exit ///
void CMasterDlg::OnBnClickedExt()
{
	GpibExit();										// [GpibExit] Function
	CDialog::OnCancel();							// End of Program
}

///////////////////////////////////////// Delete Return ///
void CMasterDlg::OnBnClickedDelRet()
{
	UpdateData(TRUE);
	m_TextRet = "";
	UpdateData(FALSE);
}

/////////////////////////////////////////// Delete Data ///
void CMasterDlg::OnBnClickedDelData()
{
	UpdateData(TRUE);
	m_SendData = "";
	m_ReceiveData = "";
	UpdateData(FALSE);
}


void CMasterDlg::OnCbnSelchangeYradrbox()
{
	Yradr = GetDlgItemInt(IDC_YRADRBOX);
}

BOOL CMasterDlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_ESCAPE: 							// ESC Key is disebled.
			return TRUE;
		default:
			break;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

/////////////////////////////////////////////// Delimiter Change ///
void CMasterDlg::OnCbnSelchangeDelimbox()
{
	UpdateData(TRUE);

	Eoi = 1;										// EOI : USE
	Delim = m_DelimBox.GetCurSel();
	Ret = GpDelim(Delim, Eoi);
}
