// serialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "serial.h"
#include "serialDlg.h"
#include "EZSerial.cpp"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSerialDlg dialog

CSerialDlg::CSerialDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSerialDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSerialDlg)
	m_TX_buffer = _T("");
	m_RX_buffer = _T("");
	m_len_TX = 0;
	m_len_RX = 0;
	m_Time = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSerialDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerialDlg)
	DDX_Text(pDX, IDC_EDIT1, m_TX_buffer);
	DDX_Text(pDX, IDC_EDIT2, m_RX_buffer);
	DDX_Text(pDX, IDC_EDIT4, m_len_TX);
	DDX_Text(pDX, IDC_EDIT5, m_len_RX);
	DDX_Text(pDX, IDC_EDIT6, m_Time);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSerialDlg, CDialog)
	//{{AFX_MSG_MAP(CSerialDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnRX)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerialDlg message handlers

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

	m_TX_buffer = ("enter text here");
	m_RX_buffer = ("               ");
	m_len_TX = 16;
	m_len_RX=16;
	m_Time = 1000;
	UpdateData(FALSE);

	
	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 CSerialDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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
	{
		CDialog::OnPaint();
	}
}

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

void CSerialDlg::OnOK() 
{
	// TODO: Add extra validation here
	

// variables used with the com port
BOOL     m_bPortReady;
HANDLE   m_hCom;
CString  m_sComPort;
DCB      m_dcb;
COMMTIMEOUTS m_CommTimeouts;
BOOL     bWriteRC;
BOOL     bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
char       sBuffer[128];

m_sComPort = "Com1";
m_hCom = CreateFile(m_sComPort, 
		GENERIC_READ | GENERIC_WRITE,
		0, // exclusive access
		NULL, // no security
		OPEN_EXISTING,
		0, // no overlapped I/O
		NULL); // null template 


m_bPortReady = SetupComm(m_hCom, 128, 128); // set buffer sizes

m_bPortReady = GetCommState(m_hCom, &m_dcb);
m_dcb.BaudRate = 9600;
m_dcb.ByteSize = 8;
m_dcb.Parity = NOPARITY;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.fAbortOnError = TRUE;

m_bPortReady = SetCommState(m_hCom, &m_dcb);

m_bPortReady = GetCommTimeouts (m_hCom, &m_CommTimeouts);

m_CommTimeouts.ReadIntervalTimeout = 50;
m_CommTimeouts.ReadTotalTimeoutConstant = 50;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 10;
m_CommTimeouts.WriteTotalTimeoutConstant = 50;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 10;

m_bPortReady = SetCommTimeouts (m_hCom, &m_CommTimeouts);
UpdateData(TRUE);

bWriteRC = WriteFile(m_hCom,m_TX_buffer ,m_len_TX,&iBytesWritten,NULL);

CloseHandle(m_hCom);

	//CDialog::OnOK();
}









void CSerialDlg::OnRX() 
{
	// TODO: Add your control notification handler code here




// variables used with the com port
BOOL     m_bPortReady;
HANDLE   m_hCom;
CString  m_sComPort;
DCB      m_dcb;
COMMTIMEOUTS m_CommTimeouts;
BOOL     bWriteRC;
BOOL     bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
char       sBuffer[128];

m_sComPort = "Com1";
m_hCom = CreateFile(m_sComPort, 
		GENERIC_READ | GENERIC_WRITE,
		0, // exclusive access
		NULL, // no security
		OPEN_EXISTING,
		0, // no overlapped I/O
		NULL); // null template 


m_bPortReady = SetupComm(m_hCom, 128, 128); // set buffer sizes

m_bPortReady = GetCommState(m_hCom, &m_dcb);
m_dcb.BaudRate = 9600;
m_dcb.ByteSize = 8;
m_dcb.Parity = NOPARITY;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.fAbortOnError = TRUE;

m_bPortReady = SetCommState(m_hCom, &m_dcb);

m_bPortReady = GetCommTimeouts (m_hCom, &m_CommTimeouts);

m_CommTimeouts.ReadIntervalTimeout = 50;
m_CommTimeouts.ReadTotalTimeoutConstant = 50;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 10;
m_CommTimeouts.WriteTotalTimeoutConstant = 50;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 10;

m_bPortReady = SetCommTimeouts (m_hCom, &m_CommTimeouts);

UpdateData(TRUE);
ZeroMemory(sBuffer,127);
Sleep(m_Time);

bReadRC = ReadFile(m_hCom, &sBuffer, m_len_RX, &iBytesRead, NULL);
m_RX_buffer = sBuffer;
UpdateData(FALSE);
CloseHandle(m_hCom);


	

	
}

void CSerialDlg::OnListen() 
{
	// TODO: Add your control notification handler code here
	
}
