📄 rfidhandler.cpp
字号:
/** @file
* RFIDHandler.cpp
*
* INTEL CONFIDENTIAL
* Copyright 2006 Intel Corporation All Rights Reserved.
* The source code contained or described herein and all documents related
* to the source code ("Material") are owned by Intel Corporation or its
* suppliers or licensors. Title to the Material remains with Intel Corporation
* or its suppliers and licensors. The Material contains trade secrets and
* proprietary and confidential information of Intel or its suppliers and licensors.
* The Material is protected by worldwide copyright and trade secret laws and
* treaty provisions. No part of the Material may be used, copied, reproduced,
* modified, published, uploaded, posted, transmitted, distributed, or disclosed
* in any way without Intel抯 prior express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be express
* and approved by Intel in writing.
*
*/
#include "StdAfx.h"
#include "RFIDHandler.h"
const std::string FULLPOWER14443ACMD = "0AFF"; //configure temporary ASIC (0A) with FullPowerMode (FF)
const std::string FULLPOWER14443BCMD = "0BFF"; //configure temporary ASIC (0B) with FullPowerMode (FF)
const std::string ENABLEAUTOGAINCONTROLCMD = "F0FF"; // Automatic Gain Control Enable
const std::string DISABLEAUTOGAINCONTROLCMD = "F000"; // Automatic Gain Control Disable
const std::string AMINPUTSELECTIONCMD = "F1FF"; // AM - Primary Input channel
const std::string FMINPUTSELECTIONCMD = "F100"; // FM - Primary Input channel
const std::string FIRMWAREVERSIONCMD = "FE"; // Returns the reader's firmware version
const std::string REGISTERWRITE14443ACMD = "1000210109"; //Set Register Write for 14443A
const std::string REGISTERWRITE14443BCMD = "100021010C"; //Set Register Write for 14443B
//const std::string RESETMICROCONTOLLERCMD = "FF"; // ?? (To, Reset the tag to ready "180026";)
const std::string LOWDATARATECMD = "100100"; // Set the low data rate on RFID Reader device
RFIDHandler::RFIDHandler(void)
{
mComHandle = INVALID_HANDLE_VALUE;
mpCom = new CommCommunicator(mComHandle);
mpProtocolHandler = NULL;
}
RFIDHandler::~RFIDHandler(void)
{
if (NULL != mpCom)
{
delete mpCom;
mpCom = NULL;
}
if (NULL != mpProtocolHandler)
{
delete mpProtocolHandler;
mpProtocolHandler = NULL;
}
}
/* This function set the device in full power mode.
@return status code
@retval #ture command accepted successfully.
@retval #false The device is no longer accepting commands.
*/
bool RFIDHandler::PowerUpRFIDReader()
{
bool RetStatus = OpenActiveCOMPort();
return RetStatus;
}
/* This function sends the LowDataRate command to check Rfid is attached
to correct COM port and set the deivce to LowDataRate mode to reduce read/write
failure on the COM port.
@return status code
@retval #HC_Success command accepted successfully.
@retval #HC_DeviceUnresponsive The device is no longer accepting commands.
*/
RF_Status RFIDHandler::InitializeDevice()
{
RF_Status rfStat = RF_Success;
char cRet[RF_MAXRETURNSIZE];
DWORD dwSize = RF_MAXRETURNSIZE;
if (!mpCom->PackageAndWrite(LOWDATARATECMD.c_str()))
{
rfStat = RF_WriteFailure;
}
else if (!mpCom->portRead(cRet, &dwSize))
{
rfStat = RF_ReadFailure;
}
if ( (RF_WriteFailure == rfStat) || (RF_ReadFailure == rfStat) )
{
rfStat = RF_DeviceUnResponsive;
}
return rfStat;
}
/*This function try to open the COM port on which RFID device is attached.
@return status code
@retval #true if able to find and open the correct COM port on which Rfid device is attached.
@retval #true if unable to find and open the correct COM port on which Rfid device is attached.
*/
bool RFIDHandler::OpenActiveCOMPort()
{
bool RetStatus = false;
mComHandle = CreateFile(mCommPort.c_str(),GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
0, // no overlapped I/O
NULL); // null template
if (INVALID_HANDLE_VALUE == mComHandle)
{
return RetStatus;
}
else
{
mpCom->SetComHandle(mComHandle);
BOOL PortReady;
DCB dcb;
PortReady = GetCommState(mComHandle, &dcb);
if (TRUE == PortReady)
{
dcb.BaudRate = 115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fAbortOnError = TRUE;
PortReady = SetCommState(mComHandle, &dcb);
if (TRUE == PortReady)
{
COMMTIMEOUTS commTimeouts;
PortReady = GetCommTimeouts (mComHandle, &commTimeouts);
if (TRUE == PortReady)
{
commTimeouts.WriteTotalTimeoutConstant = 30;
commTimeouts.WriteTotalTimeoutMultiplier = 20;
commTimeouts.ReadIntervalTimeout = 50;
commTimeouts.ReadTotalTimeoutConstant = 30;
commTimeouts.ReadTotalTimeoutMultiplier = 40;
PortReady = SetCommTimeouts (mComHandle, &commTimeouts);
if (TRUE == PortReady)
{
if (RF_Success == InitializeDevice())
{
RetStatus = true;
}
else
{
ClosePort();
return RetStatus;
}
}
}
}
}
if (FALSE == PortReady)
{
ClosePort();
return RetStatus;
}
}
return RetStatus;
}
/* This function closes the already open COM port.
*/
void RFIDHandler::ClosePort()
{
if( mComHandle != INVALID_HANDLE_VALUE)
{
HRESULT hrStatus = CloseHandle(mComHandle) ;
mComHandle = INVALID_HANDLE_VALUE;
}
return;
}
/*This function sends SetFullPower command to set the device in full power mode.
@return status code
@retval #HC_Success command accepted successfully.
@retval #HC_DeviceUnresponsive The device is no longer accepting commands.
*/
RF_Status RFIDHandler::SetFullPower(void)
{
RF_Status rfStat = RF_Success;
char cRet[RF_MAXRETURNSIZE];
DWORD dwSize = RF_MAXRETURNSIZE;
const char *cmdStr = (mSelectedProtocol == RF_ISO14443A)? FULLPOWER14443ACMD.c_str(): FULLPOWER14443BCMD.c_str();
if (!mpCom->PackageAndWrite(cmdStr))
{
rfStat = RF_WriteFailure;
}
else if (!mpCom->portRead(cRet, &dwSize))
{
rfStat = RF_ReadFailure;
}
if ( (RF_WriteFailure == rfStat) || (RF_ReadFailure == rfStat) )
{
rfStat = RF_DeviceUnResponsive;
}
return (rfStat);
}
/*This function sends AutoGainControl command to the device to set the ACG.
@return status code
@retval #HC_Success command accepted successfully.
@retval #HC_DeviceUnresponsive The device is no longer accepting commands.
*/
RF_Status RFIDHandler::AutoGainControl(void) {
RF_Status rfStat = RF_Success;
char cRet[RF_MAXRETURNSIZE];
DWORD dwSize = RF_MAXRETURNSIZE;
if (!mpCom->PackageAndWrite(DISABLEAUTOGAINCONTROLCMD.c_str()))
{
rfStat = RF_WriteFailure;
}
else if (!mpCom->portRead(cRet, &dwSize))
{
rfStat = RF_ReadFailure;
}
if ( (RF_WriteFailure == rfStat) || (RF_ReadFailure == rfStat) )
{
rfStat = RF_DeviceUnResponsive;
}
return (rfStat);
}
/*This function sends SetMainChannel command to the device to set the receiver mode(AM/PM).
@return status code
@retval #HC_Success command accepted successfully.
@retval #HC_DeviceUnresponsive The device is no longer accepting commands.
*/
RF_Status RFIDHandler::SetMainChannel(void) {
RF_Status rfStat = RF_Success;
char cRet[RF_MAXRETURNSIZE];
DWORD dwSize = RF_MAXRETURNSIZE;
if (!mpCom->PackageAndWrite(AMINPUTSELECTIONCMD.c_str()))
{
rfStat = RF_WriteFailure;
}
else if (!mpCom->portRead(cRet, &dwSize))
{
rfStat = RF_ReadFailure;
}
if ( (RF_WriteFailure == rfStat) || (RF_ReadFailure == rfStat) )
{
rfStat = RF_DeviceUnResponsive;
}
return (rfStat);
}
/*This function sends WriteRegister command to the device for selected protocol type.
@return status code
@retval #HC_Success command accepted successfully.
@retval #HC_DeviceUnresponsive The device is no longer accepting commands.
*/
RF_Status RFIDHandler::WriteRegister(void)
{
RF_Status rfStat = RF_Success;
char cRet[RF_MAXRETURNSIZE];
DWORD dwSize = RF_MAXRETURNSIZE;
const char *cmdStr = (mSelectedProtocol == RF_ISO14443A)? REGISTERWRITE14443ACMD.c_str(): REGISTERWRITE14443BCMD.c_str();
if (!mpCom->PackageAndWrite(cmdStr))
{
rfStat = RF_WriteFailure;
}
else if (!mpCom->portRead(cRet, &dwSize))
{
rfStat = RF_ReadFailure;
}
if ( (RF_WriteFailure == rfStat) || (RF_ReadFailure == rfStat) )
{
rfStat = RF_DeviceUnResponsive;
}
return (rfStat);
}
/*This function sends getfirmware command to check device is ready.
@return status code
@retval #HC_Success command accepted successfully.
@retval #HC_DeviceUnresponsive The device is no longer accepting commands.
*/
RF_Status RFIDHandler::DeviceReady(void)
{
RF_Status rfStat = RF_Success;
char cRet[RF_MAXRETURNSIZE];
DWORD dwSize = RF_MAXRETURNSIZE;
if (!mpCom->PackageAndWrite(FIRMWAREVERSIONCMD.c_str()))
{
rfStat = RF_WriteFailure;
}
else if (!mpCom->portRead(cRet, &dwSize))
{
rfStat = RF_ReadFailure;
}
if ( (RF_WriteFailure == rfStat) || (RF_ReadFailure == rfStat) )
{
rfStat = RF_DeviceUnResponsive;
}
return (rfStat);
}
/*This function set the selected protocol and sends four commands (register write,
set AGC, and set receiver mode (AM / PM).
@param[in] SelectedProtocol the selected protocol to be set.
@return status code
@retval #HC_Success command accepted successfully.
@retval #HC_DeviceUnresponsive The device is no longer accepting commands.
*/
RF_Status RFIDHandler::SetProtocol(PROTOCOL SelectedProtocol)
{
mSelectedProtocol = SelectedProtocol;
RF_Status Status = RF_Success;
Status = SetFullPower();
if (RF_Success == Status)
{
Status = WriteRegister();
if (RF_Success == Status)
{
Status = AutoGainControl();
if (RF_Success == Status)
{
Status = SetMainChannel();
}
}
}
return Status;
}
/*This function will create an object of selectedprotocol type and will return
list of tag UIDs of selected protocol.
*/
RF_Status RFIDHandler::GetUids(vector<string> &UidList)
{
RF_Status Status = RF_Success;
if (NULL != mpProtocolHandler)
{
delete mpProtocolHandler;
mpProtocolHandler = NULL;
}
switch(mSelectedProtocol)
{
case RF_ISO14443A:
mpProtocolHandler = new Protocol14443AHandler(mpCom);
break;
case RF_ISO14443B:
mpProtocolHandler = new Protocol14443BHandler(mpCom);
break;
}
if (NULL != mpProtocolHandler)
{
Status = mpProtocolHandler->GetUIDList(UidList);
}
return Status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -