
// SlaveDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Slave.h"
#include "SlaveDlg.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

// CSlaveDlg dialog



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

void CSlaveDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_RETURNCODE, m_ListReturncode);
	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_SEND, m_SendString);
	DDV_MaxChars(pDX, m_SendString, 256);
	DDX_Text(pDX, IDC_EDIT_REC, m_RecString);
	DDV_MaxChars(pDX, m_RecString, 256);
	DDX_Text(pDX, IDC_EDIT_MYADDRESS, m_Myaddress);
	DDV_MinMaxLong(pDX, m_Myaddress, 0, 31);
	DDX_Text(pDX, IDC_EDIT_STATUSBYTE, m_Stb);
	DDV_MinMaxInt(pDX, m_Stb, 0, 255);
	DDX_Radio(pDX, IDC_RADIO_SRQ_NOT_SEND, m_RadioSrqSend);
	DDX_Radio(pDX, IDC_RADIO_PARALLELPOLL_RESPONSE_NOT, m_RadioParallelpollResopnse);
}

BEGIN_MESSAGE_MAP(CSlaveDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_INITIAL, &CSlaveDlg::OnBnClickedInitial)
	ON_BN_CLICKED(IDOK, &CSlaveDlg::OnBnClickedOk)
	ON_BN_CLICKED(ID_SEND, &CSlaveDlg::OnBnClickedSend)
	ON_BN_CLICKED(ID_REC, &CSlaveDlg::OnBnClickedRec)
	ON_BN_CLICKED(ID_SRQ_SETTING, &CSlaveDlg::OnBnClickedSrqSetting)
	ON_BN_CLICKED(ID_PARALLELPOLL_SETTING, &CSlaveDlg::OnBnClickedParallelpollSetting)
	ON_BN_CLICKED(ID_CLEAR_DATA, &CSlaveDlg::OnBnClickedClearData)
	ON_BN_CLICKED(ID_CLEAR_RETURNCODE, &CSlaveDlg::OnBnClickedClearReturncode)
END_MESSAGE_MAP()


// CSlaveDlg message handlers

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

//**************************************************************************************************
//   Display transmitted string to list box of "Return code"(sub routine)
//**************************************************************************************************
void CSlaveDlg::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 CSlaveDlg::funcSetParam(void)
{
	short	Delim;
	short	Eoi;
	short	Eos;
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];
	CString	rString;

	//Set delimiter and EOI line
	Delim = m_ComboDelimiter.GetCurSel();
	Eoi = m_ComboEoi.GetCurSel();
	Eos = 0;
	Ret = GpibSetDelim(
		m_Id,
		Delim,			//Set delimiter
		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 CSlaveDlg::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 slave
		Ret = GpibGetStatus(m_Id, 0x0A, &Data);
		if ((Ret == 0) && (Data == 0)) {
			MessageBox(TEXT("It is not a slave. Please set it to slave 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 CSlaveDlg::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();
}

//**************************************************************************************************
//   Send data
//   Function: Send data to master device
//**************************************************************************************************
void CSlaveDlg::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_Id, &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 master device
//**************************************************************************************************
void CSlaveDlg::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_Id, &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
}

//**************************************************************************************************
//   Set status byte
//   Function: Transmit SRQ and set status byte
//**************************************************************************************************
void CSlaveDlg::OnBnClickedSrqSetting()
{
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

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

	//Transmit SRQ and perform the functin of seting status byte
	Ret = GpibSendSRQ(m_Id, m_RadioSrqSend, m_Stb);
	if (Ret != 0) {
		//Error
		GpibGetErrorString(Ret, strRet);
		wsprintf(szStrRet, _T("%S"), strRet);
		funcSetlstRet(szStrRet);	//Display in list
	}
	else {
		wsprintf(szStrRet, _T("[Transmit SRQ ans set status byte] completed normally"));
		funcSetlstRet(szStrRet);	//Display in list
	}
}

//**************************************************************************************************
//   Set parallel poll response
//**************************************************************************************************
void CSlaveDlg::OnBnClickedParallelpollSetting()
{
	short	Response;
	short	Ist;
	long	Ret;
	char	strRet[256];
	TCHAR	szStrRet[256];

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

	switch (m_RadioParallelpollResopnse) {
	case 0:	//[No response]
		Response = 0;
		break;
	case 1:	//[Response of ISTbitiIST=0j]
		Response = 1;
		Ist = 0;
		break;
	case 2:	//[Response of ISTbitiIST=1j]
		Response = 1;
		Ist = 1;
		break;
	case 3:	//[Response with SRQ]
		Response = 2;
		break;
	}

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

	if (Response == 1) {
		//Perform the function of setting ISTbit
		Ret = GpibSetIst(m_Id, Ist);
		if (Ret != 0) {
			//Error
			GpibGetErrorString(Ret, strRet);
			wsprintf(szStrRet, _T("%S"), strRet);
			funcSetlstRet(szStrRet);	//Display in list
		}
		else {
			wsprintf(szStrRet, _T("[Set IST of parallel poll response] completed normally"));
			funcSetlstRet(szStrRet);	//Display in list
		}
	}
}

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

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

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

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

	return CDialog::PreTranslateMessage(pMsg);
}
