added errormessages (in method MCI_TxRequest_RxResponse, for better NFC error analysis) CRC check included. If the meter CRC doesn't match the calculated CRC an error is returned. removed method MCI_Raw_Read/write (same as MCI_Read/Write, which autoselects the correct password) Refactoring more detailed errorcodes(no more unidentified, separated host (>=20) and Meter messages (0..8))
656 lines
29 KiB
C#
656 lines
29 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
//*****************************************************************************
|
|
// Copyright 2020 Sensus GmbH Ludwigshafen. All rights reserved.
|
|
// Author: Venkat, Rajeshwar
|
|
//*****************************************************************************
|
|
|
|
namespace Sensus.iPerl.NfcHandler
|
|
{
|
|
public class NFCHeadConfig
|
|
{
|
|
private SERIAL_Driver _SERIAL_Driver;
|
|
public event DelNfc_NFCHeadConfig_MessageHandler MessageEvent;
|
|
|
|
private readonly byte NFC_HEAD_CONFIG_MAX_RETRIES = 5;
|
|
private readonly byte[] NFCHeadConfig_OK_res_msg = Encoding.ASCII.GetBytes("OK\n"); //'OK\n' - ack - request is implemented successfully
|
|
private readonly byte[] NFCHeadConfig_crst_req_msg = Encoding.ASCII.GetBytes("crst\r\n"); //'crst\r\n' - command - reset message
|
|
private readonly byte[] NFCHeadConfig_cdoff_req_msg = Encoding.ASCII.GetBytes("cdoff\r\n"); //'cdoff\r\n' - command - data mode off request message
|
|
private readonly byte[] NFCHeadConfig_cdon_req_msg = Encoding.ASCII.GetBytes("cdon\r\n"); //'cdon\r\n' - command - data mode on request message
|
|
private readonly byte[] NFCHeadConfig_cpon_req_msg = Encoding.ASCII.GetBytes("cpon\r\n"); //'cpon\r\n' - command - pulse output enable command request message
|
|
private readonly byte[] NFCHeadConfig_cnfc_req_msg = Encoding.ASCII.GetBytes("cnfc\r\n"); //'cnfc\r\n' - command - nfc interface configure request message
|
|
private readonly byte[] NFCHeadConfig_crfid_req_msg = Encoding.ASCII.GetBytes("crfid\r\n"); //'crfid\r\n' - command - rfid interface configure request message
|
|
private readonly byte[] NFCHeadConfig_cadc_req_msg = Encoding.ASCII.GetBytes("cadc\r\n"); //'cadc\r\n' - command - adc sampling start request message
|
|
private byte[] NFCHeadConfig_gw_write_req_msg = Encoding.ASCII.GetBytes("gw00\r\n"); //'gw00\r\n' - write request - gain write request message. here 00 are hex digits and to be replaced with value to be written
|
|
private readonly byte[] NFCHeadConfig_ir_read_req_msg = Encoding.ASCII.GetBytes("ir\r\n"); //'ir\r\n' - read request - interface read request message
|
|
private readonly byte[] NFCHeadConfig_ir_read_res_msg_nfc = Encoding.ASCII.GetBytes("INFC\n"); //'INFC\n' - read response - NFC interface read response message
|
|
private readonly byte[] NFCHeadConfig_ir_read_res_msg_rfid = Encoding.ASCII.GetBytes("IRFID\n"); //'IRFID\n' - read response - RFID interface read response message
|
|
private readonly byte[] NFCHeadConfig_gr_read_req_msg = Encoding.ASCII.GetBytes("gr\r\n"); //'gr\r\n' - read request - gain read request message
|
|
private readonly byte[] NFCHeadConfig_gr_read_res_msg = Encoding.ASCII.GetBytes("GR00\n"); //'GR00\n' - read response - gain read response message. here 00 are hex digits
|
|
private readonly byte[] NFCHeadConfig_opr_read_req_msg = Encoding.ASCII.GetBytes("opr\r\n"); //'opr\r\n' - read request - opamp output read request message
|
|
private readonly byte[] NFCHeadConfig_opr_read_res_msg = Encoding.ASCII.GetBytes("OP0000\n"); //'OP0000\n' - read response - opamp output read response message. here 0000 are hex digits
|
|
private readonly byte[] NFCHeadConfig_fr_read_req_msg = Encoding.ASCII.GetBytes("fr\r\n"); //'fr\r\n' - read request - firmware read request message
|
|
private readonly byte[] NFCHeadConfig_fr_read_res_msg = Encoding.ASCII.GetBytes("V1.23\n"); //'V1.23\n' - read response - firmware read response message. here 1 is major number and 23 is minor numer
|
|
|
|
public NFCHeadConfig(SERIAL_Driver serialdriver)
|
|
{
|
|
_SERIAL_Driver = serialdriver;
|
|
}
|
|
|
|
#region Message Event
|
|
private void OnMessageEvent(string message)
|
|
{
|
|
NfcMessageEventArgs e = new NfcMessageEventArgs();
|
|
e.Message = message;
|
|
|
|
if (MessageEvent != null)
|
|
{
|
|
MessageEvent(this, e);
|
|
}
|
|
e = null;
|
|
}
|
|
#endregion
|
|
|
|
private byte HexToAscii(byte u8HexDigit)
|
|
{
|
|
if (u8HexDigit < 10)
|
|
{
|
|
u8HexDigit += 0x30; //'0' - 0x30
|
|
}
|
|
else if (u8HexDigit < 16)
|
|
{
|
|
u8HexDigit -= 10;
|
|
u8HexDigit += 0x61; //'a' - 0x61
|
|
}
|
|
else
|
|
{
|
|
u8HexDigit = 0xFF;
|
|
}
|
|
return u8HexDigit;
|
|
}
|
|
|
|
static byte AsciiToHex(byte u8AsciiByte)
|
|
{
|
|
if (('0' <= u8AsciiByte) && ('9' >= u8AsciiByte))
|
|
{
|
|
u8AsciiByte -= 0x30; //'0' - 0x30
|
|
}
|
|
else if (('A' <= u8AsciiByte) && ('F' >= u8AsciiByte))
|
|
{
|
|
u8AsciiByte -= 0x41; //'A' - 0x41
|
|
u8AsciiByte += 0xA;
|
|
}
|
|
else if (('a' <= u8AsciiByte) && ('f' >= u8AsciiByte))
|
|
{
|
|
u8AsciiByte -= 0x61; //'a' - 0x61
|
|
u8AsciiByte += 0xA;
|
|
}
|
|
else
|
|
{
|
|
u8AsciiByte = 0xFF;
|
|
}
|
|
return u8AsciiByte;
|
|
}
|
|
|
|
public bool NFCHeadConfig_PulseLedOutputEnable()
|
|
{
|
|
//Switching off data mode or changing to command mode ("cdoff\r\n")
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cdoff_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
return false; // true - success, false - failed
|
|
}
|
|
|
|
//Pulse output enable command ("cpon\r\n")
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cpon_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
return false; // true - success, false - failed
|
|
}
|
|
return true; // true - success, false - failed
|
|
}
|
|
|
|
public bool NFCHeadConfig_DataLedOutputEnable()
|
|
{
|
|
//Switching on data mode ("cdon\r\n")
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cdon_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
return false; // true - success, false - failed
|
|
}
|
|
return true; // true - success, false - failed
|
|
}
|
|
|
|
public ushort NFCHeadConfig_ReadOpampOutput()
|
|
{
|
|
ushort temp = 0;
|
|
byte[] bytesReceived;
|
|
int retryCounter = 0;
|
|
ushort retVal = 0; // 0 - failed, any other value is Opamp Output
|
|
|
|
//Switching off data mode (means command mode is on)
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cdoff_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
retVal = 0;
|
|
OnMessageEvent("NFCHeadConfig_ReadOpampOutput........Failed");
|
|
return retVal; // 0 - failed, any other value is Opamp Output
|
|
}
|
|
|
|
//NFC head is in command mode
|
|
//Sending command 'cadc\r\n' for starting sampling of OP_OUT
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cadc_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
retVal = 0;
|
|
OnMessageEvent("NFCHeadConfig_ReadOpampOutput........Failed");
|
|
return retVal; // 0 - failed, any other value is Opamp Output
|
|
}
|
|
|
|
OnMessageEvent("NFCHeadConfig_ReadOpampOutput........Start");
|
|
|
|
while (retryCounter++ <= NFC_HEAD_CONFIG_MAX_RETRIES)
|
|
{
|
|
//OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_opr_read_req_msg) + "=0x" + Tools.ByteArrayToHexString(NFCHeadConfig_opr_read_req_msg));
|
|
OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_opr_read_req_msg));
|
|
if (_SERIAL_Driver.SendMessage(NFCHeadConfig_opr_read_req_msg, NFCHeadConfig_opr_read_req_msg.Length))
|
|
{
|
|
bytesReceived = _SERIAL_Driver.GetRawData();
|
|
//OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived) + "=0x" + Tools.ByteArrayToHexString(bytesReceived));
|
|
if ((bytesReceived[0] == NFCHeadConfig_opr_read_res_msg[0]) && (bytesReceived[1] == NFCHeadConfig_opr_read_res_msg[1]))
|
|
{
|
|
OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived));
|
|
//'OP' received as first 2 charaters in the response. 3rd to 6th are hex digits of OP_OUT from MSB to LSB respectively
|
|
temp = AsciiToHex(bytesReceived[2]);
|
|
retVal = (ushort)(temp << 12);
|
|
temp = AsciiToHex(bytesReceived[3]);
|
|
retVal += (ushort)(temp << 8);
|
|
temp = AsciiToHex(bytesReceived[4]);
|
|
retVal += (ushort)(temp << 4);
|
|
temp = AsciiToHex(bytesReceived[5]);
|
|
retVal += temp;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent(_SERIAL_Driver.ErrorMessage);
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
if (retVal == 0)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadOpampOutput........Failed");
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadOpampOutput: " + retVal + " ........Success");
|
|
}
|
|
|
|
//Switching data mode on (means command mode is off)
|
|
NFCHeadConfig_cmd_tx(NFCHeadConfig_cdon_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES);
|
|
|
|
return retVal; // 0 - failed, any other value is gain
|
|
}
|
|
|
|
public bool NFCHeadConfig_SetGain(byte gain)
|
|
{
|
|
byte[] bytesReceived;
|
|
int retryCounter = 0;
|
|
bool bRetVal = false;
|
|
|
|
//Switching off data mode (means command mode is on)
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cdoff_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
bRetVal = false;
|
|
OnMessageEvent("NFCHeadConfig_SetGain........Failed");
|
|
return bRetVal; // true - success, false - failed
|
|
}
|
|
|
|
OnMessageEvent("NFCHeadConfig_SetGain........Start");
|
|
|
|
NFCHeadConfig_gw_write_req_msg[2] = HexToAscii((byte)((gain & 0xF0) >> 4)); //Higher nibble of gain
|
|
NFCHeadConfig_gw_write_req_msg[3] = HexToAscii((byte)(gain & 0x0F)); //Lower nibble of gain
|
|
while (retryCounter++ <= NFC_HEAD_CONFIG_MAX_RETRIES)
|
|
{
|
|
//OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_gw_write_req_msg) + "=0x" + Tools.ByteArrayToHexString(NFCHeadConfig_gw_write_req_msg));
|
|
OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_gw_write_req_msg));
|
|
if (_SERIAL_Driver.SendMessage(NFCHeadConfig_gw_write_req_msg, NFCHeadConfig_gw_write_req_msg.Length))
|
|
{
|
|
bytesReceived = _SERIAL_Driver.GetRawData();
|
|
//OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived) + "=0x" + Tools.ByteArrayToHexString(bytesReceived));
|
|
if ((bytesReceived[0] == NFCHeadConfig_OK_res_msg[0]) && (bytesReceived[1] == NFCHeadConfig_OK_res_msg[1])
|
|
&& (bytesReceived[2] == NFCHeadConfig_OK_res_msg[2]))
|
|
{
|
|
OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived));
|
|
//'OK\n' received
|
|
bRetVal = true;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent(_SERIAL_Driver.ErrorMessage);
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
if (bRetVal)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_SetGain........Success");
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_SetGain........Failed");
|
|
}
|
|
|
|
//Switching data mode on (means command mode is off)
|
|
NFCHeadConfig_cmd_tx(NFCHeadConfig_cdon_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES);
|
|
|
|
return bRetVal;
|
|
}
|
|
|
|
public byte NFCHeadConfig_ReadGain()
|
|
{
|
|
byte temp = 0;
|
|
byte[] bytesReceived;
|
|
int retryCounter = 0;
|
|
byte retVal = 0; // 0 - failed, any other value is gain
|
|
|
|
//Switching off data mode (means command mode is on)
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cdoff_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
retVal = 0;
|
|
OnMessageEvent("NFCHeadConfig_ReadGain........Failed");
|
|
return retVal; // 0 - failed, any other value is gain
|
|
}
|
|
|
|
OnMessageEvent("NFCHeadConfig_ReadGain........Start");
|
|
|
|
//NFC head is in command mode
|
|
while (retryCounter++ <= NFC_HEAD_CONFIG_MAX_RETRIES)
|
|
{
|
|
//OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_gr_read_req_msg) + "=0x" + Tools.ByteArrayToHexString(NFCHeadConfig_gr_read_req_msg));
|
|
OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_gr_read_req_msg));
|
|
if (_SERIAL_Driver.SendMessage(NFCHeadConfig_gr_read_req_msg, NFCHeadConfig_gr_read_req_msg.Length))
|
|
{
|
|
bytesReceived = _SERIAL_Driver.GetRawData();
|
|
//OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived) + "=0x" + Tools.ByteArrayToHexString(bytesReceived));
|
|
if ((bytesReceived[0] == NFCHeadConfig_gr_read_res_msg[0]) && (bytesReceived[1] == NFCHeadConfig_gr_read_res_msg[1])
|
|
&& (bytesReceived[4] == NFCHeadConfig_gr_read_res_msg[4]))
|
|
{
|
|
OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived));
|
|
//'GR' received as first 2 charaters in the response. 3rd and 4th are gain as hex digits
|
|
temp = AsciiToHex(bytesReceived[2]);
|
|
retVal = (byte)(temp << 4);
|
|
temp = AsciiToHex(bytesReceived[3]);
|
|
retVal += temp; // 0 - failed, any other value is gain
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent(_SERIAL_Driver.ErrorMessage);
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
if (retVal == 0)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadGain........Failed");
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadGain: " + retVal + " ........Success");
|
|
}
|
|
|
|
//Switching data mode on (means command mode is off)
|
|
NFCHeadConfig_cmd_tx(NFCHeadConfig_cdon_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES);
|
|
|
|
return retVal; // 0 - failed, any other value is gain
|
|
}
|
|
|
|
public bool NFCHeadConfig_SetInterface(bool value)
|
|
{
|
|
byte[] bytesTransmit;
|
|
byte[] bytesReceived;
|
|
int retryCounter = 0;
|
|
bool bRetVal = false;
|
|
|
|
//Switching off data mode (means command mode is on)
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cdoff_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
bRetVal = false;
|
|
OnMessageEvent("NFCHeadConfig_SetInterface........Failed");
|
|
return bRetVal; // true - success, false - failed
|
|
}
|
|
|
|
//NFC head is in command mode
|
|
if (value) //true - NFC, false - RFID
|
|
{
|
|
bytesTransmit = NFCHeadConfig_cnfc_req_msg;
|
|
OnMessageEvent("NFCHeadConfig_SetInterface: NFC........Start");
|
|
}
|
|
else
|
|
{
|
|
bytesTransmit = NFCHeadConfig_crfid_req_msg;
|
|
OnMessageEvent("NFCHeadConfig_SetInterface: RFID........Start");
|
|
}
|
|
|
|
while (retryCounter++ <= NFC_HEAD_CONFIG_MAX_RETRIES)
|
|
{
|
|
//OnMessageEvent("Tx- " + Encoding.ASCII.GetString(bytesTransmit) + "=0x" + Tools.ByteArrayToHexString(bytesTransmit));
|
|
OnMessageEvent("Tx- " + Encoding.ASCII.GetString(bytesTransmit));
|
|
if (_SERIAL_Driver.SendMessage(bytesTransmit, bytesTransmit.Length))
|
|
{
|
|
bytesReceived = _SERIAL_Driver.GetRawData();
|
|
//OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived) + "=0x" + Tools.ByteArrayToHexString(bytesReceived));
|
|
if ((bytesReceived[0] == NFCHeadConfig_OK_res_msg[0]) && (bytesReceived[1] == NFCHeadConfig_OK_res_msg[1])
|
|
&& (bytesReceived[2] == NFCHeadConfig_OK_res_msg[2]))
|
|
{
|
|
OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived));
|
|
//'OK\n' received
|
|
bRetVal = true;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent(_SERIAL_Driver.ErrorMessage);
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
if (bRetVal)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_SetInterface........Success");
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_SetInterface........Failed");
|
|
}
|
|
|
|
//Switching data mode on (means command mode is off)
|
|
NFCHeadConfig_cmd_tx(NFCHeadConfig_cdon_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES);
|
|
|
|
return bRetVal;
|
|
}
|
|
|
|
public byte NFCHeadConfig_ReadInterface()
|
|
{
|
|
byte[] bytesReceived;
|
|
int retryCounter = 0;
|
|
byte retVal = 0; // 0 - failed, 1 - NFC, 2 - RFID
|
|
|
|
//Switching off data mode (means command mode is on)
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cdoff_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
retVal = 0;
|
|
OnMessageEvent("NFCHeadConfig_ReadInterface........Failed");
|
|
return retVal; // 0 - failed, any other value is gain
|
|
}
|
|
|
|
OnMessageEvent("NFCHeadConfig_ReadInterface........Start");
|
|
|
|
//NFC head is in command mode
|
|
while (retryCounter++ < NFC_HEAD_CONFIG_MAX_RETRIES)
|
|
{
|
|
//OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_ir_read_req_msg) + "=0x" + Tools.ByteArrayToHexString(NFCHeadConfig_ir_read_req_msg));
|
|
OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_ir_read_req_msg));
|
|
if (_SERIAL_Driver.SendMessage(NFCHeadConfig_ir_read_req_msg, NFCHeadConfig_ir_read_req_msg.Length))
|
|
{
|
|
bytesReceived = _SERIAL_Driver.GetRawData();
|
|
//OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived) + "=0x" + Tools.ByteArrayToHexString(bytesReceived));
|
|
if ((bytesReceived[0] == NFCHeadConfig_ir_read_res_msg_nfc[0]) && (bytesReceived[1] == NFCHeadConfig_ir_read_res_msg_nfc[1])
|
|
&& (bytesReceived[2] == NFCHeadConfig_ir_read_res_msg_nfc[2]) && (bytesReceived[3] == NFCHeadConfig_ir_read_res_msg_nfc[3]))
|
|
{
|
|
OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived));
|
|
retVal = 1; // 0 - failed, 1 - NFC, 2 - RFID
|
|
break;
|
|
}
|
|
else if ((bytesReceived[0] == NFCHeadConfig_ir_read_res_msg_rfid[0]) && (bytesReceived[1] == NFCHeadConfig_ir_read_res_msg_rfid[1])
|
|
&& (bytesReceived[2] == NFCHeadConfig_ir_read_res_msg_rfid[2]) && (bytesReceived[3] == NFCHeadConfig_ir_read_res_msg_rfid[3])
|
|
&& (bytesReceived[4] == NFCHeadConfig_ir_read_res_msg_rfid[4]))
|
|
{
|
|
OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived));
|
|
retVal = 2; // 0 - failed, 1 - NFC, 2 - RFID
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent(_SERIAL_Driver.ErrorMessage);
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
if (retVal == 0)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadInterface........Failed");
|
|
}
|
|
else if (retVal == 1)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadInterface: NFC ........Success");
|
|
}
|
|
else if (retVal == 2)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadInterface: RFID ........Success");
|
|
}
|
|
|
|
//Switching data mode on (means command mode is off)
|
|
NFCHeadConfig_cmd_tx(NFCHeadConfig_cdon_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES);
|
|
|
|
return retVal; // 0 - failed, 1 - NFC, 2 - RFID
|
|
}
|
|
|
|
public byte[] NFCHeadConfig_ReadFirmwareVersion()
|
|
{
|
|
byte[] bytesReceived = { 0 };
|
|
int retryCounter = 0;
|
|
|
|
//Switching off data mode (means command mode is on)
|
|
if (NFCHeadConfig_cmd_tx(NFCHeadConfig_cdoff_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES) == false)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadFirmwareVersion........Failed");
|
|
return bytesReceived; // 0 - failed
|
|
}
|
|
|
|
OnMessageEvent("NFCHeadConfig_ReadFirmwareVersion........Start");
|
|
|
|
//NFC head is in command mode
|
|
while (retryCounter++ <= NFC_HEAD_CONFIG_MAX_RETRIES)
|
|
{
|
|
//OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_fr_read_req_msg) + "=0x" + Tools.ByteArrayToHexString(NFCHeadConfig_fr_read_req_msg));
|
|
OnMessageEvent("Tx- " + Encoding.ASCII.GetString(NFCHeadConfig_fr_read_req_msg));
|
|
if (_SERIAL_Driver.SendMessage(NFCHeadConfig_fr_read_req_msg, NFCHeadConfig_fr_read_req_msg.Length))
|
|
{
|
|
bytesReceived = _SERIAL_Driver.GetRawData();
|
|
//OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived) + "=0x" + Tools.ByteArrayToHexString(bytesReceived));
|
|
if (bytesReceived[0] == NFCHeadConfig_fr_read_res_msg[0])
|
|
{
|
|
//'V' is received as first character in the response. So, the firmware version string is followed and ended with '\n'
|
|
OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived));
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent(_SERIAL_Driver.ErrorMessage);
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
if (bytesReceived[0] == 0)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadFirmwareVersion........Failed");
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_ReadFirmwareVersion........Success");
|
|
}
|
|
|
|
//Switching data mode on (means command mode is off)
|
|
NFCHeadConfig_cmd_tx(NFCHeadConfig_cdon_req_msg, NFC_HEAD_CONFIG_MAX_RETRIES);
|
|
|
|
return bytesReceived; // 0 - failed, any other value is firmware version
|
|
}
|
|
private bool NFCHeadConfig_cmd_tx(byte[] requestTxMessage, int retries)
|
|
{
|
|
byte[] bytesReceived;
|
|
int retryCounter = 0;
|
|
bool bRetVal = false;
|
|
|
|
if (requestTxMessage == NFCHeadConfig_cpon_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cpon........Start");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cdoff_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cdoff........Start");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cdon_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cdon........Start");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cnfc_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cnfc........Start");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_crfid_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_crfid........Start");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cadc_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cadc........Start");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_crst_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_crst........Start");
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cmd_tx: unidentified command........Failed");
|
|
bRetVal = false;
|
|
return bRetVal;
|
|
}
|
|
|
|
while (++retryCounter <= retries)
|
|
{
|
|
//OnMessageEvent("Tx- " + Encoding.ASCII.GetString(requestTxMessage) + "=0x" + Tools.ByteArrayToHexString(requestTxMessage));
|
|
OnMessageEvent("Tx- " + Encoding.ASCII.GetString(requestTxMessage));
|
|
if (_SERIAL_Driver.SendMessage(requestTxMessage, requestTxMessage.Length))
|
|
{
|
|
bytesReceived = _SERIAL_Driver.GetRawData();
|
|
//OnMessageEvent("Rx- " + Encoding.ASCII.GetString(bytesReceived) + "=0x" + Tools.ByteArrayToHexString(bytesReceived));
|
|
if ((bytesReceived[0] == NFCHeadConfig_OK_res_msg[0]) && (bytesReceived[1] == NFCHeadConfig_OK_res_msg[1])
|
|
&& (bytesReceived[2] == NFCHeadConfig_OK_res_msg[2]))
|
|
{
|
|
OnMessageEvent("Rx- " + Encoding.ASCII.GetString(NFCHeadConfig_OK_res_msg));
|
|
//'OK\n' received
|
|
bRetVal = true;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
OnMessageEvent(_SERIAL_Driver.ErrorMessage);
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
if (bRetVal == true)
|
|
{
|
|
if (requestTxMessage == NFCHeadConfig_cpon_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cpon........Success");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cdoff_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cdoff........Success");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cdon_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cdon........Success");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cnfc_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cnfc........Success");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_crfid_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_crfid........Success");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cadc_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cadc........Success");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_crst_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_crst........Success");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (requestTxMessage == NFCHeadConfig_cpon_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cpon........Failed");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cdoff_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cdoff........Failed");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cdon_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cdon........Failed");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cnfc_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cnfc........Failed");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_crfid_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_crfid........Failed");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_cadc_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_cadc........Failed");
|
|
}
|
|
else if (requestTxMessage == NFCHeadConfig_crst_req_msg)
|
|
{
|
|
OnMessageEvent("NFCHeadConfig_crst........Failed");
|
|
}
|
|
}
|
|
return bRetVal;
|
|
}
|
|
}
|
|
}
|