// 7k_dio_latchDlg.cpp : implementation file
//

#include "stdafx.h"
#include "7k_dio_latch.h"
#include "7k_dio_latchDlg.h"
#include "pacsdk.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMy7k_dio_latchDlg dialog

CMy7k_dio_latchDlg::CMy7k_dio_latchDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy7k_dio_latchDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy7k_dio_latchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMy7k_dio_latchDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CMy7k_dio_latchDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CMy7k_dio_latchDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CMy7k_dio_latchDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CMy7k_dio_latchDlg message handlers

BOOL CMy7k_dio_latchDlg::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

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CMy7k_dio_latchDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_MY7K_DIO_LATCH_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_MY7K_DIO_LATCH_DIALOG));
	}
}
#endif

HANDLE hPort;
BOOL iRet;
void CMy7k_dio_latchDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	
	TCHAR temp[20] = {0};
	DWORD DIValue = 0;
	DWORD DOValue = 0;
	DWORD DIHighLatchValue,DILowLatchValue,DOHighLatchValue,DOLowLatchValue;
    TCHAR OpenPort[10];
    char port[20];
	int address;
	int BuadRate;
	int TotalDIchannel;
	int TotalDOchannel;

    //get com port
    GetDlgItemText(IDC_COMBO2, OpenPort, 10);

	//get address
	GetDlgItemText(IDC_EDIT2, temp, 20);
    address = _wtoi(temp);

	//get buad rate
    GetDlgItemText(IDC_EDIT3, temp, 20);
    BuadRate = _wtoi(temp);	

	//uart_Open parameter format is COMx,buadrate  , for ex will set COM3 port and baud rate 9600 to uart_Open,
    //parameter is "COM3,9600"
    sprintf(port, "%ls,%d", OpenPort, BuadRate);
    hPort = uart_Open(port);

    if(hPort == INVALID_HANDLE_VALUE)
    {
        ::MessageBox(NULL, L"Open Port Error", L"7k_dio_latch", MB_OK);
    }

    //get user set total DI channel
    GetDlgItemText(IDC_EDIT4, temp, 20);
    TotalDIchannel = _wtoi(temp);

    //get user set total DO channel
    GetDlgItemText(IDC_EDIT5, temp, 20);
    TotalDOchannel = _wtoi(temp);

	//Read DI and DO data
    iRet = pac_ReadDIO(hPort, PAC_REMOTE_IO(address), TotalDIchannel, TotalDOchannel, &DIValue, &DOValue);

	if(!iRet)
	{
		::MessageBox(NULL,L"Read DIO error",L"7k_dio_latch",MB_OK);
		uart_Close(hPort);
		return;
	}
	else
	{
		wsprintf(temp,L"0x%lx",DIValue);
		SetDlgItemText(IDC_EDIT6,temp);

		wsprintf(temp,L"0x%lx",DOValue);
		SetDlgItemText(IDC_EDIT7,temp);	
	}

	//Read DI & DO low latch Value
	iRet = pac_ReadDIOLatch(hPort, PAC_REMOTE_IO(address), TotalDIchannel, TotalDOchannel, 0, &DILowLatchValue, &DOLowLatchValue);
	
	if(!iRet)
	{
		::MessageBox(NULL,L"Read DIO error",L"7k_dio_latch",MB_OK);
		uart_Close(hPort);
		return;
	}
	else
	{
		wsprintf(temp,L"0x%lx",DILowLatchValue);
		SetDlgItemText(IDC_EDIT9,temp);

		wsprintf(temp,L"0x%lx",DOLowLatchValue);
		SetDlgItemText(IDC_EDIT11,temp);	
	}

	//Read DI & DO High latch Value
	iRet = pac_ReadDIOLatch(hPort, PAC_REMOTE_IO(address), TotalDIchannel, TotalDOchannel, 1, &DIHighLatchValue, &DOHighLatchValue);
	
	if(!iRet)
	{
		::MessageBox(NULL,L"Read DIO error",L"7k_dio_latch",MB_OK);
		uart_Close(hPort);
		return;
	}
	else
	{
		wsprintf(temp,L"0x%lx",DIHighLatchValue);
		SetDlgItemText(IDC_EDIT8,temp);

		wsprintf(temp,L"0x%lx",DOHighLatchValue);
		SetDlgItemText(IDC_EDIT10,temp);	
	}

	//close open port
	uart_Close(hPort);
}

void CMy7k_dio_latchDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	TCHAR temp[20];
    TCHAR OpenPort[10];
    char port[20];

	//get user set com port and buad rate
    GetDlgItemText(IDC_COMBO2, OpenPort, 10);

    GetDlgItemText(IDC_EDIT3, temp, 20);
    int BuadRate = _wtoi(temp);

    //uart_Open parameter format is COMx,buadrate  , for ex will set COM3 port and baud rate 9600 to uart_Open,
    //parameter is "COM3,9600"
    sprintf(port, "%ls,%d", OpenPort, BuadRate);
    hPort = uart_Open(port);

    //get user set address
	GetDlgItemText(IDC_EDIT2, temp, 20);
    int address = _wtoi(temp);

	//use pac_ClearDILatch functio to clear DI Latch data
	pac_ClearDIOLatch(hPort, PAC_REMOTE_IO(address));
	uart_Close(hPort);
	SetDlgItemText(IDC_EDIT8,L"0");
	SetDlgItemText(IDC_EDIT9,L"0");
	SetDlgItemText(IDC_EDIT10,L"0");
	SetDlgItemText(IDC_EDIT11,L"0");
}

void CMy7k_dio_latchDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
    TCHAR temp[20];
    DWORD DOChannel = 0;
    CString strTemp;
    char ctemp[20] = {0};
	int address;
	int TotalDOchannel;

    TCHAR OpenPort[10];
    char port[20];
	int BuadRate;

    //get com port
    GetDlgItemText(IDC_COMBO2, OpenPort, 10);

	//get buad rate
    GetDlgItemText(IDC_EDIT3, temp, 20);
    BuadRate = _wtoi(temp);	

	//uart_Open parameter format is COMx,buadrate  , for ex will set COM3 port and baud rate 9600 to uart_Open,
    //parameter is "COM3,9600"
    sprintf(port, "%ls,%d", OpenPort, BuadRate);
    hPort = uart_Open(port);

    if(hPort == INVALID_HANDLE_VALUE)
    {
        ::MessageBox(NULL, L"Open Port Error", L"7k_dio_latch", MB_OK);
    }

    //get address
	GetDlgItemText(IDC_EDIT2, temp, 20);
    address = _wtoi(temp);

    //get do channel
	GetDlgItemText(IDC_EDIT12, strTemp);

    for(int i = 0; i < strTemp.GetAllocLength(); i++)
    {
        ctemp[i] = strTemp[i];
    }

    sscanf(ctemp, "%lx", &DOChannel);

    //get user set total channel
    GetDlgItemText(IDC_EDIT5, temp, 20);
    TotalDOchannel = _wtoi(temp);

    //use pac_Writedo function to write data
	iRet = pac_WriteDO(hPort, PAC_REMOTE_IO(address), TotalDOchannel, DOChannel);

    if(!iRet)
    {
        ::MessageBox(NULL, L"Write DO error", L"7k_dio", MB_OK);
    }
	uart_Close(hPort);
}
