⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rfidradiomanager.cpp

📁 UHF RFID Reader Program
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "StdAfx.h"
#include "RFRM.h"
#include "RFIDRadioManager.h"
#include "RFIDRadio.h"

#include "mapfile.h"
#include "FirmwareUpdate.h"
#include "WaveBox.h"
#include "FrequencyBandList.h"

#include "Util.h"

static CFile file;

CRFIDRadioManager::CRFIDRadioManager(CWnd* pWnd)
: m_pRadioEnum(NULL)
, m_pParent(pWnd)
, m_bConnect(FALSE)
//, m_bStart(FALSE)
, m_nAntenna(0)
, m_nChannel(0)
, m_nPacketCount(0)
, m_radioFilterMode(RFID_RADIO_FILTER_GABAGE)
, m_nCmdCount(0)
{
	m_strBeepSound = AfxGetApp()->GetProfileString(TEXT("WaveBox"), TEXT("Path"));
	m_radioBeepMode = (RFID_RADIO_BEEP_MODE)AfxGetApp()->GetProfileInt(TEXT("WaveBox"), TEXT("Mode"), 0);

	if(!m_strBeepSound.IsEmpty())
	{
		m_waveBox = new CWaveBox();
		m_waveBox->Load((LPTSTR)(LPCTSTR)m_strBeepSound);
	}
}

CRFIDRadioManager::~CRFIDRadioManager(void)
{
	if(IsConnect())
		Disconnect();
	// free a buffer for radio enumeration
//	delete m_pRadioEnum;
	free(m_pRadioEnum);
}

RFID_STATUS CRFIDRadioManager::Connect()
{
	RFID_STATUS status;

	// First open the library
	if (RFID_STATUS_OK != 
		(status = RFID_Startup(&m_libraryVersion, 0x00000000)))
	{
		TRACE(L"RFID_Startup() failed.  RC = %d\n", status);

		return status;
	}

	// Allocate a buffer for radio enumeration
	m_pRadioEnum = (RFID_RADIO_ENUM *) malloc(sizeof(RFID_RADIO_ENUM));
	if (m_pRadioEnum == NULL)
	{
		TRACE(L"Failed to allocate memory\n");

		// Shut down the library
		RFID_Shutdown();

		status = RFID_ERROR_FAILURE;

		return status;
	}

	// Initialize the enumeration buffer
	m_pRadioEnum->length      = sizeof(RFID_RADIO_ENUM);
	m_pRadioEnum->totalLength = sizeof(RFID_RADIO_ENUM);

	// Keep attempting to enumerate while the library says that the buffer is
	// too small.  This accounts for a situation where a radio may be plugged
	// in between the call to enumerate and the return with the previously   
	// needed buffer size.                                                   
	while (RFID_ERROR_BUFFER_TOO_SMALL ==
		(status = RFID_RetrieveAttachedRadiosList(m_pRadioEnum, 0)))
	{
		/* Allocate a new, larger buffer */
		RFID_RADIO_ENUM* pNewEnum = 
			(RFID_RADIO_ENUM *) realloc(m_pRadioEnum, m_pRadioEnum->totalLength);

		if (NULL == pNewEnum)
		{
			TRACE(L"Failed to reallocate memory\n");

			/* Shut down the library */
			RFID_Shutdown();

			return FALSE;
		}

		m_pRadioEnum = pNewEnum;
	}

	// Verify that the enumeration was successful */
	if (RFID_STATUS_OK != status)
	{
		TRACE1("RFID_RetrieveAttachedRadiosList failed:  RC = %d\n", status);

		// Shut down the library
		RFID_Shutdown();

		return FALSE;
	}

	if( m_pRadioEnum != NULL && m_pRadioEnum->countRadios != 0)
	{
		for(unsigned int i = 0 ; i < m_pRadioEnum->countRadios; i++)
		{
			CRFIDRadio* pRadio = new CRFIDRadio(m_pRadioEnum->ppRadioInfo[i], this);

			if(RFID_STATUS_OK != 
				( status = pRadio->OpenRadio()))
			{
				TRACE(_T("%d's OpenRadio failed:  RC = %d\n"), i, status);
				return status;
			}

			m_listRadio.AddTail(pRadio);
		}

		m_bConnect = !m_bConnect;
	}

	file.Open(TEXT("Temperature.csv"), CFile::modeWrite | CFile::modeCreate);

	return status;
}

RFID_STATUS CRFIDRadioManager::Disconnect(void)
{
	RFID_STATUS status = RFID_STATUS_OK;

	if(m_bConnect)
	{
		if( m_pRadioEnum != NULL && m_pRadioEnum->countRadios != 0)
		{
			for(unsigned int i = 0; i< m_pRadioEnum->countRadios; i++)
			{
				POSITION pos = m_listRadio.FindIndex(i);
				CRFIDRadio* pRadio = (CRFIDRadio*)m_listRadio.GetAt(pos);

				if(pRadio->IsActive())
				{
					if(RFID_STATUS_OK != 
						(status = pRadio->RadioAbortOperation()))
					{
						TRACE1("RadioAbortOperation failed:  RC = %d\n", status);
						//				return status;
					}
				}

				if(RFID_STATUS_OK != 
					(status = pRadio->CloseRadio()))
				{
					TRACE1("CloseRadio failed:  RC = %d\n", status);
					//				return status;
				}

				delete pRadio;
				pRadio = NULL;

				m_listRadio.RemoveAll();
			}
		}

		delete m_pRadioEnum;
		m_pRadioEnum = NULL;

		// Shut down the library
		if(RFID_STATUS_OK != 
			(status = RFID_Shutdown()))
		{
			TRACE1("RFID_Shutdown failed:  RC = %d\n", status);
			return status;
		}

		m_bConnect = !m_bConnect;
	}

	file.Close();

	return status;
}

int CRFIDRadioManager::GetRadioCount(void)
{
	if(m_pRadioEnum != NULL && m_pRadioEnum->countRadios != 0)
		return m_pRadioEnum->countRadios;

	return 0;
}

RFID_STATUS CRFIDRadioManager::RunInventory(int nModule, INT32 nCount)
{
	RFID_STATUS status = RFID_STATUS_OK;

	if(m_listRadio.GetCount() > 0)
	{
		POSITION pos = m_listRadio.FindIndex(nModule);	// find the radio oject
		CRFIDRadio* pRadio = (CRFIDRadio*)m_listRadio.GetAt(pos);

		pRadio->m_nStopCountDown = nCount;	// set the inventory stop cound
		pRadio->m_runStatus = RFID_INVENTORY;	// set the inventory mode

		m_oleStartDateTime = COleDateTime::GetCurrentTime();	// set the current time

		pRadio->Create();		// create inventory thread
	}

	return status;
}

RFID_STATUS CRFIDRadioManager::TagRead(int nModule, INT32U* epc, RFID_18K6C_MEMORY_BANK bank, INT16U offset, INT16U count)
{
	RFID_STATUS status = RFID_STATUS_OK;

	if(m_listRadio.GetCount() > 0)
	{
		POSITION pos = m_listRadio.FindIndex(nModule);
		CRFIDRadio* pRadio = (CRFIDRadio*)m_listRadio.GetAt(pos);

		pRadio->m_runStatus = RFID_READ;

		if(epc != NULL)
		{
			pRadio->m_epc = epc;
		}
		else
		{
			pRadio->m_epc = NULL;
		}

		pRadio->m_MBank = bank;
		pRadio->m_nOffset = offset;
		pRadio->m_nCount = count;

		pRadio->Create();

//		m_bStart = TRUE;
	}

	return status;
}

RFID_STATUS CRFIDRadioManager::TagWrite(int nModule, INT32U* epc, RFID_18K6C_MEMORY_BANK bank, INT16U offset, INT16U count, INT16U* pData)
{
	RFID_STATUS status = RFID_STATUS_OK;

	if(m_listRadio.GetCount() > 0)
	{
		POSITION pos = m_listRadio.FindIndex(nModule);
		CRFIDRadio* pRadio = (CRFIDRadio*)m_listRadio.GetAt(pos);

		pRadio->m_runStatus = RFID_WRITE;

		if(epc != NULL)
		{
			pRadio->m_epc = epc;
		}
		else
		{
			pRadio->m_epc = NULL;
		}

		pRadio->m_MBank = bank;
		pRadio->m_nOffset = offset;
		pRadio->m_nCount = count;
		pRadio->m_nWriteData = pData;

		pRadio->Create();

		//m_bStart = TRUE;
	}

	return status;
}

RFID_STATUS CRFIDRadioManager::TagLock(int nModule, INT32U* epc, RFID_18K6C_TAG_PERM permission, INT32U accessPassword)
{
	RFID_STATUS status = RFID_STATUS_OK;

	if(m_listRadio.GetCount() > 0)
	{
		POSITION pos = m_listRadio.FindIndex(nModule);
		CRFIDRadio* pRadio = (CRFIDRadio*)m_listRadio.GetAt(pos);

		pRadio->m_runStatus = RFID_LOCK;

		if(epc != NULL)
		{
			pRadio->m_epc = epc;
		}
		else
		{
			pRadio->m_epc = NULL;
		}

		pRadio->m_lockPermission = permission;
		pRadio->m_AccessPassword = accessPassword;

		pRadio->Create();

		//m_bStart = TRUE;
	}

	return status;
}

RFID_STATUS CRFIDRadioManager::TagKill(int nModule, INT32U* epc, INT32U accessPassword, INT32U killPassword)
{
	RFID_STATUS status = RFID_STATUS_OK;

	if(m_listRadio.GetCount() > 0)
	{
		POSITION pos = m_listRadio.FindIndex(nModule);
		CRFIDRadio* pRadio = (CRFIDRadio*)m_listRadio.GetAt(pos);

		pRadio->m_runStatus = RFID_KILL;

		if(epc != NULL)
		{
			pRadio->m_epc = epc;
		}
		else
		{
			pRadio->m_epc = NULL;
		}

		pRadio->m_AccessPassword = accessPassword;
		pRadio->m_KillPassword = killPassword;

		pRadio->Create();

		//m_bStart = TRUE;
	}

	return status;
}


RFID_STATUS CRFIDRadioManager::StopInventory(int nModule)
{
	RFID_STATUS status = RFID_STATUS_OK;

	if( m_pRadioEnum != NULL && m_pRadioEnum->countRadios != 0)
	{
		POSITION pos = m_listRadio.FindIndex(nModule);
		CRFIDRadio* pRadio = (CRFIDRadio*)m_listRadio.GetAt(pos);

		pRadio->NotifyStop();
#ifdef _WIN32_WCE
		if(RFID_STATUS_OK != 
			(status = pRadio->RadioAbortOperation()))
		{
			TRACE1("RadioAbortOperation failed:  RC = %d\n", status);
			return status;
		}
#else
		if(RFID_STATUS_OK != 
			(status = pRadio->RadioCancelOperation()))
		{
			TRACE1("RadioCancelOperation failed:  RC = %d\n", status);
			return status;
		}
#endif

//		m_bStart = FALSE;
	}

	return status;
}

BOOL CRFIDRadioManager::IsConnect(void)
{
	return m_bConnect;
}

void CRFIDRadioManager::SetParent(CWnd* pParent)
{
	m_pParent = pParent;
}

BOOL CRFIDRadioManager::IsInventory(int nModule)
{
	BOOL active = FALSE;

	if(m_listRadio.GetCount() > 0)
	{
		POSITION pos = m_listRadio.FindIndex(nModule);
		CRFIDRadio* pRadio = (CRFIDRadio*)m_listRadio.GetAt(pos);

		active = pRadio->IsActive();

#ifdef _DEBUG
		CString str;

//		str.Format(TEXT("\n\nThread Activation State : %b\n\n"), active);
#endif
	}

	return active;
}

#ifdef WINCE
BOOL CRFIDRadioManager::Sound(DWORD fre, DWORD due)
{
	BOOL bRtn;
	LPCWSTR lpRes;
	_TCHAR	lpName[10];
	HANDLE hRes;
	HRSRC hResInfo;
	HINSTANCE Nl=AfxGetInstanceHandle();

	/* Find the WAVE resource. */
	switch(fre)
	{
	case SOUND_COMPLETE:
		wcscpy(lpName,_T("IDR_WAVE_COMPLETE"));
		break;
	case SOUND_BEEP:
		wcscpy(lpName,_T("IDR_WAVE_BEEP"));
		break;
	case SOUND_BATTERY:
		wcscpy(lpName,_T("IDR_WAVE_SOUND"));
		break;
	}

	hResInfo= FindResource(Nl,lpName,_T("WAVE"));

	if(hResInfo == NULL)
		return FALSE;
	/* Load the WAVE resource. */

	hRes = LoadResource(Nl,hResInfo);
	if (hRes == NULL)
		return FALSE;

	/* Lock the WAVE resource and play it. */
	lpRes=(LPWSTR)LockResource(hRes);
	if(lpRes==NULL)
		return FALSE;

	bRtn = sndPlaySound(lpRes, SND_MEMORY | SND_SYNC);
	if(bRtn == NULL)
		return FALSE;

	/* Free the WAVE resource and return success or failure. */
	FreeResource(hRes);
	return TRUE;
}
#endif

void CRFIDRadioManager::AddRadioPacket(RFID_PACKET_COMMON* packet, void* context)
{
	RFID_PACKET_COMMON *common = (RFID_PACKET_COMMON *)packet;
	INT16U packetType = MacToHost16(common->pkt_type);

	m_nPacketCount++;

	CString str;
	str.Format(L"========================= Packet Count : %u\n", m_nPacketCount);
	TRACE(str);

	switch (packetType)
	{
	case RFID_PACKET_TYPE_COMMAND_BEGIN:
		{
			RFID_PACKET_COMMAND_BEGIN *cmdbegin =
				(RFID_PACKET_COMMAND_BEGIN *)packet;
			INT32U command = MacToHost32(cmdbegin->command);

			TRACE(_T("Packet type: RFID_PACKET_COMMAND_BEGIN\n"));
			CString strTemp, temp;
			
			if(common->flags & 1)
				temp.Format(_T(""));
			else
				temp.Format(_T("%s"), _T("not"));

			TRACE(_T("\tOperation was %s executed in continuous mode\n"), temp);
			TRACE(_T("\tOperation started at millisecond %u\n"), MacToHost32(cmdbegin->ms_ctr));
			TRACE(_T("\tOperation that is beginning: "));
			switch (command)
			{
			case RFID_COMMAND_TYPE_18K6C_INVENTORY:
				temp.Format(TEXT("Inventory"));
				TRACE(_T("Inventory\n"));
				break;
			case RFID_COMMAND_TYPE_18K6C_READ:
				temp.Format(TEXT("Read"));
				TRACE(_T("Read\n"));
				break;
			case RFID_COMMAND_TYPE_18K6C_WRITE:
				temp.Format(TEXT("Write"));
				TRACE(_T("Write\n"));
				break;
			case RFID_COMMAND_TYPE_18K6C_LOCK:
				temp.Format(TEXT("Lock"));
				TRACE(_T("Lock\n"));
				break;
			case RFID_COMMAND_TYPE_18K6C_KILL:
				temp.Format(TEXT("Kill"));
				TRACE(_T("Kill\n"));
				break;
			default:
				temp.Format(TEXT("Unknown"));
				TRACE(_T("Unknown (%u)\n"), command);
				break;
			}

			strTemp.Format(_T(" #%u Command Beginning"), m_nCmdCount);

			temp += strTemp;

			TRACE(_T("Operation status: (%s)\n"), temp);

			m_pParent->SendMessage(WM_STATUS_MSG,(WPARAM)(LPCTSTR)temp);

			break;
		}
	case RFID_PACKET_TYPE_18K6C_TAG_ACCESS:
		{
			const RFID_PACKET_18K6C_TAG_ACCESS* pTagAccess = (const RFID_PACKET_18K6C_TAG_ACCESS*)packet;
			CONTEXT_PARMS*  pParms = (CONTEXT_PARMS *) context;
			CString			strTemp = TEXT("");

			/* Check to see if the tag access failed                                  */
			if (RFID_18K6C_TAG_ACCESS_ERROR(pTagAccess->cmn.flags))
			{
				if (RFID_18K6C_TAG_BACKSCATTER_ERROR(pTagAccess->cmn.flags))
				{
#ifdef _DEBUG
					TRACE(_T("ERROR: Tag backscattered error 0x%.8x on access\n"), pTagAccess->error_code);
#endif
					switch(pTagAccess->error_code)
					{
					case 0x00:
						{
							strTemp.LoadString(IDS_STRING_TAGACCESS_BACKSCATTER_00);
							m_pParent->SendMessage(WM_TAG_ACCESS, (WPARAM)(LPCTSTR)strTemp, 0);
						}
						break;
					case 0x03:
						{
							strTemp.LoadString(IDS_STRING_TAGACCESS_BACKSCATTER_03);
							m_pParent->SendMessage(WM_TAG_ACCESS, (WPARAM)(LPCTSTR)strTemp, 0);
						}
						break;
					case 0x04:
						{
							strTemp.LoadString(IDS_STRING_TAGACCESS_BACKSCATTER_04);
							m_pParent->SendMessage(WM_TAG_ACCESS, (WPARAM)(LPCTSTR)strTemp, 0);
						}
						break;
					case 0x0B:
						{
							strTemp.LoadString(IDS_STRING_TAGACCESS_BACKSCATTER_0B);
							m_pParent->SendMessage(WM_TAG_ACCESS, (WPARAM)(LPCTSTR)strTemp, 0);
						}
						break;
					case 0x0F:
						{
							strTemp.LoadString(IDS_STRING_TAGACCESS_BACKSCATTER_0F);
							m_pParent->SendMessage(WM_TAG_ACCESS, (WPARAM)(LPCTSTR)strTemp, 0);
						}
						break;
					}
				}
				else if (RFID_18K6C_TAG_ACCESS_TIMEOUT(pTagAccess->cmn.flags))
				{
#ifdef _DEBUG
					TRACE(_T("ERROR: Timeout waiting for tag to ACK\n"));
#endif
					strTemp.Format(_T("Timeout waiting for tag to ACK"));
					theApp.m_pMainWnd->SendMessage(WM_TAG_ACCESS, (WPARAM)(LPCTSTR)strTemp, 0);
				}
				else if (RFID_18K6C_TAG_ACCESS_CRC_INVALID(pTagAccess->cmn.flags))
				{
#ifdef _DEBUG

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -