
// MasterDlg.cpp : implementation file
//

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

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//Include the API-GPIB(WDM) header file
#include "CGpib.h"

// Message
#define	GPIB_NOTIFY_MESSAGE	0x15FF

// CMasterDlg dialog



CMasterDlg::CMasterDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMasterDlg::IDD, pParent)
{
	m_DeviceNameString = _T("GPIB000");
	m_Otheraddress = 1;
	m_SendString = _T("CONTEC Master Sample");
	m_Myaddress = 0;
	m_RecString = _T("");
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMasterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_RETURNCODE, m_ListReturncode);
	DDX_Control(pDX, IDC_COMBO_OTHERADDRESS, m_ComboOtheraddress);
	DDX_Control(pDX, IDC_COMBO_EOI, m_ComboEoi);
	DDX_Control(pDX, IDC_COMBO_DELIMITER, m_ComboDelimiter);
	DDX_Text(pDX, IDC_EDIT_DEVICENAME, m_DeviceNameString);
	DDV_MaxChars(pDX, m_DeviceNameString, 256);
	DDX_Text(pDX, IDC_EDIT_OTHERADDRESS, m_Otheraddress);
	DDV_MinMaxInt(pDX, m_Otheraddress, 0, 31);
	DDX_Text(pDX, IDC_EDIT_SEND, m_SendString);
	DDV_MaxChars(pDX, m_SendString, 256);
	DDX_Text(pDX, IDC_EDIT_MYADDRESS, m_Myaddress);
	DDV_MinMaxLong(pDX, m_Myaddress, 0, 30);
	DDX_Text(pDX, IDC_EDIT_REC, m_RecString);
	DDV_MaxChars(pDX, m_RecString, 256);
}

BEGIN_MESSAGE_MAP(CMasterDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_INITIAL, &CMasterDlg::OnBnClickedInitial)
	ON_BN_CLICKED(IDOK, &CMasterDlg::OnBnClickedOk)
	ON_BN_CLICKED(ID_FINDLISTENER, &CMasterDlg::OnBnClickedFindlistener)
	ON_BN_CLICKED(ID_SEND, &CMasterDlg::OnBnClickedSend)
	ON_BN_CLICKED(ID_REC, &CMasterDlg::OnBnClickedRec)
	ON_BN_CLICKED(ID_GET, &CMasterDlg::OnBnClickedGet)
	ON_BN_CLICKED(ID_SDC, &CMasterDlg::OnBnClickedSdc)
	ON_BN_CLICKED(ID_GTL, &CMasterDlg::OnBnClickedGtl)
	ON_BN_CLICKED(ID_DCL, &CMasterDlg::OnBnClickedDcl)
	ON_BN_CLICKED(ID_LLO, &CMasterDlg::OnBnClickedLlo)
	ON_BN_CLICKED(ID_SERIALPOLLING, &CMasterDlg::OnBnClickedSerialpolling)
	ON_BN_CLICKED(ID_SET_PARALLELPOLL, &CMasterDlg::OnBnClickedSetParallelpoll)
	ON_BN_CLICKED(ID_EXE_PARALLELPOLL, &CMasterDlg::OnBnClickedExeParallelpoll)
	ON_BN_CLICKED(ID_CLEAR_DATA, &CMasterDlg::OnBnClickedClearData)
	ON_BN_CLICKED(ID_CLEAR_RETURNCODE, &CMasterDlg::OnBnClickedClearReturncode)
	ON_CBN_SELCHANGE(IDC_COMBO_OTHERADDRESS, OnSelchangeComboOtheraddress)
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

	//Initialize the delimiter combo box
	m_ComboDelimiter.ResetContent();
	m_ComboDelimiter.InsertString(-1, _T("Not use"));
	m_ComboDelimiter.InsertString(-1, _T("CR+LF"));
	m_ComboDelimiter.InsertString(-1, _T("CR"));
	m_ComboDelimiter.InsertString(-1, _T("LF"));
	m_ComboDelimiter.SetCurSel(1);	//Set "CR+LF" as default

	//Initialize the EOI combo box
	m_ComboEoi.ResetContent();
	m_ComboEoi.InsertString(-1, _T("Not use"));
	m_ComboEoi.InsertString(-1, _T("Use"));
	m_ComboEoi.SetCurSel(1);	//Set "Use" as default

	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);
}

//**************************************************************************************************
//   Display transmitted string to list box of "Return code"(sub routine)
//**************************************************************************************************
void CMasterDlg::funcSetlstRet(TCHAR *strRet)
{
	//Add the item to the end of list box
	//Clear all of the items if the number of items more than 256
	if (m_ListReturncode.GetCount() >= 256) {
		m_ListReturncode.ResetContent();
		m_ListReturncode.InsertString(-1, strRet);
	}
	else {
		m_ListReturncode.InsertString(-1, strRet);
	}
	m_ListReturncode.SetCurSel(m_ListReturncode.GetCount() - 1);
}

//**************************************************************************************************
//   Set the target (sub routine)
//   Function: Set Equipment ID, Delimiter and EOI
//   Return value: TRUE(normally), FALSE(abnormally)
//**************************************************************************************************
bool CMasterDlg::funcSetParam(void)
{
	short	Delim;
	short	Eoi;
	short	Eos;
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];
	CString	rString;

	//Specify the partner apparatus being detected
	if (m_ComboOtheraddress.GetCount() != 0) {
		//Get Equipment ID of the device specified in combo box
		m_SelectEqpId = m_EqpId[m_ComboOtheraddress.GetCurSel()];
	}
	else {
		//Get Equipment ID of device address specified in edit box
		Delim = m_ComboDelimiter.GetCurSel();
		Ret = GpibSetEquipment(
			m_Id,			//Device ID
			&m_SelectEqpId,	//Retrieved Equipment ID
			m_Otheraddress,	//Primary address 
			0,				//Secondary address 
			Delim			//Set delimiter
			);
		if (Ret != 0) {
			//Error
			GpibGetErrorString(Ret, strRet);
			wsprintf(szStrRet, _T("%S"), strRet);
			funcSetlstRet(szStrRet);	//Display in list
			return FALSE;
		}
	}

	//Set delimiter code and EOI line
	Delim = m_ComboDelimiter.GetCurSel();
	Eoi = m_ComboEoi.GetCurSel();
	Eos = 0;
	Ret = GpibSetDelim(
		m_SelectEqpId,
		Delim,			//Set delimiter code
		Eoi,			//Set EOI line
		Eos				//Set EOS(reserved)
		);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else  {
		wsprintf(szStrRet, _T("[Partner apparatus setting] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}

	return TRUE;
}

//**************************************************************************************************
//   Initialization
//   Function: Get Device ID from device name
//**************************************************************************************************
void CMasterDlg::OnBnClickedInitial()
{
	long	Ret;
	long	Data;
	char	DeviceName[256];
	TCHAR	szDeviceName[256];
	char	strRet[256];
	TCHAR	szStrRet[256];

	UpdateData(TRUE);	//Reflect dialog data to member varialbes

	wsprintf(szDeviceName, m_DeviceNameString);
	sprintf_s(DeviceName, "%S", szDeviceName);

	Ret = GpibInit(DeviceName, &m_Id);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else  {
		//When got Device ID normally
		wsprintf(szStrRet, _T("GpibInit : Device ID = %d"), m_Id);
		funcSetlstRet(szStrRet);	//Display in list
		//Get device address
		Ret = GpibGetStatus(m_Id, 0x08, &Data);
		if (Ret == 0) m_Myaddress = Data;
		//Confirm whether it is set to master
		Ret = GpibGetStatus(m_Id, 0x0A, &Data);
		if ((Ret == 0) && (Data == 1)) {
			MessageBox(TEXT("It is not a master. Please set it to master in property page."), TEXT("Error"), MB_OK);
		}
		else {
			wsprintf(szStrRet, _T("[Initialize] completed normally"));
			funcSetlstRet(szStrRet);	//Display in list
		}
	}
	GpibSendIFC(m_Id, 1);	//Send IFC

	UpdateData(FALSE);	//Reflect member varialbes to dialog data
}

//**************************************************************************************************
//   Exit
//   Function: Disable Device ID
//**************************************************************************************************
void CMasterDlg::OnBnClickedOk()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	Ret = GpibExit(m_Id);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}

	CDialogEx::OnOK();
}

//**************************************************************************************************
//   Detect partner apparatus
//   FuntionF Detect partner apparatus from Device ID and get Equipment ID.
//         Send the [*IDN?] command according with IEEE488.2 standard to partner apparatus with
//         the Equipment ID and get Device ID(maker and module number etc.).
//**************************************************************************************************
void CMasterDlg::OnBnClickedFindlistener()
{
	short	PrmAddr;
	short	ScdAddr;
	short	ArraySize;
	short	AddrArray[32];
	short	ArrayCount;
	short	i;
	long	Ret;
	long	SendLen;
	long	RecLen;
	char	RecBuf[256];
	char	strRet[256];
	TCHAR	szStrRet[256];
	char	SendBuf[256];
	TCHAR	szSendBuf[256];
	TCHAR	EqpInforBuf[256 + 16];

	PrmAddr = -1;	//Search all primary address
	ScdAddr = 0;	//Do not search secondary address 
	ArraySize = 32;	//Specify the number of devices to be detected 
	Ret = GpibFindListener(m_Id, PrmAddr, ScdAddr, &ArraySize, AddrArray);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else  {
		//Display detected partner apparatus when finishing detecting normally
		wsprintf(szStrRet, _T("GpibFindListener : the number of detected partner apparatus(es) = %d"), ArraySize);
		funcSetlstRet(szStrRet);	//Display in list

		//Get device information of detected device
		m_ComboOtheraddress.ResetContent();	//Initialize combo box items
		for (ArrayCount = 0; ArrayCount < ArraySize; ArrayCount++) {
			//Get Equipment ID
			Ret = GpibSetEquipment(
				m_Id,					//Device ID
				&m_EqpId[ArrayCount],	//Retrieved Equipment ID
				AddrArray[ArrayCount],	//Primary address
				0,						//Secondary address
				m_ComboDelimiter.GetCurSel()	//Set delimiter
				);
			if (Ret != 0) {
				//Error
				GpibGetErrorString(Ret, strRet);
				wsprintf(szStrRet, _T("%S"), strRet);
				funcSetlstRet(szStrRet);	//Display in list
				continue;
			}

			//Inquiry device information(data sending)
			wsprintf(szSendBuf, _T("*IDN?"));	//Data to be sent
			sprintf_s(SendBuf, "%S", szSendBuf);
			SendLen = (long)strlen(SendBuf);	//The number of data being sent
			Ret = GpibSendData(m_EqpId[ArrayCount], &SendLen, SendBuf);
			if (Ret != 0) {
				//Error
				GpibGetErrorString(Ret, strRet);
				wsprintf(szStrRet, _T("%S"), strRet);
				funcSetlstRet(szStrRet);	//Display in list
				continue;
			}

			//Inquiry device information(data receiving)
			for (i = 0; i < 256; i++) RecBuf[i] = 0x00;
			RecLen = 255;	//Specify buffer size for receiving data
			Ret = GpibRecData(m_EqpId[ArrayCount], &RecLen, RecBuf);
			RecBuf[RecLen] = '\0';
			if (Ret != 0) {
				//Error
				GpibGetErrorString(Ret, strRet);
				wsprintf(szStrRet, _T("%S"), strRet);
				funcSetlstRet(szStrRet);	//Display in list
				continue;
			}


			//Inquiry device information(data receiving)
			if (strstr(RecBuf, ",") > 0) {
				//Combine with address information
				wsprintf(EqpInforBuf, _T("Address:%d %S"), AddrArray[ArrayCount], RecBuf);
			}
			else {
				//Include only address information if receiving data does not accord with IEEE488.2 standard
				wsprintf(EqpInforBuf, _T("Address:%d"), AddrArray[ArrayCount]);
			}

			//Add detected device to the list
			m_ComboOtheraddress.InsertString(-1, EqpInforBuf);
		}

		//Reflect detected device to combo box and edit box
		m_ComboOtheraddress.SetCurSel(0);
		OnSelchangeComboOtheraddress();
	}
}

//**************************************************************************************************
//   Send Data
//   Function: Send data to slave device
//**************************************************************************************************
void CMasterDlg::OnBnClickedSend()
{
	long	SendLen;
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];
	char	SendBuf[256];
	TCHAR	szSendBuf[256];

	UpdateData(TRUE);	//Reflect dialog data to member varialbes

	//Set Equipment ID, delimiter and EOI
	if (funcSetParam() == FALSE) return;

	//Get data to be sent from edit box to
	wsprintf(szSendBuf, m_SendString);
	sprintf_s(SendBuf, "%S", szSendBuf);
	SendLen = (long)strlen(SendBuf);
	if (SendLen == 0) {
		wsprintf(szStrRet, _T("GpibSendData : there is no data"));
		funcSetlstRet(szStrRet);	//Display in list
		return;
	}

	funcSetlstRet(_T("GpibSendData : sending data..."));	//Display in list
	//Send data
	Ret = GpibSendData(m_SelectEqpId, &SendLen, SendBuf);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else  {
		//When sent data normally
		wsprintf(szStrRet, _T("GpibSendData : the number of sent data = %d"), SendLen);
		funcSetlstRet(szStrRet);	//Display in list
		wsprintf(szStrRet, _T("[Send data] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Receive data
//   Function: Receive data from slave device
//**************************************************************************************************
void CMasterDlg::OnBnClickedRec()
{
	long	RecLen;
	long	Ret;
	long	i;
	char	strRet[256];
	TCHAR	szStrRet[256];
	char	RecBuf[256];

	UpdateData(TRUE);	//Reflect dialog data to member varialbes

	//Set Equipment ID, delimiter and EOI
	if (funcSetParam() == FALSE) return;

	for (i = 0; i < 256; i++) RecBuf[i] = 0x00;
	RecLen = 255;	//Set recieving buffer size
	funcSetlstRet(_T("GpibRecData : receiving data..."));	//Display in list
	//Receive data
	Ret = GpibRecData(m_SelectEqpId, &RecLen, RecBuf);
	RecBuf[RecLen] = '\0';
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		//When received data normally
		m_RecString.Format(_T("%S"), RecBuf);
		wsprintf(szStrRet, _T("GpibRecData : the number of received data = %d"), RecLen);
		funcSetlstRet(szStrRet);	//Display in list
		wsprintf(szStrRet, _T("[Receive data] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}

	UpdateData(FALSE);	//Reflect member varialbes to dialog data
}

//**************************************************************************************************
//   Send GET Command
//   Function: Performs transfer of command (GET) starts the matter decided by slave device.
//**************************************************************************************************
void CMasterDlg::OnBnClickedGet()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	UpdateData(TRUE);	//Reflect dialog data to member varialbes

	if (funcSetParam() == FALSE) return;

	//Perform the function of sending GET command
	Ret = GpibSendTrigger(m_SelectEqpId);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Send GET command] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Send SDC Command
//   Function: Perform transfer of commands (SDC) that make slave device to initial states (when power is supplied)
//**************************************************************************************************
void CMasterDlg::OnBnClickedSdc()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	UpdateData(TRUE);	//Reflect dialog data to member varialbes

	if (funcSetParam() == FALSE) return;

	//Perform the function of sending SDC command
	Ret = GpibSendDeviceClear(m_SelectEqpId);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Send SDC command] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Send GTL command
//   Function: Transmit the command(GTL) that make slave device return to local mode
//**************************************************************************************************
void CMasterDlg::OnBnClickedGtl()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	UpdateData(TRUE);	//Reflect dialog data to member varialbes

	if (funcSetParam() == FALSE) return;

	//Perform the function of sending GTL command
	Ret = GpibChangeLocal(m_SelectEqpId);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Send GTL command] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Send DCL command
//   Function: Perform transfer of command (DCL) that make all slave devices to initial states (when power is supplied)
//**************************************************************************************************
void CMasterDlg::OnBnClickedDcl()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	//Perform the function of sending DCL command
	Ret = GpibSendDeviceClear(m_Id);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Send DCL command] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}

}

//**************************************************************************************************
//   Send LLO command
//   Function: Perform transfer of command (LLO) that disables the transition to local mode by switch on device of all slave devices
//**************************************************************************************************
void CMasterDlg::OnBnClickedLlo()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	//Perform the function of Sending LLO command
	Ret = GpibSendLocalLockout(m_Id);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Send LLO command] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Perform serial poll
//   Function: Get status byte of slave device
//**************************************************************************************************
void CMasterDlg::OnBnClickedSerialpolling()
{
	short	Stb;
	short	Srq;
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	UpdateData(TRUE);	//Reflect dialog data to member varialbes

	if (funcSetParam() == FALSE) return;

	wsprintf(szStrRet, _T("GpibSPoll : serial polling..."));
	funcSetlstRet(szStrRet);	//Display in list

	//Perform the function of serial poll
	Ret = GpibSPoll(m_SelectEqpId, &Stb, &Srq);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("GpibSPoll : status byte = %02X(H) SRQ line = %02X(H)"), Stb, Srq);
		funcSetlstRet(szStrRet);	//Display in list
		wsprintf(szStrRet, _T("[Serial poll] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Set parallel poll
//   Function: set the line of parallel poll response
//**************************************************************************************************
void CMasterDlg::OnBnClickedSetParallelpoll()
{
	short	AddrArray[9];
	short	DataLineArray[9];
	short	PolarityArray[9];
	short	i;
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	UpdateData(TRUE);	//Reflect dialog data to member varialbes

	//Set by device address in edit box
	i = 0;
	AddrArray[i] = m_Otheraddress;	//Device address
	DataLineArray[i] = i + 1;		//DIO line number
	PolarityArray[i] = 1;			//Set as inphase
	i++;

	//Set the end of the array
	AddrArray[i] = -1;
	DataLineArray[i] = -1;
	PolarityArray[i] = -1;

	//Perform the function of setting parallel poll
	Ret = GpibSetPPoll(m_Id, AddrArray, DataLineArray, PolarityArray);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Set parallel poll] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Perform parallel poll
//   Function: Perform parallel poll and get DIO line data
//**************************************************************************************************
void CMasterDlg::OnBnClickedExeParallelpoll()
{
	short	Ppr;
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

	//Perform the function of parallel poll
	Ret = GpibPPoll(m_Id, &Ppr);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("GpibPPoll : DIO line data = %02X(H)"), Ppr);
		funcSetlstRet(szStrRet);	//Display in list
		wsprintf(szStrRet, _T("[Parallel poll] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Clear send/receive text data
//**************************************************************************************************
void CMasterDlg::OnBnClickedClearData()
{
	m_SendString.Format(_T("%S"), "");
	m_RecString.Format(_T("%S"), "");

	UpdateData(FALSE);	//Reflect member varialbes to dialog data
}

//**************************************************************************************************
//   Clear return code
//**************************************************************************************************
void CMasterDlg::OnBnClickedClearReturncode()
{
	//Clear items of list box
	m_ListReturncode.ResetContent();
}

//**************************************************************************************************
//   Disable application ending by ESC key
//**************************************************************************************************
BOOL CMasterDlg::PreTranslateMessage(MSG* pMsg)
{
	//Disable the ESC key
	if (pMsg->wParam == 0x1b) return 0;

	return CDialog::PreTranslateMessage(pMsg);
}

//**************************************************************************************************
//   Reflect device GPIB address specified in combo box to edit box
//**************************************************************************************************
void CMasterDlg::OnSelchangeComboOtheraddress()
{
	long	Ret;
	short	ScdAddr;
	short	Eos;

	//Get Equipment ID
	Ret = GpibGetEquipment(
		m_EqpId[m_ComboOtheraddress.GetCurSel()],	//Set retrieved Equipment ID
		&m_Otheraddress,	//Primary address
		&ScdAddr,			//Secondary address
		&Eos				//Set delimiter
		);

	UpdateData(FALSE);	//Reflect member varialbes to dialog data	
}
