
// ConRecDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ConRec.h"
#include "ConRecDlg.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

// CConRecDlg dialog



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

void CConRecDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_DELIMITER, m_ComboDelimiter);
	DDX_Control(pDX, IDC_COMBO_EOI, m_ComboEoi);
	DDX_Control(pDX, IDC_LIST_RETURNCODE, m_ListReturncode);
	DDX_Control(pDX, IDC_COMBO_OTHERADDRESS, m_ComboOtheraddress);
	DDX_Control(pDX, IDC_LIST_SRQ_REC_DATA, m_ListSrqrecdata);
	DDX_Text(pDX, IDC_EDIT_DEVICENAME, m_DeviceNameString);
	DDV_MaxChars(pDX, m_DeviceNameString, 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);
	DDX_Text(pDX, IDC_EDIT_SEND, m_SendString);
	DDV_MaxChars(pDX, m_SendString, 256);
	DDX_Text(pDX, IDC_EDIT_OTHERADDRESS, m_Otheraddress);
	DDV_MinMaxInt(pDX, m_Otheraddress, 0, 30);
	DDX_Radio(pDX, IDC_RADIO_TIMING, m_RadioTiming);
	DDX_Text(pDX, IDC_EDIT_TIMER, m_Timer);
	DDV_MinMaxUInt(pDX, m_Timer, 0, 10000);
}

BEGIN_MESSAGE_MAP(CConRecDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_INITIAL, &CConRecDlg::OnBnClickedInitial)
	ON_BN_CLICKED(IDOK, &CConRecDlg::OnBnClickedOk)
	ON_BN_CLICKED(ID_FINDLISTENER, &CConRecDlg::OnBnClickedFindlistener)
	ON_BN_CLICKED(ID_SEND, &CConRecDlg::OnBnClickedSend)
	ON_BN_CLICKED(ID_REC, &CConRecDlg::OnBnClickedRec)
	ON_BN_CLICKED(ID_CLEAR_DATA, &CConRecDlg::OnBnClickedClearData)
	ON_BN_CLICKED(ID_SRQ_REC_START, &CConRecDlg::OnBnClickedSrqRecStart)
	ON_BN_CLICKED(ID_SRQ_REC_STOP, &CConRecDlg::OnBnClickedSrqRecStop)
	ON_BN_CLICKED(ID_SAVE_SRQ_REC_DATA, &CConRecDlg::OnBnClickedSaveSrqRecData)
	ON_BN_CLICKED(ID_SRQ_REC_TRIGGER, &CConRecDlg::OnBnClickedSrqRecTrigger)
	ON_BN_CLICKED(ID_CLEAR_RETURNCODE, &CConRecDlg::OnBnClickedClearReturncode)
	ON_CBN_SELCHANGE(IDC_COMBO_OTHERADDRESS, OnSelchangeComboOtheraddress)
	ON_WM_TIMER()
	ON_MESSAGE(GPIB_NOTIFY_MESSAGE, NotifySignalHandler)	//Add message handler
END_MESSAGE_MAP()


// CConRecDlg message handlers

BOOL CConRecDlg::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 CConRecDlg::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 CConRecDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//**************************************************************************************************
//   Display transmitted string to list box of "Return code"(sub routine)
//**************************************************************************************************
void CConRecDlg::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);
}

//**************************************************************************************************
//   Display transmitted string to list box of received data(sub routine)
//**************************************************************************************************
void CConRecDlg::funcSetlstSrqRecData(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_ListSrqrecdata.GetCount() >= 256) {
		m_ListSrqrecdata.ResetContent();
		m_ListSrqrecdata.InsertString(-1, strRet);
	}
	else {
		m_ListSrqrecdata.InsertString(-1, strRet);
	}
	m_ListSrqrecdata.SetCurSel(m_ListSrqrecdata.GetCount() - 1);
}

//**************************************************************************************************
//   Set the target (sub routine)
//   Function: Set Equipment ID, Delimiter and EOI
//   Return value: TRUE(normally), FALSE(abnormally)
//**************************************************************************************************
bool CConRecDlg::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 CConRecDlg::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 CConRecDlg::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 CConRecDlg::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;
			}

			//Confirm whether receiving data accords with IEEE488.2 standard
			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 CConRecDlg::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
	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 CConRecDlg::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	
}

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

	//Clear items of list box
	m_ListSrqrecdata.ResetContent();

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

//**************************************************************************************************
//   Start SRQ event receiving
//   Function: Receive data when SRQ is enabled
//**************************************************************************************************
void CConRecDlg::OnBnClickedSrqRecStart()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

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

	//Start timer if "Timer" is selected
	if (m_RadioTiming == 1) {
		SetTimer(1, m_Timer, NULL);
	}
	//Perform trigger if "Maximum speed" is selected
	if (m_RadioTiming == 2) {
		OnBnClickedSrqRecTrigger();
	}

	//Perform the function of event setting(enable SRQ event receiving)
	Ret = GpibSetNotifySignal(m_Id, m_hWnd, 0x0020);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Start SRQ event receiving] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Stop SRQ event receiving
//   Function: Stop event receiving
//**************************************************************************************************
void CConRecDlg::OnBnClickedSrqRecStop()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

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

	//Stop timer if "timer" is selected
	if (m_RadioTiming == 1) {
		KillTimer(1);
	}

	//Perform the function of event setting(cancel SRQ event receiving)
	Ret = GpibSetNotifySignal(m_Id, m_hWnd, 0x0000);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Stop SRQ event receiving] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Save received data to CSV file
//**************************************************************************************************
void CConRecDlg::OnBnClickedSaveSrqRecData()
{
	//Display file dialog
	CFileDialog dlgFile(FALSE,
		_T("csv"),
		_T("ConRec.csv"),
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("csv Files (*.csv)|*.csv|All Files (*.*)|*.*||"),
		this);
	if (dlgFile.DoModal() == IDOK)
	{
		CStdioFile	fout;
		long		i;
		TCHAR		strGetListData[256];
		TCHAR		strFileData[256];

		//Open file
		if (fout.Open(dlgFile.GetPathName(), CFile::modeCreate | CFile::modeWrite) == NULL){
			return;
		}
		//Write received data to ther file
		fout.WriteString(_T("Received Times, Received Data\n"));
		for (i = 0; i < m_ListSrqrecdata.GetCount(); i++) {
			m_ListSrqrecdata.GetText(i, strGetListData);
			wsprintf(strFileData, _T("%d,%s\n"), i, strGetListData);
			fout.WriteString(strFileData);
		}
		//Close file
		fout.Close();
	}
}

//**************************************************************************************************
//   Start manual timing
//   Function: Send GET command
//         When the GET command can not operate normally according to different
//         partner apparatus,  please modify it according with device specification.
//**************************************************************************************************
void CConRecDlg::OnBnClickedSrqRecTrigger()
{
	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
	}
}

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

//**************************************************************************************************
//   Disable application ending by ESC key
//**************************************************************************************************
BOOL CConRecDlg::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 CConRecDlg::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
}

//**************************************************************************************************
//   Start timer event
//   Function: Perform trigger at fixed interval
//**************************************************************************************************
void CConRecDlg::OnTimer(UINT_PTR nIDEvent)
{
	OnBnClickedSrqRecTrigger();

	CDialog::OnTimer(nIDEvent);
}

//**************************************************************************************************
//   SRQ event receiving
//   Function: Perform serial poll and receive data when SRQ line is enabled
//**************************************************************************************************
LRESULT CConRecDlg::NotifySignalHandler(WPARAM wParam, LPARAM lParam)
{
	short	Stb;
	short	Srq;
	long	RecLen;
	long	Ret;
	long	i;
	char	strRet[256];
	TCHAR	szStrRet[256];
	char	RecBuf[256];
	TCHAR	szRecBuf[256];

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

	//----------------------------------------
	//  Process of SRQ receiving
	//----------------------------------------
	if (wParam & 0x0020) {
		//Set Equipment ID, delimiter and EOI
		if (funcSetParam() == FALSE) goto cmsNotifySignalEventEnd;

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

		//Perform serial poll function
		Ret = GpibSPoll(m_SelectEqpId, &Stb, &Srq);
		if (Ret != 0) {
			//Error
			GpibGetErrorString(Ret, strRet);
			wsprintf(szStrRet, _T("%S"), strRet);
			funcSetlstRet(szStrRet);	//Display in list
			//Stop SRQ receiving when serial poll occurred error
			OnBnClickedSrqRecStop();
			return 0;
		}
		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
		}

		//   Receive data
		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
			//Stop SRQ receiving when error occurred in receiving data
			OnBnClickedSrqRecStop();
			return 0;
		}
		else {
			//When received data normally
			wsprintf(szRecBuf, _T("%S"), RecBuf);
			funcSetlstSrqRecData(szRecBuf);	//Display received data in list

			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
		}

		//Perform trigger if "Maximum Speed" is selected
		if (m_RadioTiming == 2) {
			OnBnClickedSrqRecTrigger();
		}
	}

cmsNotifySignalEventEnd:
	//----------------------------------------
	//   Setting of waiting next event
	//----------------------------------------
	//Perform the function of event setting
	Ret = GpibSetNotifySignal(m_Id, m_hWnd, 0x0020);
	//Process of return code
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}

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

	return 0;
}
