// SampleApp_VC++_MFC.cpp : Definiert das Klassenverhalten für die Anwendung.
//

#include "stdafx.h"
#include "SampleApp_VC++_MFC.h"
#include "MainFrm.h"
#include "Reader_Interface.h"

#include "ChildFrm.h"
#include "SampleApp_VC++_MFCDoc.h"
#include "SampleApp_VC++_MFCView.h"
#include ".\sampleapp_vc++_mfc.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#endif

	BOOL	SEARCH;

	BYTE	Identifier[33];					// Maximum length of identifier (32 bytes + 1 length-byte)
	BYTE	PortType = 4;					// Set port type:	0 = standard serial
											//					1 = CompactFlash by elserial (only for PPC 2000 systems)
											//					2 = Bluetooth SPP
											//					4 = USB port (Win32 systems only)
	LPTSTR	lpszPortName = TEXT("USB:");	// and serial port name, or any port name for CF/USB
	INT32	InterfaceType = 868;			// Set Interface_Type:	125 = 125kHz
											//						1356 = 13.56MHz
											//						868 = 868 MHz
											//						0x10 = PEN-BT with memory support (125kHz/13.56MHz)
	INT32	ProtocolType = 0x4;				// Set ProtocolType:	0x03 = iID®-3000 protocol
											//						0x04 = iID® interface protocol V4
											//						0x1001 = CAEN
											//						0x1002 = LEGIC
	INT32	TimeOut = 250;					// Set TimeOut for iIDDriver functions identify, readbytes and writebytes (10...4999msec)
	INT32	SystemMask = 0xFF;				// Set supported systems for this demo (0x400000FFF -> enable all transponder systems)
											//		ISO15693			= 0x01
											//		iID®-L				= 0x02
											//		iID®-D				= 0x04
											//		iID®G				= 0x08
											//		ARIO/FDX-B			= 0x10
											//		I-Code UID			= 0x20
											//		LEGIC® UID			= 0x40
											//		ISO14443A			= 0x100
											//		ISO14443B			= 0x800
											//		LEGIC® file system	= 0x40000000
	INT32	TagType,						// Transponder type information
			TagMaxLength,					// Maximum R/W accessible transponder memory (in bytes)
			TagSystem,						// Transponder system information
			TagXtraParam;					// RFU
	INT32	From, Length;					// ReadBytes() and WriteBytes() need these parameters	
	BYTE	Lock = 0;						// Lock Blocks after WriteBytes() ?
	INT32	main_ver, sub_ver;				// Driver version bytes

	HANDLE m_HCom;							


	HMENU m_hMenu;
	
	//Forward declarations
	bool InitializePort();
	bool ClosePort();
	bool ReadInterfaceID();
	bool SetAutoOff();
	void Read();
	void Write();
	void Reader_Enable_PC_EPC(bool bEnable);

	void EPCLock(UINT32 payload);
	void EPCLockSecure(UINT32 payload);

	BYTE ISO18000_ReadWords(BYTE epc[], BYTE EPC_length, BYTE data[], UINT32 bank, UINT32 address, UINT32 length);
	BYTE ISO18000_WriteWords(BYTE epc[], BYTE EPC_length, BYTE data[], UINT32 bank, UINT32 address, UINT32 length);
	BYTE ISO18000_ReadWords_Secure(BYTE epc[], BYTE EPC_length, BYTE data[], UINT16 bank, UINT16 address, UINT16 length, BYTE password[]);
	BYTE ISO18000_WriteWords_Secure(BYTE epc[], BYTE EPC_length, BYTE data[], UINT32 bank, UINT32 address, UINT32 length, BYTE password[]);

	BYTE Iso18000_Identify_RSSI(CStringArray *pUIIs);

	BYTE ISO18000_WriteEPC(BYTE old_epc[], BYTE old_length, BYTE new_pc[], BYTE new_epc[], BYTE epc_length, BYTE password[]);
	BYTE ISO18000_lock(BYTE epc[], BYTE EPC_length, UINT32 payload);
	BYTE ISO18000_lock_secure(BYTE epc[], BYTE EPC_length, UINT32 payload, BYTE password[]);


// CSampleApp_VC_MFCApp

BEGIN_MESSAGE_MAP(CSampleApp_VC_MFCApp, CWinApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	// Dateibasierte Standarddokumentbefehle
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)

	// Standarddruckbefehl "Seite einrichten"
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
	ON_COMMAND(ID_PORT_INITIALIZEPORT, OnPortInitializeport)
	ON_COMMAND(ID_PORT_CLOSEPORT, OnPortCloseport)
	ON_COMMAND(ID_READER_SETAUTO, OnReaderSetauto)
	ON_COMMAND(ID_READER_READINTERFACE, OnReaderReadinterface)

	ON_COMMAND(ID_PORTTYPE_SERIAL, OnPortTypeSerial)
	ON_COMMAND(ID_PORTTYPE_BLUESPP, OnPortTypeBluetoothSPP)
	ON_COMMAND(ID_PORTTYPE_CF, OnPortTypeCompactFlash)
	ON_COMMAND(ID_PORTTYPE_USB, OnPortTypeUSB)

	ON_COMMAND(ID_PORTNAME_COM1, OnPortNameCOM1)
	ON_COMMAND(ID_PORTNAME_COM2, OnPortNameCOM2)
	ON_COMMAND(ID_PORTNAME_COM3, OnPortNameCOM3)
	ON_COMMAND(ID_PORTNAME_COM4, OnPortNameCOM4)
	ON_COMMAND(ID_PORTNAME_COM5, OnPortNameCOM5)
	ON_COMMAND(ID_PORTNAME_COM6, OnPortNameCOM6)
	ON_COMMAND(ID_PORTNAME_COM7, OnPortNameCOM7)
	ON_COMMAND(ID_PORTNAME_COM8, OnPortNameCOM8)
	ON_COMMAND(ID_PORTNAME_COM9, OnPortNameCOM9)
	ON_COMMAND(ID_PORTNAME_COM10, OnPortNameCOM10)
	ON_COMMAND(ID_PORTNAME_COM11, OnPortNameCOM11)
	ON_COMMAND(ID_PORTNAME_COM12, OnPortNameCOM12)
	ON_COMMAND(ID_PORTNAME_COM13, OnPortNameCOM13)
	ON_COMMAND(ID_PORTNAME_COM14, OnPortNameCOM14)
	ON_COMMAND(ID_PORTNAME_USB, OnPortNameUSB)

	ON_COMMAND(ID_INTERFACETYPE_125, OnInterfaceType125K)
	ON_COMMAND(ID_INTERFACETYPE_1356, OnInterfaceType13M56)
	ON_COMMAND(ID_INTERFACETYPE_868, OnInterfaceType868M)
	ON_COMMAND(ID_PROTOCOLTYPE_3000, OnProtocolTypeIID3000)
	ON_COMMAND(ID_PROTOCOLTYPE_V4, OnProtocolTypeIIDv4)
	ON_COMMAND(ID_PROTOCOLTYPE_LEGIC, OnProtocolTypeLEGIC)
	ON_COMMAND(ID_PROTOCOLTYPE_CAEN, OnProtocolTypeCAEN)

	ON_COMMAND(ID_MASK_ISO15693, OnSystemMaskISO15693)
	ON_COMMAND(ID_MASK_IIDL, OnSystemMaskIIDL)
	ON_COMMAND(ID_MASK_IIDD, OnSystemMaskIIDD)
	ON_COMMAND(ID_MASK_IIDG, OnSystemMaskIIDG)
	ON_COMMAND(ID_MASK_ARIORO, OnSystemMaskARIORO)
	ON_COMMAND(ID_MASK_ICODE_UID, OnSystemMaskICODEUID)
	ON_COMMAND(ID_MASK_LEGIC_UID, OnSystemMaskLEGICUID)
	ON_COMMAND(ID_MASK_ISO14443A, OnSystemMaskISO14443A)
	ON_COMMAND(ID_MASK_ISO14443B, OnSystemMaskISO14443B)
	ON_COMMAND(ID_MASK_LEGIC_FS, OnSystemMaskLEGICFS)
	ON_COMMAND(ID_MASK_SETALL, OnSystemMaskSetAll)
	ON_COMMAND(ID_MASK_CLEARALL, OnSystemMaskClearAll)

	ON_COMMAND(ID_DRV_IDENTIFY, OnDriverIdentify)
	ON_COMMAND(ID_DRV_READTEXT, OnDriverReadText)
	ON_COMMAND(ID_DRV_WRITETEXT, OnDriverWriteText)
	ON_COMMAND(ID_DRV_READ, OnDriverRead)
	ON_COMMAND(ID_DRV_WRITE, OnDriverWrite)

	ON_COMMAND(ID_ISO18000_READ_EPC, OnIso18000Identify)
	ON_COMMAND(ID_ISO18000_READ, OnIso18000Read)
	ON_COMMAND(ID_ISO18000_WRITE, OnIso18000Write)

	ON_COMMAND(ID_ISO18000_READ_UIIS, OnIso18000ReadUIIs)
	ON_COMMAND(ID_ISO18000_READ_UIIS_RSSI, OnIso18000ReadUIIsRSSI)
	ON_COMMAND(ID_ISO18000_GET_AFI, OnIso18000GetAFI)
	ON_COMMAND(ID_ISO18000_SET_AFI, OnIso18000SetAFI)
	
	ON_COMMAND(ID_ISO18000_READ_SECURE, OnIso18000ReadSecure)
	ON_COMMAND(ID_ISO18000_WRITE_SECURE, OnIso18000WriteSecure)

	ON_COMMAND(ID_ISO18000_WRITE_EPC, OnIso18000WriteEpc)
	ON_COMMAND(ID_ISO18000_LOCK_EPC, OnLockEpc)
	ON_COMMAND(ID_ISO18000_UNLOCK_EPC, OnUnlockEpc)
	ON_COMMAND(ID_ISO18000_LOCK_EPC_SECURE, OnLockEpcSecure)
	ON_COMMAND(ID_ISO18000_UNLOCK_EPC_SECURE, OnUnlockEpcSecure)
END_MESSAGE_MAP()


// CSampleApp_VC_MFCApp-Erstellung

CSampleApp_VC_MFCApp::CSampleApp_VC_MFCApp()
{
	// TODO: Hier Code zur Konstruktion einfügen
	// Alle wichtigen Initialisierungen in InitInstance positionieren
}


// Das einzige CSampleApp_VC_MFCApp-Objekt

CSampleApp_VC_MFCApp theApp;

// CSampleApp_VC_MFCApp Initialisierung

BOOL CSampleApp_VC_MFCApp::InitInstance()
{
	// InitCommonControls() ist für Windows XP erforderlich, wenn ein Anwendungsmanifest
	// die Verwendung von ComCtl32.dll Version 6 oder höher zum Aktivieren
	// von visuellen Stilen angibt. Ansonsten treten beim Erstellen von Fenstern Fehler auf.
	InitCommonControls();

	CWinApp::InitInstance();

	// OLE-Bibliotheken initialisieren
	if (!AfxOleInit())
	{
		AfxMessageBox(IDP_OLE_INIT_FAILED);
		return FALSE;
	}
	AfxEnableControlContainer();
	// Standardinitialisierung
	// Wenn Sie diese Features nicht verwenden und die Größe
	// der ausführbaren Datei verringern möchten, entfernen Sie
	// die nicht erforderlichen Initialisierungsroutinen.
	// Ändern Sie den Registrierungsschlüssel unter dem Ihre Einstellungen gespeichert sind.
	// TODO: Ändern Sie diese Zeichenfolge entsprechend,
	// z.B. zum Namen Ihrer Firma oder Organisation.
	SetRegistryKey(_T("Vom lokalen Anwendungs-Assistenten generierte Anwendungen"));
	LoadStdProfileSettings(4);  // Standard INI-Dateioptionen laden (einschließlich MRU)
	// Dokumentvorlagen der Anwendung registrieren. Dokumentvorlagen
	//  dienen als Verbindung zwischen Dokumenten, Rahmenfenstern und Ansichten.
	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(IDR_SampleApp_VC_MFTYPE,
		RUNTIME_CLASS(CSampleApp_VC_MFCDoc),
		RUNTIME_CLASS(CChildFrame), // Benutzerspezifischer MDI-Child-Rahmen
		RUNTIME_CLASS(CSampleApp_VC_MFCView));
	if (!pDocTemplate)
		return FALSE;
	//AddDocTemplate(pDocTemplate);
	// Haupt-MDI-Rahmenfenster erstellen
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;
	// Rufen Sie DragAcceptFiles nur auf, wenn eine Suffix vorhanden ist.
	//  In einer MDI-Anwendung ist dies unmittelbar nach dem Festlegen von m_pMainWnd erforderlich
	// Befehlszeile parsen, um zu prüfen auf Standardumgebungsbefehle DDE, Datei offen
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	// Verteilung der in der Befehlszeile angegebenen Befehle. Es wird FALSE zurückgegeben, wenn
	// die Anwendung mit /RegServer, /Register, /Unregserver oder /Unregister gestartet wurde.
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;
	// Das Hauptfenster ist initialisiert und kann jetzt angezeigt und aktualisiert werden.
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	if (!Init_Driver(TEXT("iiddrv30_pro.dll"))) 
	{
		MessageBox(NULL,TEXT("Error loading iiddrv30_pro.dll!"),TEXT("DRIVER"),MB_OK);
		return FALSE;
	}

	m_hMenu = AfxGetApp()->GetMainWnd()->GetMenu()->GetSafeHmenu();

	Update_Settings();
	return TRUE;
}

// CAboutDlg-Dialogfeld für Anwendungsbefehl 'Info'

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

// Dialogfelddaten
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV-Unterstützung

// Implementierung
protected:
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// Anwendungsbefehl zum Ausführen des Dialogfelds
void CSampleApp_VC_MFCApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}


//------------------------------------------------------
//------------------------------------------------------
//------------------------------------------------------


void CSampleApp_VC_MFCApp::OnPortTypeSerial()
{
	CheckMenuItem(m_hMenu, ID_PORTTYPE_SERIAL, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_BLUESPP, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_CF, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_USB, MF_BYCOMMAND | MF_UNCHECKED);
	PortType = 0;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortTypeBluetoothSPP()
{
	CheckMenuItem(m_hMenu, ID_PORTTYPE_SERIAL, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_BLUESPP, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_CF, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_USB, MF_BYCOMMAND | MF_UNCHECKED);
	PortType = 2;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortTypeCompactFlash()
{
	CheckMenuItem(m_hMenu, ID_PORTTYPE_SERIAL, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_BLUESPP, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_CF, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_USB, MF_BYCOMMAND | MF_UNCHECKED);
	PortType = 1;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortTypeUSB()
{
	CheckMenuItem(m_hMenu, ID_PORTTYPE_SERIAL, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_BLUESPP, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_CF, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_USB, MF_BYCOMMAND | MF_CHECKED);
	PortType = 4;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnInterfaceType125K()
{
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_125, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_1356, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_868, MF_BYCOMMAND | MF_UNCHECKED);
	InterfaceType = 125;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnInterfaceType13M56()
{
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_125, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_1356, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_868, MF_BYCOMMAND | MF_UNCHECKED);
	InterfaceType = 1356;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnInterfaceType868M()
{
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_125, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_1356, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_868, MF_BYCOMMAND | MF_CHECKED);
	InterfaceType = 868;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnProtocolTypeIID3000()
{
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_3000, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_V4, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_LEGIC, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_CAEN, MF_BYCOMMAND | MF_UNCHECKED);
	ProtocolType = 0x03;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnProtocolTypeIIDv4()
{
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_3000, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_V4, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_LEGIC, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_CAEN, MF_BYCOMMAND | MF_UNCHECKED);
	ProtocolType = 0x04;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnProtocolTypeLEGIC()
{
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_3000, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_V4, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_LEGIC, MF_BYCOMMAND | MF_CHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_CAEN, MF_BYCOMMAND | MF_UNCHECKED);
	ProtocolType = 0x1002;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnProtocolTypeCAEN()
{
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_3000, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_V4, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_LEGIC, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_CAEN, MF_BYCOMMAND | MF_CHECKED);
	ProtocolType = 0x1001;
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM1()
{
	UncheckMenus();
	lpszPortName = _T("COM1:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM1, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM2()
{
	UncheckMenus();
	lpszPortName = _T("COM2:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM2, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM3()
{
	UncheckMenus();
	lpszPortName = _T("COM3:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM3, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM4()
{
	UncheckMenus();
	lpszPortName = _T("COM4:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM4, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM5()
{
	UncheckMenus();
	lpszPortName = _T("COM5:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM5, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM6()
{
	UncheckMenus();
	lpszPortName = _T("COM6:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM6, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM7()
{
	UncheckMenus();
	lpszPortName = _T("COM7:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM7, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM8()
{
	UncheckMenus();
	lpszPortName = _T("COM8:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM8, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM9()
{
	UncheckMenus();
	lpszPortName = _T("COM9:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM9, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM10()
{
	UncheckMenus();
	lpszPortName = _T("COM10:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM10, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM11()
{
	UncheckMenus();
	lpszPortName = _T("COM11:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM11, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM12()
{
	UncheckMenus();
	lpszPortName = _T("COM12:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM12, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM13()
{
	UncheckMenus();
	lpszPortName = _T("COM13:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM13, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameCOM14()
{
	UncheckMenus();
	lpszPortName = _T("COM14:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM14, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}
void CSampleApp_VC_MFCApp::OnPortNameUSB()
{
	UncheckMenus();
	lpszPortName = _T("USB:");
	CheckMenuItem(m_hMenu, ID_PORTNAME_USB, MF_BYCOMMAND | MF_CHECKED);
	Update_Settings();
}

void CSampleApp_VC_MFCApp::UncheckMenus()
{
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM1, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM2, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM3, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM4, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM5, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM6, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM7, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM8, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM9, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM10, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM11, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM12, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM13, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_COM14, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTNAME_USB, MF_BYCOMMAND | MF_UNCHECKED);
}
void CSampleApp_VC_MFCApp::Update_Settings()
{
	UncheckMenus();
	if (lpszPortName == _T("COM1:"))	{
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM1, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM2:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM2, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM3:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM3, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM4:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM4, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM5:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM5, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName== _T("COM6:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM6, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM7:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM7, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM8:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM8, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM9:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM9, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM10:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM10, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM11:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM11, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM12:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM12, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM13:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM13, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("COM14:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_COM14, MF_BYCOMMAND | MF_CHECKED);
	} else if (lpszPortName == _T("USB:")) {
		CheckMenuItem(m_hMenu, ID_PORTNAME_USB, MF_BYCOMMAND | MF_CHECKED);
	}

	CheckMenuItem(m_hMenu, ID_PORTTYPE_SERIAL, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_BLUESPP, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_CF, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PORTTYPE_USB, MF_BYCOMMAND | MF_UNCHECKED);
	switch (PortType)
	{
		case 0:	CheckMenuItem(m_hMenu, ID_PORTTYPE_SERIAL, MF_BYCOMMAND | MF_CHECKED); break;
		case 1: CheckMenuItem(m_hMenu, ID_PORTTYPE_CF, MF_BYCOMMAND | MF_CHECKED); break;
		case 2: CheckMenuItem(m_hMenu, ID_PORTTYPE_BLUESPP, MF_BYCOMMAND | MF_CHECKED); break;
		case 4: CheckMenuItem(m_hMenu, ID_PORTTYPE_USB, MF_BYCOMMAND | MF_CHECKED); break;
	}

	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_125, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_1356, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_INTERFACETYPE_868, MF_BYCOMMAND | MF_UNCHECKED);
	switch (InterfaceType)
	{
		case 125: CheckMenuItem(m_hMenu, ID_INTERFACETYPE_125, MF_BYCOMMAND | MF_CHECKED); break;
		case 1356: CheckMenuItem(m_hMenu, ID_INTERFACETYPE_1356, MF_BYCOMMAND | MF_CHECKED); break;
		case 868: CheckMenuItem(m_hMenu, ID_INTERFACETYPE_868, MF_BYCOMMAND | MF_CHECKED); break;
	}

	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_3000, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_V4, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_LEGIC, MF_BYCOMMAND | MF_UNCHECKED);
	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_CAEN, MF_BYCOMMAND | MF_UNCHECKED);
	switch (ProtocolType)
	{
		case 3:	CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_3000, MF_BYCOMMAND | MF_CHECKED); break;
		case 4: CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_V4, MF_BYCOMMAND | MF_CHECKED); break;
		case 0x1001: CheckMenuItem(m_hMenu, ID_PROTOCOLTYPE_CAEN, MF_BYCOMMAND | MF_CHECKED); break;
	}

	if (GetSubMenu(m_hMenu, 4) != NULL)
	{
		if ((ProtocolType != 0x4) || (InterfaceType != 868))
		{
			EnableMenuItem(m_hMenu, 5, MF_BYPOSITION | MF_GRAYED);
		}
		else
		{
			EnableMenuItem(m_hMenu, 5, MF_BYPOSITION | MF_ENABLED);
		}
		DrawMenuBar(AfxGetApp()->GetMainWnd()->GetSafeHwnd());
	}

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskISO15693()
{
	if (SystemMask & 0x01) 
		SystemMask &= 0xFFFFFFFE;
	else
		SystemMask |= 0x01;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskIIDL()
{
	if (SystemMask & 0x02) 
		SystemMask &= 0xFFFFFFFD;
	else
		SystemMask |= 0x02;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskIIDD()
{
	if (SystemMask & 0x04) 
		SystemMask &= 0xFFFFFFFB;
	else
		SystemMask |= 0x04;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskIIDG()
{
	if (SystemMask & 0x08) 
		SystemMask &= 0xFFFFFFF7;
	else
		SystemMask |= 0x08;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskARIORO()
{
	if (SystemMask & 0x10) 
		SystemMask &= 0xFFFFFFEF;
	else
		SystemMask |= 0x10;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskICODEUID()
{
	if (SystemMask & 0x20) 
		SystemMask &= 0xFFFFFFDF;
	else
		SystemMask |= 0x20;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskLEGICUID()
{
	if (SystemMask & 0x40) 
		SystemMask &= 0xFFFFFFBF;
	else
		SystemMask |= 0x40;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskISO14443A()
{
	if (SystemMask & 0x100) 
		SystemMask &= 0xFFFFFEFF;
	else
		SystemMask |= 0x100;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskISO14443B()
{
	if (SystemMask & 0x800) 
		SystemMask &= 0xFFFFF7FF;
	else
		SystemMask |= 0x800;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskLEGICFS()
{
	if (SystemMask & 0x40000000) 
		SystemMask &= 0xBFFFFFFF;
	else
		SystemMask |= 0x40000000;

	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskSetAll()
{
	SystemMask = GROUP_LEGICFS | GROUP_DEFAULT;
	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::OnSystemMaskClearAll()
{
	SystemMask = 0;
	UpdateSystemMask();
}
void CSampleApp_VC_MFCApp::UpdateSystemMask()
{
	CheckMenuItem(m_hMenu, ID_MASK_ISO15693, MF_BYCOMMAND | ((SystemMask & 0x01) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_IIDL, MF_BYCOMMAND | ((SystemMask & 0x02) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_IIDD, MF_BYCOMMAND | ((SystemMask & 0x04) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_IIDG, MF_BYCOMMAND | ((SystemMask & 0x08) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_ARIORO, MF_BYCOMMAND | ((SystemMask & 0x10) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_ICODE_UID, MF_BYCOMMAND | ((SystemMask & 0x20) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_LEGIC_UID, MF_BYCOMMAND | ((SystemMask & 0x80) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_ISO14443A, MF_BYCOMMAND | ((SystemMask & 0x100) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_ISO14443B, MF_BYCOMMAND | ((SystemMask & 0x800) ? MF_CHECKED : MF_UNCHECKED)); 
	CheckMenuItem(m_hMenu, ID_MASK_LEGIC_FS, MF_BYCOMMAND | ((SystemMask & 0x40000000) ? MF_CHECKED : MF_UNCHECKED)); 
}

//------------------------------------------------------
//------------------------------------------------------
//------------------------------------------------------


/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Dynamic load of the driver functions and first setting of -
 * -port and driver parameters---------------------------------
 *-------------------------------------------------------------*/
bool InitializePort()
{
	// Check driver version
	PROC_driver_get_driver_version(&main_ver, &sub_ver);
	if ((main_ver<0x10) || (sub_ver<0xB))
	{
		MessageBox(NULL,TEXT("Wrong DLL version !"),TEXT("DRIVER"),MB_OK);
		return FALSE;
	}
	else if (sub_ver < 0x50)
	{
		MessageBox(NULL, _T("Driver version 0x10.0x50 recommended in order to perform extended ISO18000-6 functionality"), _T("Information"), MB_OK);
	}
	
	// Set port type
	// and serial port name
	if (PROC_reader_set_port_type(PortType,lpszPortName)!=PortType) 
	{
		MessageBox(NULL,TEXT("ERROR setting parameters !"),TEXT("OPEN INTERFACE"),MB_OK);
		return FALSE;
	}
	// and interface type
	if (PROC_reader_set_interface_type(InterfaceType)!=0)
	{
		MessageBox(NULL,TEXT("ERROR setting parameters !"),TEXT("OPEN INTERFACE"),MB_OK);
		return FALSE;
	}
	// and protocol type
	if (PROC_reader_set_protocol_type(ProtocolType) != 0)
	{
		MessageBox(NULL, _T("ERROR setting parameters !"), _T("OPEN INTERFACE"), MB_OK);
		return FALSE;
	}
	// and timeout
	if (PROC_driver_set_timeout(TimeOut)!=0)
	{
		MessageBox(NULL,TEXT("ERROR setting parameters !"),TEXT("OPEN INTERFACE"),MB_OK);
		return FALSE;
	}

    // Set system mask for iID® driver mode
	if (PROC_driver_set_system_mask(SystemMask)!=0) 
	{
		MessageBox(NULL,TEXT("ERROR setting parameters !"),TEXT("OPEN INTERFACE"),MB_OK);
		return FALSE;
	}

	// Now open the specified interface using 'c_initialize'
	if (PROC_initialize()!=0)
	{
		MessageBox(NULL,TEXT("ERROR during 'Open Interface' !"),TEXT("OPEN INTERFACE"),MB_OK);
		return FALSE;
	}

	// Retrieve port handle
	m_HCom=PROC_reader_get_handle();
	MessageBox(NULL,TEXT("Interface initialized !"),TEXT("OPEN INTERFACE"),MB_OK);
	return TRUE;
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Closes interface-connection and device handle.-------------
 * -Device is set into power-optimized mode--------------------
 *-------------------------------------------------------------*/
bool ClosePort()
{
	// Terminate reader communication
	PROC_terminate();
	MessageBox(NULL,TEXT("Interface communication terminated !"),TEXT("CLOSE INTERFACE"),MB_OK);
	return TRUE;
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Reads ID-number and parameters from RFID interface with----
 * -iID®3000 protocol frame------------------------------------
 *-------------------------------------------------------------*/
bool ReadInterfaceID()
{
	CString Out,Out2;
	int id;
	
	// Read reader's Identifier
	int MSS_Result=PROC_read_reader_id(&id,&ReadArray[0]);
	if (MSS_Result!=0)
	{
		Out.Format("Error: 0x%02X reading Interface-ID",MSS_Result);
		MessageBox(NULL,Out,TEXT("READ INTERFACE-ID"),MB_OK);
		return FALSE;
	}
	Out=TEXT("Answer to command: ");
	for (int i=0;i<ReadArray[0];i++)
	{
		Out2.Format("%02X ",ReadArray[i+1]);
		Out=Out+Out2;
	}
	id=ReadArray[1] + ReadArray[2] * 0x100 + ReadArray[3] * 0x10000;
	Out.Format("%s\nInterface-ID: %d",Out, id);
	MessageBox(NULL,Out,TEXT("READ INTERFACE-ID"),MB_OK);
	return TRUE;
}
/*------------------------------------------------------------------
 * MICROSENSYS: ----------------------------------------------------
 * -----------------------------------------------------------------
 * -Reads Unique Identifier from any TAG near RFID interface--------
 * -It includes an auto scanning feature for all known transponders-
 * -Scanning timeout is defined by c_set_timeout--------------------
 *------------------------------------------------------------------*/
bool Identify(bool bShowMessageBox = true)
{
	CString Out,Out2;
	bool result = false;
	Out = _T("");
	ZeroMemory(&Identifier[0], sizeof(Identifier));

	// Try to identify TAGs
	MSS_Result=PROC_identify(&ReadArray[0]);
	switch (MSS_Result)
	{
		// TAG(s) found. Retrieve extended information
		case 0:
			Out2 = _T("");
			memcpy(&Identifier, &ReadArray, ReadArray[0] + 1);
			for (int i = 0; i < ReadArray[0]; i++)
			{
				Out2.Format("%02X ", ReadArray[i + 1]);
				Out += Out2;
			}
			Out2.Format("%s", Out);
			Out.Format("TAG found: %s", Out2);
			// Retrieve extended TAG information
			MSS_Result = PROC_driver_get_transponder_parameters(&TagType, &TagMaxLength, &TagSystem, &TagXtraParam, WriteArray);
			if (MSS_Result)
			{
				Out2.Format("%s", Out);
				Out.Format("%s\nError 0x%02X retrieving transponder parameters", Out2, MSS_Result);
				break;
			}
			result = true;
			break;

		// No TAGs found
		case 0x24:
			Out = _T("No TAGs found");
			break;

		// Error
		default:
			Out.Format("Error: 0x%02X searching for TAGs", MSS_Result);
			break;
	}
	if (bShowMessageBox)
	{
		MessageBox(NULL, Out, _T("IDENTIFY"), MB_OK);
	}
	return result;
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Sets antenna switch-off parameter for RFID interface-------
 *-------------------------------------------------------------*/
bool SetAutoOff()
{
	CString Out,Out2;
	BYTE	Setting;
	
	// Setting = 0			-> Switch OFF antenna immediately
	// Setting = 1..0xFE	-> Switch OFF after n x 10msec
	// Setting = 0xFF		-> newer switch OFF antenna (not recommended)
	Setting=100;
	int MSS_Result=PROC_c_reader_set_auto_off_iid3000(m_HCom,Setting);
	if (MSS_Result!=0)
	{
		Out.Format("Error: 0x%X setting AUTO-OFF",MSS_Result);
		MessageBox(NULL,Out,TEXT("SET AUTO-OFF"),MB_OK);
		return FALSE;
	}

	Out.Format("Set AUTO-OFF: %dmsec",Setting*10);

	MessageBox(NULL,Out,TEXT("SET AUTO-OFF"),MB_OK);
	return TRUE;
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Reads text from TAG's memory-------------------------------
 *-------------------------------------------------------------*/
void ReadText()
{
	CString Out, Out2;
	int i;
	BYTE* auxid;
	INT32 iFailCounter = 0;

	// TAG start address (0..n) to read data from
	// (for interface type 0x10 should be a multiple of 8)
	From = 0;
	// Length (bytes) of data to read from TAG
	// (for interface type 0x10 should be a multiple of 8)
	Length = 16;

	// Search for any TAG near the antenna
	if (Identify(false))
	{
		if (InterfaceType != 868)
		{
			if (Length > TagMaxLength)
			{
				Length = TagMaxLength;
			}
		}
		else
		{
			Length = 16;
		}

		// Build a new byte array with the TAG-ID. Identifier[0] contains
		// the length. The new array will contain only the ID-bytes
		auxid = new BYTE[Identifier[0]];
		memcpy(auxid, &Identifier[1], (size_t)Identifier[0]);

		if (InterfaceType == 868)
		{
			PROC_driver_set_transponder_page(3);	
		}
		
		while (iFailCounter < 10)
		{
			// Try to read "Length" bytes starting at "From" address from TAG with "auxid"-ID
			// and save the data into "ReadArray"
			MSS_Result = PROC_readBytes(auxid, From, Length, ReadArray);
			if (!MSS_Result)	
			{
				// Successful
				MessageBeep(0);
				Out = _T("");
				Out2 = _T("");
				i = 0;
				while (i < ReadArray[0])
				{
					if (ReadArray[i + 1] == 0)
					{
						break;
					}

					Out2.Format("%c", ReadArray[i + 1]);
					Out = Out + Out2;
					i++;
				}
				Out2.Format("%s", Out);
				Out.Format("Text read: \"%s\"", Out2);
				break;
			}
			else
			{
				// Failed
				Out.Format("Error 0x%02X reading text", MSS_Result);
			}
			iFailCounter++;
			Sleep(100);
		}

		free(auxid);
	}
	else
	{
		switch (MSS_Result)
		{
			// No TAG(s) found
			case 0x24:
				Out = _T("No TAGs found");
				break;

			// Error
			default:
				Out.Format("Error: 0x%02X searching for TAGs", MSS_Result);
				break;
		}
	}

	MessageBox(NULL, Out, _T("READ_TEXT"), MB_OK);
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Writes text into TAG's memory------------------------------
 *-------------------------------------------------------------*/
void WriteText()
{
	CString Out, Out2, DemoText;
	int i = 0;
	BYTE* auxid;
	INT32 iFailCounter = 0;

	// TAG start address (0..n) to write data to
	// (for interface type 0x10 should be a multiple of 8)
	From = 0;
	// Length (bytes) of data to write data to TAG
	// (for interface type 0x10 should be a multiple of 8)
	Length = 16;
	// Text to write
	DemoText = _T("iID(R) Demo Text");
	// DON'T LOCK Blocks after writing
	Lock = 0;
	
	// Search for any TAG near the antenna
	if (Identify(false))
	{
		if (InterfaceType != 868)
		{
			if (Length > TagMaxLength)
			{
				Length = TagMaxLength;
			}
		}
		else
		{
			Length = 16;
		}

		// Build a new byte array with the TAG-ID. Identifier[0] contains
		// the length. The new array will contain only the ID-bytes
		auxid = new BYTE[Identifier[0]];
		memcpy(auxid, &Identifier[1], (size_t)Identifier[0]);

		// Copy the to-be-written-text bytes into WriteArray[]
		memcpy(WriteArray, DemoText, DemoText.GetLength());

		if (InterfaceType == 868)
		{
			PROC_driver_set_transponder_page(3);	
		}

		while (iFailCounter < 10)
		{
			Out = _T("");
			// Try to write "Length" bytes starting at "From" address to TAG with "auxid"-ID
			// WriteArray contains the text bytes
			// Memory blocks will NOT locked after writing
			MSS_Result = PROC_writeBytes(auxid, From, Length, WriteArray, Lock);
			if (!MSS_Result)
			{
				// Successful
				MessageBeep(0);
				Out.Format("Text written: \"%s\"", DemoText);
				break;
			}
			else
			{
				// Failed
				Out.Format("Error 0x%02X writing text", MSS_Result);
			}
			iFailCounter++;
			Sleep(100);
		}

		free(auxid);
	}
	else
	{
		switch (MSS_Result)
		{
			// No TAGs found
			case 0x24:
				Out = _T("No TAGs found");
				break;

			// Error
			default:
				Out.Format("Error: 0x%02X searching for TAGs", MSS_Result);
				break;
		}
	}
	MessageBox(NULL, Out, _T("WRITE_TEXT"), MB_OK);
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Reads data from TAG's memory-------------------------------
 *-------------------------------------------------------------*/
void Read()
{
	CString Out, Out2;
	int i, DataBank;
	BYTE* auxid;
	INT32 iFailCounter = 0;

	// TAG start address (0..n) to write data to
	// (for interface type 0x10 should be a multiple of 8)
	From = 0;
	// Length (bytes) of data to write data to TAG
	// (for interface type 0x10 should be a multiple of 8)
	Length = 16;
	// Page to write data to
	DataBank = 3;
	
	// Search for any TAG near the antenna
	if (Identify(false))
	{
		// TAG found

		if (InterfaceType != 868)
		{
			if (Length > TagMaxLength)
			{
				Length = TagMaxLength;
			}
		}
		else
		{
			Length = 16;
		}

		// Build a new byte array with the TAG-ID. Identifier[0] contains
		// the length. The new array will contain only the ID-bytes
		auxid = new BYTE[Identifier[0]];
		memcpy(auxid, &Identifier[1], (size_t)Identifier[0]);

		while (iFailCounter < 10)
		{
			
			if ((InterfaceType == 868) || ((InterfaceType == 1356) && (ProtocolType == 0x1002)))
			{
				PROC_driver_set_transponder_page(DataBank);
			}

			// Try to read "Length" bytes starting at "From" address from TAG with "auxid"-ID
			// and save the data into ReadArray[]
			MSS_Result = PROC_readBytes(auxid, From, Length, ReadArray);
			if (!MSS_Result)
			{
				// Successful
				MessageBeep(0);
				Out = _T("");
				Out2 = _T("");
				for (i = 0; i < ReadArray[0]; i++)
				{
					Out2.Format("%02X", ReadArray[i + 1]);
					Out = Out + Out2;
				}
				Out2.Format("%s", Out);
				Out.Format("Data read: %s", Out2);
			}
			else
			{
				// Failed
				Out.Format("Error 0x%02X reading data", MSS_Result);
			}
			iFailCounter++;
			Sleep(100);
		}

		free(auxid);
	}
	else
	{
		switch (MSS_Result)
		{
			// No TAG(s) found
			case 0x24:
				Out = _T("No TAGs found");
				break;

			// Error
			default:
				Out.Format("Error: 0x%02X searching for TAGs", MSS_Result);
				break;
		}
	}
	MessageBox(NULL, Out, _T("READ_DATA"), MB_OK);
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Writes data into TAG's memory------------------------------
 *-------------------------------------------------------------*/
void Write()
{
	CString Out, Out2;
	int i;
	BYTE *auxid, data[16], DataBank;
	INT32 iFailCounter = 0;

	i = 0;

	// TAG start address (0..n) to write data to
	// (for interface type 0x10 should be a multiple of 8)
	From = 0;
	// Length (bytes) of data to write data to TAG
	// (for interface type 0x10 should be a multiple of 8)
	Length = 16;
	// Page to write data to
	DataBank = 3;
	// Bytes to write: { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
	//                   0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }
	for (int j = 0; j < sizeof(data); j++)
	{
		data[j] = i;
		i += 0x11;
	}
	// Don't LOCK blocks after writing
	Lock = 0;
	
	// Search for any TAG near the antenna
	if (Identify(false))
	{
		// TAG found

		if (InterfaceType != 868)
		{
			if (Length > TagMaxLength)
			{
				Length = TagMaxLength;
			}
		}
		else
		{
			Length = 16;
		}

		// Build a new byte array with the TAG-ID. Identifier[0] contains
		// the length. The new array will contain only the ID-bytes
		auxid = new BYTE[Identifier[0]];
		memcpy(auxid, &Identifier[1], (size_t)Identifier[0]);

		// Copy the to-be-written-text bytes into WriteArray[]
		memcpy(WriteArray, data, sizeof(data));

		if ((InterfaceType == 868) || ((1356 == InterfaceType) && (0x1002 == ProtocolType)))
		{
			PROC_driver_set_transponder_page(DataBank);
		}

		while (iFailCounter < 10)
		{
			// Try to write "Length" bytes starting at "From" address to TAG with "auxid"-ID
			// WriteArray contains the text bytes
			// Memory blocks will NOT locked after writing
			MSS_Result = PROC_writeBytes(auxid, From, Length, WriteArray, Lock);
			if (!MSS_Result)
			{
				// Successful
				MessageBeep(0);
				Out = _T("");
				for (int i = 0; i < Length; i++)
				{
					Out2.Format("%02X", data[i]);
					Out = Out + Out2;
				}
				Out2.Format("%s", Out);
				Out.Format("Data written: %s", Out2);
				break;
			}
			else
			{
				// Failed
				Out.Format("Error 0x%02X writing data", MSS_Result);
			}
			iFailCounter++;
			Sleep(100);
		}

		free(auxid);
	}
	else
	{
		switch (MSS_Result)
		{
			// No TAG(s) found
			case 0x24:
				Out = _T("No TAGs found");
				break;

			// Error
			default:
				Out.Format("Error: 0x%02X searching for TAGs", MSS_Result);
				break;
		}
	}
	MessageBox(NULL, Out, _T("WRITE_TEXT"), MB_OK);
}

/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Manages EPC scan settings.---------------------------------
 * -Both used functions are available up from driver-----------
 * -version 0x10.0x4F
 *-------------------------------------------------------------*/
void Reader_Enable_PC_EPC(bool bEnable)
{
	BYTE value;

	// Get current EPC scan settings
	// xxxx xxx1			: EPC scan response keeps PC word during EPC scan
	// xxxx xxx0 (default)	: No return of PC word during EPC scan
	// xxxx xx1x			: EPC scan response keeps session during EPC scan
	// xxxx xx0x (default)  : No return of session during EPC scan
	// xxxx x1xx			: EPC scan response keeps RSSI value during EPC scan
	// xxxx x0xx (default)	: No return of RSSI value during EPC scan
	value = PROC_get_EPC_extended_parameter();

	if (bEnable)
	{
		if (!(value & 0x01))
		{
			// EPC scan response keeps PC word during EPC scan
			value = BYTE(value | 0x01);
			PROC_set_EPC_extended_parameter(value);
		}
	}
	else
	{
		if (value & 0x01)
		{
			// No return of PC word during EPC scan
			value = BYTE(value & 0xFE);
			PROC_set_EPC_extended_parameter(value);
		}
	}
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Reads Unique Identifier from any ISO18000-6c TAG near------
 * -antenna of the interface.----------------------------------
 * -Scanning timeout is defined by c_set_timeout---------------
 *-------------------------------------------------------------*/
INT32 Iso18000_Identify(CStringArray *pUIIs, CByteArray pEPCs[])
{
	CString Out, Out2;
	BYTE numIds = 0;			// Count of found TAGs
	BYTE tagTypes[30];			// Type of found TAG(s)
	BYTE *lengths;				// Length of found TAG(s)'s identifiers
	BYTE *data;					// Array containing the identifier(s) of found TAG(s)
								// data[0] will contain the length of data
	CByteArray epc;

	INT32 dataPointer;
	BYTE converted[100];
	INT32 convLength;
	CString newString;

	lengths = new BYTE[sizeof(tagTypes)];
	data = new BYTE[32* sizeof(lengths)];

	// Enable retrieving PC byte by scanning
	Reader_Enable_PC_EPC(true);

	// Read EPC from transponder near RFID interface. 
	// Maximum length per EPC supported by this library is 64 bytes
	//  PROC_reader_get_handle() -> Communication handle
	//  0 -> driver parameter (reserved)
	//  numIds -> number of transponders found
	//  tagTypes -> type of each transponder
	//  lengths -> length of each ID
	//  data -> buffer where the data is load
	MSS_Result = PROC_iso18000_6_c_read_epc_gen2(PROC_reader_get_handle(), 0, &numIds, tagTypes, lengths, data);
	MSS_Result_ext = PROC_c_get_extended_status_information();

	if (!MSS_Result)
	{
		// TAG(s) found
		dataPointer = 0;
		convLength = 0;
		pUIIs->SetSize(numIds);

		// For each ID
		for (int i = 0; i < numIds; i++)
		{
			epc.SetSize(lengths[i] - 2);
			for (int j = 0; j < (lengths[i] - 2); j++)
			{
				epc.SetAt(j, *(data + dataPointer + 2 + j));
			}
			pEPCs[i].Append(epc);

			// If UII flag is set (first ID byte)
			if (*(data + dataPointer) & 0x01)
			{
				//Convert UII to ISO17367 6bit string
				//	0 -> driverParam (reserved. Should be 0)
				//	1 -> convertType Decodes ISO17367 6bit reader response to 8bit byte values (_from and _length have to be filled properly)
				//	data -> data to decode
				//	dataPointer+2 -> first byte of "data" to use to decode
				//	lengths[i] - 2 -> number of bytes of "data" to use to decode
				//	converted -> array where converted data will be
				//	convLength -> number of bytes saved in "converted"
				MSS_Result = PROC_uii_decode(0, 1, data, dataPointer + 2, lengths[i] - 2, converted, &convLength);
				if (!MSS_Result)
				{
					// Successful conversion
					newString = _T("");
					for (int j = 0; j < convLength; j++)
					{
						newString.AppendChar((TCHAR)*(converted + j));
					}

					newString.AppendChar(TCHAR(0));
					pUIIs->SetAtGrow(i, newString);
				}
			}
			else
			{
				// Not ISO17367 coded -> Show HEX representation of EPC code
				Out = _T("");
				Out2 = _T("");
				for (int j = 0; j < (lengths[i] - 2); j++)
				{
					Out2.Format("%02X", data[dataPointer + 2 + j]);
					Out.Append(Out2);
				}
				
				Out2.Format("HEX <%s>", Out);
				pUIIs->SetAtGrow(i, Out2);
			}

			dataPointer += lengths[i];
		}
	}

	// Disable retrieving PC byte by scanning
	Reader_Enable_PC_EPC(false);

	newString.ReleaseBuffer();
	free(lengths);
	free(data);
	return MSS_Result;
}
/*-------------------------------------------------------------
 * MICROSENSYS: -----------------------------------------------
 * ------------------------------------------------------------
 * -Search for a ISO18000-6c Transponder near the antenna------
 * -Scanning timeout is defined by c_set_timeout---------------
 *-------------------------------------------------------------*/
BYTE Iso18000_Identify_RSSI(CStringArray *pUIIs)
{
	CString Out, Out2;
	BYTE numIds = 0;			// Number of transponders found
	BYTE tagTypes[50];			// Array to hold the transponder type
	BYTE *lengths;				// Array to hold the length of each ID
	BYTE *data;					// Array containing the ID of each transponder. data[0] will contain the length of the data
	FLOAT *rssi;			

	INT32 dataPointer;
	BYTE converted[100];
	INT32 convLength;
	CString newString;

	lengths = new BYTE[sizeof(tagTypes)];
	data = new BYTE[32 * sizeof(tagTypes)];
	rssi = new FLOAT[sizeof(tagTypes)];

	// Enable retrieving PC byte by scanning
	Reader_Enable_PC_EPC(true);

	// Read EPC from transponder near the antenna
	//  PROC_reader_get_handle() -> Communication handle
	//  0 -> driver parameter (reserved)
	//  numIds -> number of transponders found
	//  tagTypes -> type of each transponder
	//  lengths -> length of each ID
	//  data -> buffer where the data is load
	//  rssi -> rssi value of each Tag
	MSS_Result = PROC_iso18000_6_c_read_epc_rssi_gen2(PROC_reader_get_handle(), 0, &numIds, tagTypes, lengths, data, rssi);
	MSS_Result_ext = PROC_c_get_extended_status_information();

	if (!MSS_Result)
	{
		// Result OK
		dataPointer = 0;
		convLength = 0;
		pUIIs->SetSize(numIds);

		// For each ID
		for (int i = 0; i < numIds; i++)
		{
			// If UII flag is set (first ID byte)
			if (*(data + dataPointer) & 0x01)
			{
				// Convert UII to ISO17367 6bit string
				//  0 -> driverParam (reserved. Should be 0)
				//  1 -> convertType. Decodes ISO17367 6bit reader response to 8bit byte values (_from and _length have to be properly filled)
				//  data -> data to decode
				//  dataPointer + 2 -> first byte of "data" to use to decode
				//  lengths[i] - 2 -> number of bytes of "data" to use to decode
				//  converted -> array where converted data will be saved
				//  convLength -> number of bytes saved in "converted"
				MSS_Result = PROC_uii_decode(0, 1, data, dataPointer + 2, lengths[i] - 2, converted, &convLength);
				if (!MSS_Result) 
				{
					// Successful conversion
					newString = _T("");
					for (int j = 0; j < convLength; j++)
					{
						newString.AppendChar((TCHAR)*(converted + j));
					}

					newString.AppendFormat(_T(" (%.1f dBm)"), rssi[i]);
					newString.AppendChar(TCHAR(0));
					pUIIs->SetAtGrow(i, newString);
				}
			}
			else
			{
				// Not ISO17367 coded -> Show HEX representation of EPC code
				Out = _T("");
				Out2 = _T("");
				for (int j = 0; j < (lengths[i] - 2); j++)
				{
					Out2.Format("%02X", data[dataPointer + 2 + j]);
					Out = Out + Out2;
				}
				
				Out2.Format("HEX <%s>", Out);
				Out2.AppendFormat(_T(" (%.1f dBm)"), rssi[i]);
				Out2.AppendChar(TCHAR(0));
				pUIIs->SetAtGrow(i, Out2);
			}

			dataPointer += lengths[i];
		}
	}

	// Disable retrieving PC byte by scanning
	Reader_Enable_PC_EPC(false);

	newString.ReleaseBuffer();
	free(lengths);
	free(data);
	return MSS_Result;
}
/*-----------------------------------------------------------------
 * MICROSENSYS: ---------------------------------------------------
 * ----------------------------------------------------------------
 * -Reads data from an ISO18000-6c transponder near RFID interface-
 *-----------------------------------------------------------------*/
BYTE Iso18000_ReadWords(BYTE epc[], BYTE EPC_length, BYTE data[], UINT32 bank, UINT32 address, UINT32 length)
{
	BYTE result;
	INT32 FailCounter = 0;

	m_HCom = PROC_reader_get_port_state(m_HCom);
	if (!m_HCom)
	{
		return 0xFF;
	}

	ZeroMemory(data, sizeof(data));
	while (FailCounter < 10)
	{
		if (epc != NULL)
		{
			// EPC provided

			// Read data from transponder near RFID interface
			// Function prototype:
			// BYTE iso18000_6_c_read_words(HANDLE m_HCom,			// Handle of port with RFID interface
			//								UINT32 _uDriverParam,	// Reserved, should be 0xFF
			//								UINT32 _uStdParam,		// Hardware parameter, see hardware documentation
			//								UINT32 _uDataBank,		// Page address to read from transponder
			//								UINT32 _uDataAddress,	// Memory address to read from transponder
			//								UINT32 _uDataLength,	// Data length to read from transponder
			//								BYTE* _bCode,			// Pointer to byte array containing EPC code
			//								BYTE _bCodeLength,		// Length of EPC code
			//								BYTE* _bData)			// Pointer to array of byte, which will contain the
			//														// data read from transponder. data[0] contains length of data
			result = PROC_iso18000_6_c_read_words(m_HCom, 0xFF, 1, bank, address, length, epc, EPC_length, data);
		}
		else
		{
			// EPC not provided. The function will search for a transponder near RFID interface
			result = PROC_iso18000_6_c_read_words(m_HCom, 0xFF, 1, bank, address, length, epc, 0, data);
		}
		if (!result)
		{
			// Successful, stop trying reading
			break;
		}
		FailCounter++;
	}
	return result;
}
/*----------------------------------------------------------------
 * MICROSENSYS: --------------------------------------------------
 * ---------------------------------------------------------------
 * -Writes data to an ISO18000-6c transponder near RFID interface-
 *----------------------------------------------------------------*/
BYTE Iso18000_WriteWords(BYTE epc[], BYTE EPC_length, BYTE data[], UINT32 bank, UINT32 address, UINT32 length)
{
	BYTE result;
	INT32 FailCounter = 0;

	m_HCom = PROC_reader_get_port_state(m_HCom);
	if (!m_HCom)
	{
		return 0xFF;
	}

	while (FailCounter < 10)
	{
		if (epc != NULL)
		{
			// EPC provided 

			// Writes data to transponder near RFID interface
			// Function prototype:
			// BYTE iso18000_6_c_write_words(HANDLE m_HCom,				// Handle of port with RFID interface
			//								 UINT32 _uDriverParam,		// Reserved, should be 0xFF
			//								 UINT32 _uStdParam,			// Hardware parameter, see hardware documentation
			//								 UINT32 _uDataBank,			// Page address to read from transponder
			//								 UINT32 _uDataAddress,		// Memory address to read from transponder
			//								 UINT32 _uDataLength,		// Data length to read from transponder
			//								 BYTE* _bCode,				// Pointer to array of byte containing EPC code
			//								 BYTE _bCodeLength,			// Byte which contains length of EPC
			//								 BYTE* _bData)				// Pointer to array of byte which contains data to write.
			result = PROC_iso18000_6_c_write_words(m_HCom, 0xFF, 0, bank, address, length, epc, EPC_length, data);
		}
		else
		{
			// EPC not provided. The function will search for a transponder near RFID interface
			result = PROC_iso18000_6_c_write_words(m_HCom, 0xFF, 0, bank, address, length, epc, 0, data);
		}
		if (!result)
		{
			// Successful, stop trying reading from transponder
			break;
		}
		FailCounter++;
	}
	return result;
}
/*-----------------------------------------------------------------
 * MICROSENSYS: ---------------------------------------------------
 * ----------------------------------------------------------------
 * -Searchss for ISO18000-6c transponder near RFID interface-------
 *-----------------------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000Identify()
{
	CStringArray pUIIs;
	CString Out, Out2;
	CByteArray pEPCs[30];

	Out = _T("");
	Out2 = _T("");
	
	try
	{
		// Search for transponder near RFID interface
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Tranponder(s) found
			if (pUIIs.GetCount() > 0)
			{
				for (int i = 0; i < (pUIIs.GetCount()); i++)
				{
					Out2.Format("%s%c", pUIIs.GetAt(i), TCHAR(0x0A));
					Out.Format("%s%s", Out, Out2);
				}
			}
		}
		else
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponders found");
					break;

				// Error
				default:
					Out.Format("Error 0x%02X searching for transponders", MSS_Result);
					break;
			}
		}
		MessageBox(NULL, Out, _T("ISO18000_IDENTIFY"), MB_OK);
	}
	catch (...)
	{
	}
}
/*----------------------------------------------------------------------
 * MICROSENSYS: --------------------------------------------------------
 * ---------------------------------------------------------------------
 * -Reads 'n' words from an ISO18000-6c transponder near RFID interface-
 *----------------------------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000Read()
{
	BYTE data[17];
	UINT32 page, address, length;
	BYTE epcLength;
	CString Out, Out2;
	CStringArray pUIIs;
	CByteArray pEPCs[30];

	page = 3;
	address = 0;
	length = 16;
	Out = _T("");
	Out2 = _T("");

	try
	{
		// Search for ISO18000-6c tranponders
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Transponder(s) found
			if (pUIIs.GetCount() > 1)
			{
				Out = _T("Too many transponders found!");
			}
			else
			{
				// Only a transponder found. Get its EPC
				Out2 = pUIIs.GetAt(0);
				Out2.TrimLeft(_T("HEX <"));
				Out2.TrimRight(_T(">"));
				epcLength = (BYTE)Out2.GetLength();
				if ((epcLength % 2) != 0)
				{
					MessageBox(NULL, _T("Wrong length!"), _T("ISO18000_READ"), MB_OK);
					return;
				}

				ZeroMemory(data, sizeof(data));

				// Read data from found transponder
				MSS_Result = Iso18000_ReadWords(pEPCs[0].GetData(), (BYTE)pEPCs[0].GetSize(), data, page, address, length);
				if (MSS_Result)
				{
					Out.Format("Error 0x%02X reading data", MSS_Result);
				}
				else
				{
					Out.Format(_T("Target UID: %s\n"), Out2);
					Out.Append(_T("Data read: "));
					Out = _T("Data read: ");
					for (int i = 0; i < data[0]; i++)
					{
						Out2.Format("%02X", data[i + 1]);
						Out.Append(Out2);
					}
				}
				Sleep(100);
			}
		}
		else
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponders found");
					break;

				// Error
				default:
					Out.Format("Error 0x%02X searching for transponders!", MSS_Result);
					break;
			}
		}

		MessageBox(NULL, Out, _T("ISO18000_READ"), MB_OK);
	}
	catch (...)
	{
	}
}
/*---------------------------------------------------------------------
 * MICROSENSYS: -------------------------------------------------------
 * --------------------------------------------------------------------
 * -Writes 'n' words to an ISO18000-6c transponder near RFID interface-
 *---------------------------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000Write()
{
	BYTE data[16];
	UINT32 dataBank, address, length;
	BYTE epcLength;
	CString Out, Out2;
	CStringArray pUIIs;
	INT32 iFailCounter = 0;

	CByteArray pEPCs[30];

	dataBank = 3;		// Page to write data to
	address = 0;		// Address to start writing data to
	length = 16;		// Length of data to write
	Out = _T("");
	Out2 = _T("");

	try
	{
		// Search for ISO18000-6c transponder near RFID interface
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Tranponder(s) found)
			if (pUIIs.GetCount() > 1)
			{
				Out = _T("Too many transponders found!");
			}
			else
			{
				// Only a transponder found. Get its EPC
				Out2 = pUIIs.GetAt(0);
				Out2.TrimLeft(_T("HEX <"));
				Out2.TrimRight(_T(">"));
				epcLength = (BYTE)Out2.GetLength();
				if ((epcLength % 2) != 0)
				{
					MessageBox(NULL, _T("Wrong length!"), _T("ISO18000_WRITE"), MB_OK);
					return;
				}

				// Build array with data to write
				for (int i = 0; i < sizeof(data); i++)
				{
					data[i] = 0xFF - (0x11 * i);		// 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88
														// 0x77, 0x66, 0xff, 0x44, 0x33, 0x22, 0x11, 0x00
				}

				// Write "Length" bytes starting on address "Address" on transponder with EPC = "epc"
				// "epcLength" contains the length of epc-bytes
				// "data" contains the data to write
				MSS_Result = Iso18000_WriteWords(pEPCs[0].GetData(), (BYTE)pEPCs[0].GetSize(), data, dataBank, address, length);
				if (MSS_Result)
				{
					// Failed
					Out.Format("Error 0x%02X writing data!", MSS_Result);
				}
				else
				{
					// Successful
					Out.Format(_T("Target UID: %s\n"), Out2);
					Out.Append(_T("Data written: "));
					for (int i = 0; i < (INT32)Length; i++)
					{
						Out2.Format("%02X", data[i]);
						Out.Append(Out2);
					}
				}
				Sleep(100);
			}
		}
		else
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No TAGs found");
					break;
				
				// Error
				default:
					Out.Format("Error 0x%02X searching for TAGs!", MSS_Result);
					break;
			}
		}
		
		MessageBox(NULL, Out, _T("ISO18000_WRITE"), MB_OK);
	}
	catch(...)
	{
	}
}
/*-----------------------------------------------------------
 * MICROSENSYS: ---------------------------------------------
 * ----------------------------------------------------------
 * -Searches for ISO18000-6c transponder near RFID interface-
 *-----------------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000ReadUIIs()
{
	CStringArray pUIIs;
	CString Out, Out2;
	CByteArray pEPCs[30];

	Out = _T("");
	Out2 = _T("");

	try
	{
		// Search for transponder near RFID interface
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Transponder(s) found
			if (pUIIs.GetCount() > 0)
			{
				for (int i = 0; i < (pUIIs.GetCount()); i++)
				{
					Out2.Format(_T("%s%c"), pUIIs.GetAt(i), TCHAR(0x0A));
					Out.Append(Out2);
				}
			}
		}
		else
		{	
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponder found");
					break;

				// Error
				default:
					Out.Format(_T("Error 0x%02X searching for transponders"), MSS_Result);
					break;
			}
		}

		MessageBox(NULL, Out, _T("ISO18000_READ_UII"), MB_OK);
	}
	catch (...)
	{
	}
}
/*-----------------------------------------------------------
 * MICROSENSYS: ---------------------------------------------
 * ----------------------------------------------------------
 * -Searches for ISO18000-6c transponder near RFID interface-
 * -und shows the RSSI value for each one--------------------
 *-----------------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000ReadUIIsRSSI()
{
	CStringArray pUIIs;
	CString Out, Out2;

	Out = _T("");
	Out2 = _T("");

	try
	{
		// Search for transponder near RFID interface
		MSS_Result = Iso18000_Identify_RSSI(&pUIIs);
		if (!MSS_Result)
		{
			// Transponder(s) found
			if (pUIIs.GetCount() > 0)
			{
				for (int i = 0; i < (pUIIs.GetCount()); i++)
				{
					Out2.Format(_T("%s%c"), pUIIs.GetAt(i), TCHAR(0x0A));
					Out.Append(Out2);
				}
			}
		}
		else
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponder found");
					break;

				// Error
				default:
					Out.Format(_T("Error 0x%02X searching for transponders"), MSS_Result);
					break;
			}
		}

		MessageBox(NULL, Out, _T("ISO18000_READ_UII_RSSI"), MB_OK);
	}
	catch (...)
	{
	}
}
/*--------------------------------------
 * MICROSENSYS: ------------------------
 * -------------------------------------
 * -Get the current (when set) AFI mask-
 *--------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000GetAFI()
{
	BYTE afi = 0;
	CString Out;

	Out = _T("");

	try
	{
		MSS_Result = PROC_get_AFI_mask(&afi);
		if (MSS_Result)
		{
			Out = _T("No AFI set");
		}
		else
		{
			Out.Format(_T("Current AFI mask: 0x%02X"), afi);
		}
	}
	catch (...)
	{ }

	MessageBox(NULL, Out, _T("ISO18000_GET_AFI"), MB_OK);
}
/*---------------------------------------------------
 * MICROSENSYS: -------------------------------------
 * --------------------------------------------------
 * -Set an AFI mask for EPC-filtering by Identifying-
 *---------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000SetAFI()
{
	BYTE afi = 0;
	CString Out;

	try
	{
		MSS_Result = PROC_set_AFI_mask(afi);
		if (!MSS_Result)
		{
			Out.Format(_T("AFI mask set: 0x%02X"), afi);
		}
		else
		{
			Out.Format(_T("Error 0x%02X setting AFI mask"), MSS_Result);
		}
	}
	catch (...)
	{
	}
	
	MessageBox(NULL, Out, _T("ISO18000_SET_AFI"), MB_OK);
}
/*--------------------------------------------------
 * MICROSENSYS: ------------------------------------
 * -------------------------------------------------
 * -Reads 'n' words from an ISO18000-6c transponder-
 * -near RFID interface using an access password----
 *--------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000ReadSecure()
{
	BYTE data[17];
	BYTE password[4];				// Minimum password length = 4
	UINT32 page, address, count;
	BYTE epcLength;
	CString Out, Out2;
	CStringArray pUIIs;
	CByteArray pEPCs[30];

	page = 3;						// UHF memory bank or LEGIC sector
	address = 0;					
	count = 16;					
	Out = _T("");
	Out2 = _T("");

	try
	{
		// Search for ISO18000-6c transponders
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Transponder(s) found
			if (pUIIs.GetCount() > 1)
			{
				Out = _T("Too many transponders found!");
			}
			else
			{
				// Only a transponder found. Get its EPC
				Out2 = pUIIs.GetAt(0);
				Out2.TrimLeft(_T("HEX <"));
				Out2.TrimRight(_T(">"));
				epcLength = (BYTE)Out2.GetLength();
				if ((epcLength % 2) != 0)
				{
					MessageBox(NULL, _T("Wrong length!"), _T("ISO18000_READ_SECURE"), MB_OK);
					return;
				}

				// Default password = 0x00000000
				ZeroMemory(password, sizeof(password));	

				ZeroMemory(data, sizeof(data));

				// Write data into transponder
				MSS_Result = ISO18000_ReadWords_Secure(pEPCs[0].GetData(), (BYTE)pEPCs[0].GetSize(), data, page, address, count, password);
				if (MSS_Result)
				{
					Out.Format("Error 0x%02X reading data", MSS_Result);
				}
				else
				{
					Out.Format(_T("Target UID: %s\n"), Out2);
					Out.Append(_T("Data read: "));
					for (int i = 0; i < data[0]; i++)
					{
						Out2.Format("%02X", data[i + 1]);
						Out.Append(Out2);
					}
				}
				Sleep(100);
			}
		}
		else
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponder found");
					break;
				
				// Error
				default:
					Out.Format("Error 0x%02X searching for TAGs!", MSS_Result);
					break;
			}
		}
	}
	catch (...)
	{ 
	}

	MessageBox(NULL, Out, _T("ISO18000_READ_SECURE"), MB_OK);
}
/*-------------------------------------------------
 * MICROSENSYS: -----------------------------------
 * ------------------------------------------------
 * -Writes 'n' words to an ISO18000-6c transponder-
 * -near RFID interface using an access password---
 *-------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000WriteSecure()
{
	BYTE data[16];
	BYTE password[4];				// Password length = 32 bits
	UINT32 page, address, count;
	BYTE epcLength;
	CString Out, Out2;
	CStringArray pUIIs;
	CByteArray pEPCs[30];

	page = 3;						// 3rd UHF page or LEGIC segment
	address = 0;					// 
	count = 16;					// Number of bytes to write (8 words)
	Out = _T("");	
	Out2 = _T("");

	try
	{
		// Search for ISO18000-6c transponders
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Transponder(s) found
			if (pUIIs.GetCount() > 1)
			{
				Out = _T("Too many transponders found!");
			}
			else
			{
				// Only a transponder found. Get its EPC
				Out2 = pUIIs.GetAt(0);
				Out2.TrimLeft(_T("HEX <"));
				Out2.TrimRight(_T(">"));
				epcLength = (BYTE)Out2.GetLength();
				if ((epcLength % 2) != 0)
				{
					MessageBox(NULL, _T("Wrong length!"), _T("ISO18000_WRITE_SECURE"), MB_OK);
					return;
				}
				
				// Default password = 0x00000000
				ZeroMemory(password, sizeof(password));

				for (int i = 0; i < sizeof(data); i++)
				{
					data[i] = 0x11 * i;					// 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
														// 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
				}

				// Read data from found transponder
				MSS_Result = ISO18000_WriteWords_Secure(pEPCs[0].GetData(), (BYTE)pEPCs[0].GetSize(), data, page, address, count, password);
				if (MSS_Result)
				{
					Out.Format("Error 0x%02X reading data", MSS_Result);
				}
				else
				{
					Out.Format(_T("Target UID: %s\n"), Out2);
					Out.Append(_T("Data written: "));
					for (int i = 0; i < sizeof(data); i++)
					{
						Out2.Format("%02X", data[i]);
						Out.Append(Out2);
					}
				}
				Sleep(100);
			}
		}
		else 
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponder found");
					break;
				
				// Error
				default:
					Out.Format("Error 0x%02X searching for TAGs!", MSS_Result);
					break;
			}
		}
	}
	catch (...)
	{ }

	MessageBox(NULL, Out, _T("ISO18000_WRITE_SECURE"), MB_OK);
}
void CSampleApp_VC_MFCApp::OnLockEpcSecure()
{
	UINT32 payload;
	//              0          8         0..
	//          --------- ----------- ----
	//         |         |           |
	//         Kll | Acc | EPC | TID | Usr
	//   Mask: 0-0 | 0-0 | 1-0 | 0-0 | 0-0
	//
	//         ..        2           0     
	//         ---- ----------- ---------- 
	//             |           |          |
	// Action: 0-0 | 0-0 | 1-0 | 0-0 | 0-0 
	payload = 0x080200;	// Left-aligned, 1-nibble-padding on the right
	EPCLockSecure(payload);
}

void CSampleApp_VC_MFCApp::OnUnlockEpcSecure()
{
	UINT32 payload;
	//              0          8         0..
	//          --------- ----------- ----
	//         |         |           |
	//         Kll | Acc | EPC | TID | Usr
	//   Mask: 0-0 | 0-0 | 1-0 | 0-0 | 0-0
	//
	//         ..        0           0     
	//         ---- ----------- ---------- 
	//             |           |          |
	// Action: 0-0 | 0-0 | 0-0 | 0-0 | 0-0 
	payload = 0x080000;	// Left-aligned, 1-nibble-padding on the right
	EPCLockSecure(payload);
}

void CSampleApp_VC_MFCApp::OnLockEpc()
{
	UINT32 payload;
	//              0          8         0..
	//          --------- ----------- ----
	//         |         |           |
	//         Kll | Acc | EPC | TID | Usr
	//   Mask: 0-0 | 0-0 | 1-0 | 0-0 | 0-0
	//
	//         ..        2           0     
	//         ---- ----------- ---------- 
	//             |           |          |
	// Action: 0-0 | 0-0 | 1-0 | 0-0 | 0-0 
	payload = 0x080200;	// Left-aligned, 1-nibble-padding on the right
	EPCLock(payload);
}

void CSampleApp_VC_MFCApp::OnUnlockEpc()
{ 
	UINT32 payload;
	//              0          8         0..
	//          --------- ----------- ----
	//         |         |           |
	//         Kll | Acc | EPC | TID | Usr
	//   Mask: 0-0 | 0-0 | 1-0 | 0-0 | 0-0
	//
	//         ..        0           0     
	//         ---- ----------- ---------- 
	//             |           |          |
	// Action: 0-0 | 0-0 | 0-0 | 0-0 | 0-0 
	payload = 0x080000;	// Left-aligned, 1-nibble-padding on the right
	EPCLock(payload);
}
/*-------------------------------------------------
 * MICROSENSYS: -----------------------------------
 * ------------------------------------------------
 * -Writes a new EPC to an ISO18000-6c transponder-
 * -near RFID interface using an access password---
 *-------------------------------------------------*/
void CSampleApp_VC_MFCApp::OnIso18000WriteEpc()
{
	BYTE pc[2]			= { 0x30, 0x00 };
	BYTE data[12]		= { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C };
	BYTE password[4];				// Password length = 32 bits
	BYTE epcLength;
	CString Out, Out2;
	CStringArray pUIIs;

	CByteArray pEPCs[30];

	Out = _T("");	
	Out2 = _T("");

	try
	{
		// Search for ISO18000-6c transponders
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Transponder(s) found
			if (pUIIs.GetCount() > 1)
			{
				Out = _T("Too many transponders found!");
			}
			else
			{
				// Only a transponder found. Get its EPC
				Out2 = pUIIs.GetAt(0);
				Out2.TrimLeft(_T("HEX <"));
				Out2.TrimRight(_T(">"));
				epcLength = (BYTE)Out2.GetLength();
				if ((epcLength % 2) != 0)
				{
					MessageBox(NULL, _T("Wrong length!"), _T("ISO18000_WRITE_EPC"), MB_OK);
					return;
				}
			
				// Default password = 0x00000000
				ZeroMemory(password, sizeof(password));

				// Write new EPC into transponder
				MSS_Result = ISO18000_WriteEPC(pEPCs[0].GetData(), (BYTE)pEPCs[0].GetSize(), pc, data, sizeof(data), password);
				if (MSS_Result)
				{
					Out.Format("Error 0x%02X writing EPC", MSS_Result);
				}
				else
				{
					Out.Format(_T("Old EPC: %s\n"), Out2);
					Out.Append(_T("New EPC: "));
					for (int i = 0; i < sizeof(data); i++)
					{
						Out2.Format("%02X", data[i]);
						Out.Append(Out2);
					}
				}
				Sleep(100);
			}
		}
		else
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponder found");
					break;
				
				// Error
				default:
					Out.Format("Error 0x%02X searching for TAGs!", MSS_Result);
					break;
			}
		}
	}
	catch (...)
	{ }

	MessageBox(NULL, Out, _T("ISO18000_WRITE_EPC"), MB_OK);
}
/*------------------------------------------------
 * MICROSENSYS: ----------------------------------
 * -----------------------------------------------
 * -Locks/unlocks the EPC bytes in an ISO18000-6c-
 * -transponder near RFID interface---------------
 *------------------------------------------------*/
void EPCLock(UINT32 payload)
{
	BYTE epcLength;
	CString Out, Out2;
	CStringArray pUIIs;
	CByteArray pEPCs[32];

	try
	{
		// Search for ISO18000-6c transponders
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Transponder(s) found
			if (pUIIs.GetCount() > 1)
			{
				Out = _T("Too many transponders found!");
			}
			else
			{
				// Only a transponder found. Get its EPC
				Out2 = pUIIs.GetAt(0);
				Out2.TrimLeft(_T("HEX <"));
				Out2.TrimRight(_T(">"));
				epcLength = (BYTE)Out2.GetLength();
				if ((epcLength % 2) != 0)
				{
					MessageBox(NULL, _T("Wrong length!"), _T("ISO18000_LOCK"), MB_OK);
					return;
				}

				// Lock
				MSS_Result = ISO18000_lock(pEPCs[0].GetData(), (BYTE)pEPCs[0].GetSize(), payload);
				if (MSS_Result)
				{
					Out.Format("Error 0x%02X", MSS_Result);
				}
				else
				{
					Out.Format(_T("Target UID: %s\n"), Out2);
					Out.Append(_T("Payload: "));
					Out2.Format("%05X", payload);
					Out.Append(Out2);
				}
				Sleep(100);
			}
		}
		else 
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponder found");
					break;
				
				// Error
				default:
					Out.Format("Error 0x%02X searching for TAGs!", MSS_Result);
					break;
			}
		}
	}
	catch (...)
	{ 
	}

	MessageBox(NULL, Out, _T("ISO18000_LOCK"), MB_OK);
}
/*------------------------------------------------
 * MICROSENSYS: ----------------------------------
 * -----------------------------------------------
 * -Locks/unlocks the EPC bytes in an ISO18000-6c-
 * -transponder near RFID interface---------------
 *------------------------------------------------*/
void EPCLockSecure(UINT32 payload)
{
	BYTE epcLength;
	BYTE password[4];
	CString Out, Out2;
	CStringArray pUIIs;

	CByteArray pEPCs[32];

	try
	{
		// Search for ISO18000-6c transponders
		MSS_Result = Iso18000_Identify(&pUIIs, pEPCs);
		if (!MSS_Result)
		{
			// Transponder(s) found
			if (pUIIs.GetCount() > 1)
			{
				Out = _T("Too many transponders found!");
			}
			else
			{
				// Only a transponder found. Get its EPC
				Out2 = pUIIs.GetAt(0);
				Out2.TrimLeft(_T("HEX <"));
				Out2.TrimRight(_T(">"));
				epcLength = (BYTE)Out2.GetLength();
				if ((epcLength % 2) != 0)
				{
					MessageBox(NULL, _T("Wrong length!"), _T("ISO18000_LOCK"), MB_OK);
					return;
				}

				// Default password = 0x00000000
				ZeroMemory(password, sizeof(password));

				// Lock
				MSS_Result = ISO18000_lock_secure(pEPCs[0].GetData(), (BYTE)pEPCs[0].GetSize(), payload, password);
				if (MSS_Result)
				{
					Out.Format("Error 0x%02X locking", MSS_Result);
				}
				else
				{
					Out.Format(_T("%s locked\n"), Out2);
					Out.Append(_T("Payload: "));
					Out2.Format("%05X", payload);
					Out.Append(Out2);
				}
				Sleep(100);
			}
		}
		else 
		{
			switch (MSS_Result)
			{
				// No transponder(s) found
				case 0x24:
					Out = _T("No transponder found");
					break;
				
				// Error
				default:
					Out.Format("Error 0x%02X searching for TAGs!", MSS_Result);
					break;
			}
		}
	}
	catch (...)
	{ 
	}

	MessageBox(NULL, Out, _T("ISO18000_LOCK_SECURE"), MB_OK);
}


/*-----------------------------------------------------------------
 * MICROSENSYS: ---------------------------------------------------
 * ----------------------------------------------------------------
 * -Read data from ISO18000-6c Transponder memory------------------
 *-----------------------------------------------------------------*/
BYTE ISO18000_ReadWords_Secure(BYTE epc[], BYTE EPC_length, BYTE data[], UINT16 bank, UINT16 address, UINT16 length, BYTE password[])
{
	BYTE result;
	INT32 FailCounter = 0;

	// Check port state & obtain Port Handle
	m_HCom = PROC_reader_get_port_state(m_HCom);
	if (!m_HCom)
	{
		return 0xFF;
	}

	ZeroMemory(data, sizeof(data));
	while (FailCounter < 10)
	{
		if (epc != NULL)
		{
			// Read selected
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> Hardware parameter
			//  bank -> Memory bank to read
			//  address -> first byte position to read
			//  length -> number of bytes to read
			//  epc -> EPC of the selected transponder
			//  EPC_length -> length of the EPC
			//  password -> Access password
			//  data -> buffer to load read data
			result = PROC_iso18000_6_c_read_words_secure(m_HCom, 0, 0, bank, address, length, epc, EPC_length, password, data);
		}
		else
		{
			// Read not selected (will read one random transponder near the antenna)
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> Hardware parameter
			//  bank -> Memory bank to read
			//  address -> first byte position to read
			//  length -> number of bytes to read
			//  epc -> EPC of the selected transponder
			//  0 -> do not use EPC
			//  password -> Access password
			//  data -> buffer to load read data
			result = PROC_iso18000_6_c_read_words_secure(m_HCom, 0, 0, bank, address, length, epc, 0, password, data);
		}
		MSS_Result_ext = PROC_c_get_extended_status_information();

		if (!result) 
		{
			// Successful, stop trying reading
			break;
		}

		FailCounter++;
	}

	return result;
}
/*----------------------------------------------------------------
 * MICROSENSYS: --------------------------------------------------
 * ---------------------------------------------------------------
 * -Writes data to an ISO18000-6c transponder near RFID interface-
 *----------------------------------------------------------------*/
BYTE ISO18000_WriteWords_Secure(BYTE epc[], BYTE EPC_length, BYTE data[], UINT32 bank, UINT32 address, UINT32 length, BYTE password[])
{
	BYTE result;
	INT32 FailCounter = 0;

	// Check port state & obtain Port Handle
	m_HCom = PROC_reader_get_port_state(m_HCom);
	if (!m_HCom)
	{
		return 0xFF;
	}

	while (FailCounter < 10)
	{
		if (epc != NULL)
		{
			// Write secure selected
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> Hardware parameter
			//  bank -> Memory bank to write
			//  address -> First byte position to write
			//  length -> number of bytes to write
			//  epc -> EPC of the selected transponder
			//  EPC_length -> length of the EPC
			//  password -> Access password
			//	data -> data to write
			result = PROC_iso18000_6_c_write_words_secure(m_HCom, 0, 0, bank, address, length, epc, EPC_length, password, data);
		}
		else
		{
			// Write secure not selected (will write one random transponder near the antenna)
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> Hardware parameter
			//  bank -> Memory bank to write
			//  address -> First byte position to write
			//  length -> number of bytes to write
			//  epc -> EPC of the selected transponder
			//  0 -> do not use EPC
			//  password -> Access password
			//  data -> data to write
			result = PROC_iso18000_6_c_write_words_secure(m_HCom, 0, 0, bank, address, length, epc, 0, password, data);
		}
		MSS_Result_ext = PROC_c_get_extended_status_information();

		if (!result)
		{
			// Successful, stop trying writing to the transponder
			break;
		}
		FailCounter++;
	}

	return result;
}
/*---------------------------------------------------------------------
 * MICROSENSYS: -------------------------------------------------------
 * --------------------------------------------------------------------
 * -Writes a new EPC to an ISO18000-6c transponder near RFID interface-
 *---------------------------------------------------------------------*/
BYTE ISO18000_WriteEPC(BYTE old_epc[], BYTE oldEpc_length, BYTE new_pc[], BYTE new_epc[], BYTE newEpc_length, BYTE password[])
{
	BYTE result;
	INT32 FailCounter = 0;

	m_HCom = PROC_reader_get_port_state(m_HCom);
	if (!m_HCom)
	{
		return 0xFF;
	}

	while (FailCounter < 10)
	{
		if (old_epc != NULL)
		{
			// Write epc selected
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> Hardware parameter
			//  old_epc -> EPC of the selected transponder
			//  OLD_length -> length of the old EPC
			//  new_pc -> PC bytes to write
			//  new_epc -> EPC bytes to write into the selected transponder
			//  EPC_length -> length of the EPC bytes to write
			//  password -> Access password
			result = PROC_iso18000_6_c_write_epc_gen2(m_HCom, 0, 0, old_epc, oldEpc_length, new_pc, new_epc, newEpc_length, password);
		}
		else
		{
			// Write epc not selected (will write one random transponder near the antenna)
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> Hardware parameter
			//  old_epc -> EPC of the selected transponder
			//  0 -> do not use old_epc
			//  new_pc -> PC bytes to write
			//  new_epc -> EPC bytes to write into the selected transponder
			//  EPC_length -> length of the EPC bytes to write
			//  password -> Access password
			result = PROC_iso18000_6_c_write_epc_gen2(m_HCom, 0, 0, old_epc, 0, new_pc, new_epc, newEpc_length, password);
		}

		MSS_Result_ext = PROC_c_get_extended_status_information();

		if (!result)
		{
			break;
		}

		FailCounter++;
	}

	return result;
}
/*----------------------------------------------------------------------------------
 * MICROSENSYS: --------------------------------------------------------------------
 * ---------------------------------------------------------------------------------
 * -Locks/unlocks selected fields in an ISO18000-6c transponder near RFID interface-
 *----------------------------------------------------------------------------------*/
BYTE ISO18000_lock(BYTE epc[], BYTE EPC_length, UINT32 payload)
{ 
	BYTE result;
	INT32 FailCounter = 0;

	m_HCom = PROC_reader_get_port_state(m_HCom);
	if (!m_HCom)
	{
		return 0xFF;
	}

	while (FailCounter < 10)
	{
		if (epc != NULL)
		{
			// Lock selected
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> Hardware parameter
			//  payload -> lock payload (as DWORD, left adjusted)
			//  epc -> EPC of the selected transponder
			//  EPC_length -> length of the EPC
			result = PROC_iso18000_6_c_lock(m_HCom, 0, 0, payload, epc, EPC_length);
		}
		else
		{
			// Lock not selected (will lock one random transponder near the antenna)
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> Hardware parameter
			//  payload -> lock payload (as DWORD, left adjusted)
			//  epc -> EPC of the selected transponder
			//  0 -> do not use EPC
			result = PROC_iso18000_6_c_lock(m_HCom, 0, 0, payload, epc, 0);
		}
		MSS_Result_ext = PROC_c_get_extended_status_information();

		if (!result)
		{
			break;
		}
		FailCounter++;
	}

	return result;
}
/*--------------------------------------------------------------
 * MICROSENSYS: ------------------------------------------------
 * -------------------------------------------------------------
 * -Locks/unlocks selected fields in an ISO18000-6c transponder-
 * -near RFID interface using an access password----------------
 *--------------------------------------------------------------*/
BYTE ISO18000_lock_secure(BYTE epc[], BYTE EPC_length, UINT32 payload, BYTE password[])
{
	BYTE result;
	INT32 FailCounter = 0;

	m_HCom = PROC_reader_get_port_state(m_HCom);
	if (!m_HCom)
	{
		return 0xFF;
	}

	while (FailCounter < 10)
	{
		if (epc != NULL)
		{
			// Lock secure selected
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> hardware parameter
			//  payload -> lock payload (as DWORD, left adjusted)
			//  epc -> EPC of the selected transponder
			//  EPC_length -> length of the EPC
			//  password -> Access password
			result = PROC_iso18000_6_c_lock_secure(m_HCom, 0, 0, payload, epc, EPC_length, password);
		}
		else
		{
			// Lock secure not selected (will lock one random transponder near the antenna)
			//  m_HCom -> Port Handle
			//  0 -> driver parameter (reserved)
			//  0 -> hardware parameter
			//  payload -> lock payload (as DWORD, left adjusted)
			//  epc -> EPC of the selected transponder
			//  0 -> do not use EPC
			//  password -> Access password
			result = PROC_iso18000_6_c_lock_secure(m_HCom, 0, 0, payload, epc, 0, password);
		}
		MSS_Result_ext = PROC_c_get_extended_status_information();

		if (!result)
		{
			break;
		}

		FailCounter++;
	}

	return result;
}
void CSampleApp_VC_MFCApp::OnPortInitializeport()
{
	InitializePort();
}
void CSampleApp_VC_MFCApp::OnPortCloseport()
{
	ClosePort();
}
void CSampleApp_VC_MFCApp::OnReaderSetauto()
{
	SetAutoOff();
}
void CSampleApp_VC_MFCApp::OnReaderReadinterface()
{
	ReadInterfaceID();
}
void CSampleApp_VC_MFCApp::OnDriverIdentify()
{
	Identify();
}
void CSampleApp_VC_MFCApp::OnDriverReadText()
{
	ReadText();
}
void CSampleApp_VC_MFCApp::OnDriverWriteText()
{
	WriteText();
}
void CSampleApp_VC_MFCApp::OnDriverRead()
{
	Read();
}
void CSampleApp_VC_MFCApp::OnDriverWrite()
{
	Write();
}
