// Card123Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Card123.h"
#include "Card123Dlg.h"
#include <stdlib.h>


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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCard123Dlg dialog

CCard123Dlg::CCard123Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCard123Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCard123Dlg)
	m_strStatus = _T("");
	m_strMsg = _T("");
	m_strCardNo = _T("");
	m_strValidData = _T("");
	m_strReceiveData = _T("");
	m_SetIP = _T("192.168.1.115");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCard123Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCard123Dlg)
	DDX_Control(pDX, IDC_CHECK1, m_chkCtrlTcpip);
	DDX_Control(pDX, IDC_BUTTON_HEX, m_btnCtrlHex);
	DDX_Control(pDX, IDC_BUTTON_ASCII, m_btnCtrlAscii);
	DDX_Control(pDX, IDC_COMBO_PROTOCOL, m_cboCtrlProtocol);
	DDX_Control(pDX, IDC_COMBO_BAUD, m_cboCtrlBaud);
	DDX_Control(pDX, IDC_COMBO_PORT, m_cboCtrlSetPort);
	DDX_Text(pDX, DC_STATIC_PORT_STATUS, m_strStatus);
	DDX_Text(pDX, IDC_STATIC_PRINT_MSG, m_strMsg);
	DDX_Text(pDX, IDC_STATIC_CARD_NO, m_strCardNo);
	DDX_Text(pDX, IDC_STATIC_VAILD_DATE, m_strValidData);
	DDX_Text(pDX, IDC_EDIT1, m_strReceiveData);
	DDX_Text(pDX, IDC_EDIT2, m_SetIP);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCard123Dlg, CDialog)
	//{{AFX_MSG_MAP(CCard123Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ASCII, OnButtonAscii)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_Track2, OnBUTTONTrack2)
	ON_BN_CLICKED(IDC_BUTTON_Track3, OnBUTTONTrack3)
	ON_BN_CLICKED(IDC_BUTTON_Track23, OnBUTTONTrack23)
	ON_BN_CLICKED(IDC_BUTTON_Cancel, OnBUTTONCancel)
	ON_BN_CLICKED(IDC_BUTTON_1DBAR, OnButton1dbar)
	ON_BN_CLICKED(IDC_BUTTON_BITMAP, OnButtonBitmap)
	ON_BN_CLICKED(IDC_BUTTON_Print, OnBUTTONPrint)
	ON_BN_CLICKED(IDC_BUTTON_HEX, OnButtonHex)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_2DBAR, OnButton2dbar)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
	
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCard123Dlg message handlers

BOOL CCard123Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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_cboCtrlBaud.SetCurSel(5);
	m_cboCtrlSetPort.SetCurSel(1);
	m_cboCtrlProtocol.SetCurSel(0);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCard123Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CCard123Dlg::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 CCard123Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CCard123Dlg::OnButtonOpen() 
{
	UpdateData(TRUE);
	m_strStatus = _T("");
	
	//
	ClosePrinter();
	
	
	int BaudRate;
	CString SetPort;

	switch (m_cboCtrlSetPort.GetCurSel())
	{
		
		case 0 : SetPort = _T("BSP2:");
			break;
		case 1 : SetPort = _T("COM1:");
			break;
		case 2 : SetPort = _T("COM2:");
			break;
		case 3 : SetPort = _T("COM3:");
			break;
		case 4 : SetPort = _T("COM4:");
			break;
		case 5 : SetPort = _T("COM5:");
			break;
		case 6 : SetPort = _T("COM6:"); 
			break;
		case 7 : SetPort = _T("COM7:");
			break;
		case 8 : SetPort = _T("COM8:");
			break;
			
		case 9 : SetPort = _T("COM9:");
			break;
		case 10 : SetPort = _T("COM10:");
			break;
	}
	switch (m_cboCtrlBaud.GetCurSel())
	{
		case 0 : BaudRate = 2400;
			break;
		case 1 : BaudRate = 4800;
			break;
		case 2 : BaudRate = 9600;
			break;
		case 3 : BaudRate = 19200;
			break;
		case 4 : BaudRate = 38400;
			break;
		case 5 : BaudRate = 57600; 
			break;
		case 6 : BaudRate = 115200; 
			break;
	}

	if(m_chkCtrlTcpip.GetCheck())
	{
		switch(m_cboCtrlProtocol.GetCurSel())
		{
			case 0 :
				iRet = InitWlanPrinter(m_SetIP.GetBuffer(m_SetIP.GetLength()),1470, 3000, FALSE);
				break;
			case 1 :
				iRet = InitWlanPrinter(m_SetIP.GetBuffer(m_SetIP.GetLength()),1470, 3000, FALSE);
				break;
		}

		switch (iRet)
		{
			
			case 1 : 
				m_strStatus = _T("Success");
				break;
			case -1 : 
				m_strStatus = _T("Socket Error");
				break;
				
			case -2 : 
				m_strStatus = _T("Connect Fail");
				break;
			case -3 : 
				m_strStatus =_T("Already Connected");
				break;
			case -5 : 
				m_strStatus =_T("No response from printer");
				break;	
			default :
				m_strStatus.Format(_T("%d"),iRet);
				break;		
		}

	}
	else
	{
		switch (m_cboCtrlProtocol.GetCurSel())
		{
			case 0 : 
				intReturnValue=InitPrinter(SetPort.GetBuffer(SetPort.GetLength())  , BaudRate,FALSE);
				break;
			case 1 :
				intReturnValue=InitPrinter(SetPort.GetBuffer(SetPort.GetLength()), BaudRate,TRUE);
				break;
		}
		
		
		
		switch (intReturnValue)
		{
			case 1 : 
				m_strStatus = _T("Success");
				break;
			case -1 : 
				m_strStatus = _T("Already opened");
				break;
			case -2 : 
				m_strStatus =_T("Unable to open the port");
				break;
			case -3 : 
				m_strStatus = _T("Unable to configure the serial port");
				break;
			case -4 : 
				m_strStatus =_T("Unable to set the timeout parameters");
				break;		
			case -5 : 
				m_strStatus =_T("No response from printer");
				break;		
		}

	}

	UpdateData(FALSE);
}

void CCard123Dlg::OnBUTTONTrack2() 
{
	// TODO: Add your control notification handler code here
	m_strMsg = _T("");
	m_strCardNo = _T("");
	m_strValidData = _T("");
	m_strReceiveData = _T("");
	m_btnCtrlHex.EnableWindow(FALSE);
	m_btnCtrlHex.ShowWindow(SW_HIDE);
	m_btnCtrlAscii.EnableWindow(FALSE);
	m_btnCtrlAscii.ShowWindow(SW_HIDE);	
	UpdateData(FALSE);
	CardRead(FALSE);
}

void CCard123Dlg::OnBUTTONTrack3() 
{
	// TODO: Add your control notification handler code here
	m_strMsg = _T("");
	m_strCardNo = _T("");
	m_strValidData = _T("");
	m_strReceiveData = _T("");
	m_btnCtrlHex.EnableWindow(FALSE);
	m_btnCtrlHex.ShowWindow(SW_HIDE);
	m_btnCtrlAscii.EnableWindow(FALSE);
	m_btnCtrlAscii.ShowWindow(SW_HIDE);	
	UpdateData(FALSE);
	CardRead(TRUE);
}

void CCard123Dlg::OnBUTTONTrack23() 
{
	// TODO: Add your control notification handler code here
	m_strMsg = _T("");
	m_strCardNo = _T("");
	m_strValidData = _T("");
	m_strReceiveData = _T("");
	m_btnCtrlHex.EnableWindow(FALSE);
	m_btnCtrlHex.ShowWindow(SW_HIDE);
	m_btnCtrlAscii.EnableWindow(FALSE);
	m_btnCtrlAscii.ShowWindow(SW_HIDE);	
	UpdateData(FALSE);
	BYTE Card23[]  = {0x1b,0x4d,0x45};
   	ControlCommand(Card23, 3);
	PrintSpool();

}

void CCard123Dlg::OnBUTTONCancel() 
{
	// TODO: Add your control notification handler code here
	m_strMsg = _T("");
	m_strCardNo = _T("");
	m_strValidData = _T("");
	m_strReceiveData = _T("");
	m_btnCtrlHex.EnableWindow(FALSE);
	m_btnCtrlHex.ShowWindow(SW_HIDE);
	m_btnCtrlAscii.EnableWindow(FALSE);
	m_btnCtrlAscii.ShowWindow(SW_HIDE);	
	UpdateData(FALSE);
	CardCancel();
	
}

void CCard123Dlg::OnButton1dbar() 
{
	// TODO: Add your control notification handler code here
	m_strMsg = _T("");
	UpdateData(FALSE);
	UpdateData();
	CString strTmp;
	
	////프린터 초기화 ESC @
	BYTE Print_init[]={0x1b,0x40};
	ControlCommand(Print_init, 2);
	

	//Barcode Print	
	BarcodeSaveSpool(UPCA, 2, 100, true, "12345678901", _2INCH, 200, 50);
	BarcodeSaveSpool(UPCE, 2, 100, true, "02345678901", _2INCH, 200, 50);
	BarcodeSaveSpool(EAN13, 2, 100, true, "12345678901", _2INCH, 200, 100);
	BarcodeSaveSpool(EAN8, 2, 100, true, "12345678", _2INCH, 200, 150);
	BarcodeSaveSpool(CODE39, 2, 100, true, "12345678901", _2INCH, 200, 200);
	BarcodeSaveSpool(ITF, 2, 100, true, "123456", _2INCH, 200, 250);
	BarcodeSaveSpool(CODABAR, 2, 100, true, "123456", _2INCH, 200, 0);
	BarcodeSaveSpool(CODE93, 2, 100, true, "12345678901", _2INCH, 200, 0);
	BarcodeSaveSpool(CODE128, 2, 100, true, "12345678901", _2INCH, 200, 0);
	
	PrintSpool();

	///////////////////////////////////////////////////////////////
	//Barcode command
	//
	/*

	//print 
	BYTE FF[]={0x0c};
	
	//바코드 높이 GS k
	BYTE GS_h[] = {0x1d, 0x68, 0x44};
	ControlCommand(GS_h,3);
	
	//바코드 너비 GS w
	BYTE GS_w[] = {0x1d, 0x77, 0x02};
	ControlCommand(GS_w,3);
	
	//바코드 문자 출력 GS H
	BYTE GS_H[] = {0x1d, 0x48, 0x01};
	
	
	SaveSpool(_T("UPC-A\r\n"),0);
	BYTE GS_k41[] = {0x1d, 0x6b, 0x41 , 0x0b, 0X30, 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x39, 0x30};
	ControlCommand(GS_k41,15);
	ControlCommand(GS_H,3);
	ControlCommand(FF,1);
	
	
	SaveSpool(_T("UPC-E\r\n"),0);	
	BYTE GS_k42[] = {0x1d, 0x6b, 0x42 , 0x0b, 0x30, 0X30, 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x39};
	ControlCommand(GS_k42,15);
	ControlCommand(GS_H,3);
	ControlCommand(FF,1);
	
	
	SaveSpool(_T("EAN13\r\n"),0);
	BYTE GS_k43[] = {0x1d, 0x6b, 0x43 , 0x0b, 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x39, 0x30, 0x31};
	ControlCommand(GS_k43,15);
	ControlCommand(GS_H,3);
	ControlCommand(FF,1);
	
	
	SaveSpool(_T("EAN8 Data 7\r\n"),0);
	BYTE GS_k44[] = {0x1d, 0x6b, 0x44 , 0x07, 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 ,0x37};
	ControlCommand(GS_k44,11);
	ControlCommand(GS_H,3);
	ControlCommand(FF,1);
	
	
	SaveSpool(_T("code39 Even\r\n"),0);
	BYTE GS_k45[] = {0x1d, 0x6b, 0x45 , 0x0b, 0x41, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x5a};
	ControlCommand(GS_k45,15);
	ControlCommand(GS_H,3);
	ControlCommand(FF,1);
	
	
	SaveSpool(_T("ITF\r\n"),0);
	BYTE GS_k46[] = {0x1d, 0x6b, 0x46 , 0x06, 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36};
	ControlCommand(GS_k46,10);
	ControlCommand(GS_H,3);
	ControlCommand(FF,1);
	
	
	SaveSpool(_T("codabar\r\n"),0);
	BYTE GS_k47[] = {0x1d, 0x6b, 0x47 , 0x0b, 0x41, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x44};
	ControlCommand(GS_k47,15);
	ControlCommand(GS_H,3);
	ControlCommand(FF,1);
	
	
	SaveSpool(_T("code93\r\n"),0);
	BYTE GS_k48[] = {0x1d, 0x6b, 0x48 , 0x0b, 0x21, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,0x7d};
	ControlCommand(GS_k48,15);
	ControlCommand(GS_H,3);
	ControlCommand(FF,1);
	
	PrintSpool();
*/
}

void CCard123Dlg::OnButtonBitmap() 
{
	// TODO: Add your control notification handler code here
	m_strMsg = _T("");
	UpdateData(FALSE);
	UpdateData();
	CString strTmp;
	
	//프린터 초기화
	BYTE Print_init[]={0x1b,0x40};
	ControlCommand(Print_init, 2);
	
	//페이지 모드
	BYTE Pagemode[]={0x1b,0x4c};
	ControlCommand(Pagemode, 2);
	
	//page area setting
	//1inch printer width : 192dot = 0xC0
	//2inch printer width : 384dot = 0x180
	//3inch printer width : 576dot = 0x240
	//4inch printer width : 832dot = 0x340
	
	
	//페이지 영역 설정
	//3인치 프린터 width : 576dot = 0x240
	//Page Size setting ESC W   384x176x2 bitmap
	// 1dot = 0.0125cm  
	// calculation : X : 0x180 => 384(decimal,2inch width)  
	//	              Y : 0xa7  => 167(decimal) => 167*0.0125 = 2.08cm
	BYTE Pagesize[]={0x1b,0x57, 0x00,0x00, 0x00,0x00, 0x80,0x01,0xa7,0x00};
	ControlCommand(Pagesize, 10);
	
	// 페이지 영역 초기화
	BYTE can[]={0x18};
	ControlCommand(can, 1);
	
	//비트맵 
	BmpSaveSpool(_T("\\woosim.bmp"),0,0);
	
	//페이지 영역 출력후 Standard Mode로 변환
	BYTE FF[]={0x0c};
	ControlCommand(FF, 1);
	
	
	PrintSpool();

}

void CCard123Dlg::OnBUTTONPrint() 
{
	// TODO: Add your control notification handler code here
	m_strMsg = _T("");
	UpdateData(FALSE);
	//print init
	BYTE init[]={0x1b,0x40};
	ControlCommand(init, 2);
	
	//Title
	SaveSpool("  SALES  SLIP\r\n", 3, TRUE);
	SaveSpool("--------------------------------\r\n", 0,FALSE );
	
	//Company
	SaveSpool("MERCHANT NAME      Woosim Coffee\r\n", 0, TRUE);
	
	//Reg Number
	SaveSpool("MASTER                LEE IL BOK\r\n", 0 ,FALSE);
	
	//Address
	SaveSpool("ADDRESS\r\n #501, DaerungTechnotown3th 448,\r\n  Gasan-dong, Geumchun-gu, Seoul\r\n  Korea\r\n", 0,0 );
	
	//Tel
	SaveSpool("HELP DESK       (+82-2)2107-3714\r\n", 0 ,FALSE);
	SaveSpool("--------------------------------\r\n\r\n", 0 ,FALSE);
	SaveSpool("        WOOSIM  CARD \r\n", 1 ,0);
	SaveSpool("--------------------------------\r\n", 0 ,0);
	
	//Card number
	SaveSpool("CARD NO.     9445 4103 9533 2096\r\n", 0 ,FALSE);
	
	SaveSpool("EXPIRY                 2012 / 10\r\n", 0 ,FALSE);
	SaveSpool("--------------------------------\r\n", 0,0 );
	
	SaveSpool("TRANS. YEAR & DATE    2011/04/11\r\n", 0 ,FALSE);
	
	SaveSpool("TRANS CLASS             [Credit]\r\n", 0 ,FALSE);
	SaveSpool("TERMINAL ID.           123456789\r\n", 0 ,FALSE);
	SaveSpool("APPROVAL NO.           987654321\r\n", 0 ,FALSE);
	SaveSpool("AMOUNT                   500.00$\r\n", 0 ,FALSE);
	
	SaveSpool("--------------------------------\r\n", 0 ,0);
	
	SaveSpool("SIGNATURE: \r\n\r\n\r\n\r\n\r\n", 0,0 );
	SaveSpool("--------------------------------\r\n", 0 ,0);
	
	PrintSpool();
}


void CCard123Dlg::OnButtonAscii() 
{
	// TODO: Add your control notification handler code here
	m_btnCtrlAscii.EnableWindow(FALSE);
	m_btnCtrlAscii.ShowWindow(SW_HIDE);
	m_btnCtrlHex.EnableWindow(TRUE);
	m_btnCtrlHex.ShowWindow(SW_SHOW);
	m_strReceiveData = strDataAscii;
	UpdateData(FALSE);
	
}

void CCard123Dlg::OnButtonHex() 
{
	// TODO: Add your control notification handler code here
	m_btnCtrlHex.EnableWindow(FALSE);
	m_btnCtrlHex.ShowWindow(SW_HIDE);
	m_btnCtrlAscii.EnableWindow(TRUE);
	m_btnCtrlAscii.ShowWindow(SW_SHOW);
	m_strReceiveData = strDataHex;
	
	UpdateData(FALSE);
	
}

LRESULT CCard123Dlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(message == ::RegisterWindowMessage(MSG_RECEIVE_CHECK))
	{
		switch (wParam)
		{
			
		case STATUS_ANSWER:

			m_strMsg = _T("STATUS_ANSWER");
			m_strReceiveData.Format(_T("H%02x"), (BYTE)lParam);
			break;
		case NACK:

			m_strMsg = _T("NACK");
			break;
		case EOT:

			m_strMsg = _T("EOT");
			break;
		case ETX:

			m_strMsg = _T("ETX");
			break;
		case MSR_FAIL:

			m_strMsg = _T("MSR_FAIL");
			break;
		case CAN:

			m_strMsg = _T("CAN");
			break;
			
		case DATA:

			CString data,temp;
			data = _T("");
			temp = _T("");
			strDataHex =  _T("");
			strDataAscii =  _T("");
			
			
			switch (m_cboCtrlProtocol.GetCurSel())
			{
			case 0:
				data      = (LPCSTR)(unsigned char*)lParam;
				m_strCardNo.Format(_T("%s %s %s %s"), data.Mid(4,4),data.Mid(8,4),data.Mid(12,4),data.Mid(16,4)); 
				m_strValidData.Format(_T("%s Month / %s Year"),data.Mid(23,2),data.Mid(21,2)); 
				m_strMsg = _T("DATA");
				break;
			case 1 :
				data      = (LPCSTR)(unsigned char*)lParam;
				m_strCardNo.Format(_T("%s %s %s %s"), data.Mid(1,4),data.Mid(5,4),data.Mid(9,4),data.Mid(13,4)); 
				m_strValidData.Format(_T("%s Month / %s Year"),data.Mid(20,2),data.Mid(18,2)); 
				m_strMsg = _T("DATA");
				break;
			}
			
			//Hex  
			strDataHex.Format(_T("Receive %dbyte"),data.GetLength() ); 
			for(int i=0; i < data.GetLength(); i++)
			{
				
				if(i%8==0)
				{	
					temp.Format(_T("%s\r\n"),strDataHex); 
					strDataHex = temp ;
				}
				temp.Format(_T("%sH%02x "),strDataHex,data[i]); 
				strDataHex = temp;
				
			}
			
			m_strReceiveData = strDataHex;
			
			//Ascii 
			strDataAscii.Format(_T("Receive %dbyte"),data.GetLength() ); 
			for(i=0; i < data.GetLength(); i++)
			{
				
				if(i%24==0)
				{	
					temp.Format(_T("%s\r\n"),strDataAscii); 
					strDataAscii = temp ;
				}
				temp.Format(_T("%s%c"),strDataAscii,data[i]); 
				strDataAscii = temp;
				
			}
			m_btnCtrlAscii.EnableWindow(TRUE);
			m_btnCtrlAscii.ShowWindow(SW_SHOW);
			m_btnCtrlHex.EnableWindow(FALSE);
			m_btnCtrlHex.ShowWindow(SW_HIDE);
			
			
			break;
			
		}
		UpdateData(FALSE);
		
	}
	return CDialog::DefWindowProc(message, wParam, lParam);
}

void CCard123Dlg::OnDestroy() 
{
	ClosePrinter();
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void CCard123Dlg::OnOK() 
{
	// TODO: Add extra validation here
	ClosePrinter();
	CDialog::OnOK();
}

void CCard123Dlg::OnButton2dbar() 
{
	// TODO: Add your control notification handler code here
	//Printer initialization ESC @
	BYTE Print_init[] ={0x1b,0x40};
	ControlCommand(Print_init, 2);
	
	//Barcode print 
	BYTE FF[] = {0x0c};
	
	SaveSpool("PDF417\r\n", 0, 0);
	
	//Select 2D barcode type GS Z  n (0 = PDF417 / 1 = DATAMATRIX / 2 = QR-CODE)
	BYTE GS_Z[] = {0x1d, 0x5a, 0x00};
	ControlCommand(GS_Z,3);
	
	BYTE GS_W[] = {0x1d, 0x77, 0x03};
	ControlCommand(GS_W,3);
	
	
	//Print 2D barcode
	BYTE ESC_Z[] = {0x1b, 0x5a,
					0x01 , 0x00, 0X03, 0x0f, 0x00, 
					0x41,0x42,0x43,0x44,0x45,0x31,0x32,0x33,0x34,0x35,0x61,0x62,0x63,0x64,0x65};
	ControlCommand(ESC_Z,22);
	
	ControlCommand(FF,1);
	
	BYTE LF[]={0x0a};
	ControlCommand(LF, 1);
		
	SaveSpool("DATAMAREIX\r\n", 0, 0);
	//Select 2D barcode type GS Z  n (0 = PDF417 / 1 = DATAMATRIX / 2 = QR-CODE)
	BYTE GS_Z3[] = { 0x1d, 0x5a, 0x01 };
	ControlCommand(GS_Z3, 3);
	
	
	//Print 2D barcode
	BYTE ESC_Z1[] = {0x1b, 0x5a,
					 0x00 , 0x00, 0X03, 0x0f, 0x00, 
					 0x41,0x42,0x43,0x44,0x45,0x31,0x32,0x33,0x34,0x35,0x61,0x62,0x63,0x64,0x65};
	ControlCommand(ESC_Z1, 22);
	
	ControlCommand(LF, 1);
	
	SaveSpool("QR-CODE\r\n", 0, 0);
	//Select 2D barcode type GS Z  n (0 = PDF417 / 1 = DATAMATRIX / 2 = QR-CODE)
	BYTE GS_Z4[] = { 0x1d, 0x5a, 0x02 };
	ControlCommand(GS_Z4, 3);
	
	
	//Print 2D barcode
	BYTE ESC_Z2[] = {0x1b, 0x5a,
					 0x00 , 0x4d, 0X03, 0x0f, 0x00, 
					 0x41,0x42,0x43,0x44,0x45,0x31,0x32,0x33,0x34,0x35,0x61,0x62,0x63,0x64,0x65};
	ControlCommand(ESC_Z2, 22);
	
	ControlCommand(LF, 1);
	PrintSpool(1);
        
}

void CCard123Dlg::OnButtonClose() 
{
	// TODO: Add your control notification handler code here
	m_strStatus = _T("Close Printer");
	UpdateData(FALSE);
	ClosePrinter();
}
