package com.gary.rfid.proto;

import java.util.Arrays;

import android.content.Context;
import android.hardware.usb.UsbManager;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import com.andorid.MuliModeComdemo.RFID_DEV;
import com.gary.msr.hid.USBOp;

class ST_PCDINFO{
	
	public int     ucNad;
	public int     ucNadEn;/*the picc support NAD*/
	public int     ucCid;/*the identification channel*/
	public int     ucCidEn;/*the picc support CID*/
	public int      uiFsc;/*the maximum size of PICC frame*/
	public int      uiFwi;/*the maximum frame waiting time*/
	public int      uiSfgi;/*the frame guard time*/
    /*the PCD and PICC maintain last i block PCB*/
	public int     ucPcdPcb;
	public int     ucPiccPcb;
    /*rf data buffer*/
	public int      uiPcdTxRNum;
	public int      uiPcdTxRLastBits;
	public int   ucWTXM;
	void clear()//全部成员变量清零
	{
		ucNad=0;
		ucNadEn=0;
		ucCid=0;
		ucCidEn=0;
		uiFsc=0;
		uiFwi=0;
		uiSfgi=0;
		ucPcdPcb=0;
		ucPiccPcb=0;
		uiPcdTxRNum=0;
		uiPcdTxRLastBits=0;
		ucWTXM=0;
	}
}
class tag_info{
	public int opt_step;
	public int uid_length;
	public int tag_type;
	public int[] tag_type_bytes=new int [2];
	public int[] serial_num=new int[12];
	public int[] uncoded_key=new int[6];
	void clear()
	{
		opt_step=0;
		uid_length=0;
		tag_type=0;
		Arrays.fill(tag_type_bytes, 0);
		Arrays.fill(serial_num, 0);
		Arrays.fill(uncoded_key, 0);
	}
}
class statistics_t{
	public int reqa_cnt;
	public int reqa_fail;
	public int reqb_cnt;
	public int reqb_fail;
	public int write_fail;
	public int read_fail;
	public int check_fail;
	public int atqa_fail;
	public int cos_fail;
	public int atqa_cnt;
	public int cos_cnt;
	public int lpcd_cnt;
	public int lpcd_fail;
	void clear()
	{
		reqa_cnt=0;
		reqa_fail=0;
		reqb_cnt=0;
		reqb_fail=0;
		write_fail=0;
		read_fail=0;
		check_fail=0;
		atqa_fail=0;
		cos_fail=0;
		atqa_cnt=0;
		cos_cnt=0;
		lpcd_cnt=0;
		lpcd_fail=0;
		
	}
}
class lpcd_cfg_t{
	
	public int t1;
	public int phase;
	public int phase_offset;
	public int default_amp;
	public int amp;
	public int max_amp;
	public int min_amp;
	public int sense;
	public int[] idx=new int[8];
}
class transceive_buffer{
	public int 	mf_command;
    public int 	mf_length;
    public int[]  mf_data=new int[RFIDPnPacket.MAX_TRX_BUF_SIZE];
}
/*
typedef struct
{
    uint8_t start_code;  // 1d
    uint8_t flag[4];   // PUDI
    uint8_t param[3];   //00 08|58|a8|f8   01
    uint8_t cid;  // 04
} S_ATTRIB;*/
class S_ATTRIB{
    public int start_code;  // 1d
    public int[] flag=new int[4];   // PUDI
    public int[]param=new int[3];   //00 08|58|a8|f8   01
    int cid;  // 04
}


public class RFIDPnPacket {
	
	private static final String TAG = "ZZYLG";
	
	public static int[] PACKET = new int[9 * 1024];
	
	int buildLen;
	
	//protected SerialPort mSerialPort;
	//protected InputStream mInputStream;
	//protected OutputStream mOutputStream;
	////错误列表
	public static final int SUCCESS				= 0;				//操作成功
	public static final int PARAMETER_ERR		= -1001;			//数据包格式错误
	public static final int READ_ERR			= -1002;			//读数据错误
	public static final int WRITE_ERR			= -1003;			//写数据错误
	public static final int COMOPEN_ERR			= -1004;			//打开串口失败
	public static final int FORMART_ERR			= -1005;			//数据包格式错误
	public static final int AUTHENTICATION_ERR	= -1006;			//验证密钥失败
	public static final int READ_FORMAT_ERR		= -1007;			//读取返回的数据格式错误
	public static final int READ_CARD_ERR		= -1008;			//读卡返回状态错误
	public static final int PURSE_FORMAT_ERR	= -1009;			//钱包格式错误
	public static final int TRANSFER_ERR		= -1010;			//钱包扣款充值完成之后的transfer指令失败
	//#define CARD_OF_OTHER_TYPE   -1013			//放上的卡片和调用的API的类型不匹配(调用其他的API来操作这张卡片)
	public static final int CARD_OF_OTHER_TYPE		= -1013;			//钱包扣款充值完成之后的transfer指令失败
	//////头文件中的宏定义
	
	//	#define BIT0   0x01
	public static final int BIT0				= 0x01;
	//	#define BIT1   0x02
	public static final int BIT1				= 0x02;
	//	#define BIT2   0x04
	public static final int BIT2				= 0x04;
	//	#define BIT3   0x08
	public static final int BIT3				= 0x08;
	//	#define BIT4   0x10
	public static final int BIT4				= 0x10;
	//	#define BIT5   0x20
	public static final int BIT5				= 0x20;
	//	#define BIT6   0x40
	public static final int BIT6				= 0x40;
	//	#define BIT7   0x80
	public static final int BIT7				= 0x80;
	//#define CommandReg            0x01
	public static final int CommandReg			= 0x01;
	public static final int PCD_RESETPHASE		= 0x0F;
	//#define TxControlReg          0x14
	public static final int TxControlReg		= 0x14;
	//#define FWI_DEFAULT				4
	public static final int FWI_DEFAULT			= 0x04;
	//#define TPrescalerReg         0x2B
	public static final int TPrescalerReg		= 0x2B;
	//#define TP_FWT_302us			2048
	public static final int TP_FWT_302us		= 2048;
	//#define TModeReg              0x2A
	public static final int TModeReg			= 0x2A;
	//#define TReloadRegH           0x2C
	public static final int TReloadRegH			= 0x2C;
	//#define TReloadRegL           0x2D
	public static final int TReloadRegL			= 0x2D;
	//#define MAX_TRX_BUF_SIZE    255
	public static final int MAX_TRX_BUF_SIZE	= 255;
	//#define PCD_IDLE              0x00               //取消当前命令
	public static final int PCD_IDLE	= 0x00;
	//#define PCD_AUTHENT           0x0E               //验证密钥
	public static final int PCD_AUTHENT	= 0x0E;
	//#define IdleIEn     BIT4
	public static final int IdleIEn	= BIT4;
	//#define TimerIEn    BIT0
	public static final int TimerIEn	= BIT0;
	//#define IdleIRq     BIT4
	public static final int IdleIRq	= BIT4;
	//#define PCD_RECEIVE           0x08               //接收数据
	public static final int PCD_RECEIVE	= 0x08;
	//#define RxIEn       BIT5
	public static final int RxIEn	= BIT5;
	//#define RxIRq       BIT5
	public static final int RxIRq	= BIT5;
	//#define PCD_TRANSMIT          0x04               //发送数据
	public static final int PCD_TRANSMIT	= 0x04;
	//#define TxIEn       BIT6
	public static final int TxIEn	= BIT6;
	//#define TxIRq       BIT6
	public static final int TxIRq	= BIT6;
	//#define PCD_TRANSCEIVE        0x0C               //发送并接收数据
	public static final int PCD_TRANSCEIVE	= 0x0C;
	//#define MI_UNKNOWN_COMMAND              (-23)
	public static final int MI_UNKNOWN_COMMAND	= -23;
	
	//#define IRQ_MODE				0                   //宏定义在sl2x23.h
	public static final boolean IRQ_MODE	= false;    //逻辑判断开关改为boolean型
	//#define ComIEnReg             0x02
	public static final int ComIEnReg	= 0x02;
	//#define ComIrqReg             0x04
	public static final int ComIrqReg	= 0x04;
	//#define DivIrqReg             0x05
	public static final int DivIrqReg	= 0x05;
	
	//#define FIFOLevelReg          0x0A
	public static final int FIFOLevelReg	= 0x0A;
	//#define FIFO_SIZE				64
	public static final int FIFO_SIZE	= 64;
	//#define FIFODataReg           0x09
	public static final int FIFODataReg	= 0x09;
	//#define BitFramingReg         0x0D
	public static final int BitFramingReg	= 0x0D;
	//#define WATER_LEVEL				16
	public static final int WATER_LEVEL	= 16;
	//#define ControlReg            0x0C
	public static final int ControlReg	= 0x0C;
	
	//#define MI_NOTAGERR                     (-1)
	public static final int MI_NOTAGERR	= -1;
	//#define ErrorReg              0x06
	public static final int ErrorReg	= 0x06;
	//#define ErrIRq      BIT1
	public static final int ErrIRq	= BIT1;
	//#define MI_COM_ERR                      (-125)
	public static final int MI_COM_ERR	= -125;
	//#define MI_OK                           0
	public static final int MI_OK	= 0;
	//#define CollErr     BIT3
	public static final int CollErr	= BIT3;
	//#define MI_COLLERR                      (-24)
	public static final int MI_COLLERR	= -24;
	//#define CollReg               0x0E
	public static final int CollReg	= 0x0E;
	//#define CollPos     (BIT0|BIT1|BIT2|BIT3|BIT4)
	public static final int CollPos	= (BIT0|BIT1|BIT2|BIT3|BIT4);
	//#define ProtocolErr BIT0
	public static final int ProtocolErr	= BIT0;
	//#define MI_FRAMINGERR                   (-21)
	public static final int MI_FRAMINGERR	= -21;
	//#define CrcErr      BIT2
	public static final int CrcErr	= BIT2;
	//#define ParityErr   BIT1
	public static final int ParityErr	= BIT1;
	//#define MI_INTEGRITY_ERR                (-35) //完整性错误(crc/parity/protocol)
	public static final int MI_INTEGRITY_ERR	= -35;
	//#define DivIEnReg             0x03
	public static final int DivIEnReg	= 0x03;
	///用户使用错误
	//#define USER_ERROR                      (-127)
	public static final int USER_ERROR	= -127;
	//#define MI_WRONG_PARAMETER_VALUE        (-60)
	public static final int MI_WRONG_PARAMETER_VALUE	= -60;
	
	//#define TxModeReg             0x12
	public static final int TxModeReg	= 0x12;
	///#define RxModeReg             0x13
	public static final int RxModeReg	= 0x13;
	//#define ModWidthReg           0x24
	public static final int ModWidthReg	= 0x24;
	//#define RxSelReg              0x17
	public static final int RxSelReg	= 0x17;
	//#define EXT_TRMOD               0x50
	public static final int EXT_TRMOD	= 0x50;
	//#define ModeReg               0x11
	public static final int ModeReg	= 0x11;
	//#define TxASKReg              0x15
	public static final int TxASKReg	= 0x15;
	//#define Status2Reg            0x08
	public static final int Status2Reg	= 0x08;
	//#define GsNReg                0x27
	public static final int GsNReg	= 0x27;
	//#define AutoTestReg           0x36
	public static final int AutoTestReg	= 0x36;
	//#define ModGsPReg             0x29
	public static final int ModGsPReg	= 0x29;
	//#define TypeBReg              0x1E
	public static final int TypeBReg	= 0x1E;
	//#define ISO14443B_ANTICOLLISION                  0x05
	public static final int ISO14443B_ANTICOLLISION	= 0x05;
	//#define UID_4  4
	public static final int UID_4	= 4;
	//#define MI_BITCOUNTERR                  (-11)
	public static final int MI_BITCOUNTERR	= -11;
	
	//#define TxLastBits  (BIT0|BIT1|BIT2)
	public static final int TxLastBits	= (BIT0|BIT1|BIT2);
	//#define RxAlign     (BIT4|BIT5|BIT6)
	public static final int RxAlign	= (BIT4|BIT5|BIT6);
	//#define WAKEUPA_CMD   0x52
	public static final int WAKEUPA_CMD	= 0x52;
	//#define PICC_ANTICOLL1        0x93               //防冲撞
	public static final int PICC_ANTICOLL1	= 0x93;
	//#define PICC_ANTICOLL2        0x95               //防冲撞
	public static final int PICC_ANTICOLL2	= 0x95;
	//#define PICC_ANTICOLL3        0x97               //防冲撞
	public static final int PICC_ANTICOLL3	= 0x97;
	//#define UID_7  7
	public static final int UID_7	= 7;
	//#define UID_10 10
	public static final int UID_10	= 10;
	//#define PCD_CMD_SLEEP         0x10               //芯片进入休眠状态
	public static final int PCD_CMD_SLEEP	= 0x10;
	
	//#define ISO14443_CL_PROTOCOL_CHAINED              ( 0x10 )
	public static final int ISO14443_CL_PROTOCOL_CHAINED	= 0x10;
	//#define ISO14443_CL_PROTOCOL_CID                  ( 0x8 )
	public static final int ISO14443_CL_PROTOCOL_CID	= 0x8;
	//#define ISO14443_CL_PROTOCOL_NAD                  ( 0x4 )
	public static final int ISO14443_CL_PROTOCOL_NAD	= 0x4;
	//#define ISO14443_CL_PROTOCOL_ISN                  ( 0x1 )
	public static final int ISO14443_CL_PROTOCOL_ISN	= 0x1;
	//#define ISO14443_4_ERR_PROTOCOL  ( -100 )
	public static final int ISO14443_4_ERR_PROTOCOL	= -100;
	//#define ISO14443_PROTOCOL_RETRANSMISSION_LIMITED  ( 2 )
	public static final int ISO14443_PROTOCOL_RETRANSMISSION_LIMITED	= 2;
	//#define TimerIRq    BIT0
	public static final int TimerIRq	= BIT0;
	//#define TP_dFWT					192
	public static final int TP_dFWT	= 192;
	//#define PROTOCOL_ERR                    (-126)
	public static final int PROTOCOL_ERR	= -126;
	
	//#define PICC_RATS             0xE0               //Request for Answer To Select
	public static final int PICC_RATS	= 0xE0;
	//#define FSDI 8 //Frame Size for proximity coupling Device, in EMV test. 身份证必须FSDI = 8
	public static final int FSDI	=  8;
	//#define PICC_CID 0x00 // 0~14 随意指定
	public static final int PICC_CID	=  0x00;
	//#define ISO14443B_ATTRIB                         0x1D
	public static final int ISO14443B_ATTRIB	=  0x1D;
	//#define WaterLevelReg         0x0B
	public static final int WaterLevelReg	=  0x0B;
	//#define ADC_VALUE0 0x84
	public static final int ADC_VALUE0	=  0x84;
	//#define LPCD_CTRL0  0x86
	public static final int LPCD_CTRL0	=  0x86;
	//#define ADC_TIME0  0x82
	public static final int ADC_TIME0	=  0x82;
	//#define ADC_TIME1  0x83
	public static final int ADC_TIME1	=  0x83;
	//#define ADC_TIME0_VAL      (0xff) //time for filed on
	public static final int ADC_TIME0_VAL	=  0xff;
	//#define ADC_CTRL   0x81
	public static final int ADC_CTRL	=  0x81;
	//#define AGC_CTRL   0x5f
	public static final int AGC_CTRL	=  0x5f;
	//#define AGC_TARGET         (0x60) //-0x80~3.3V; //0x60~3.0V
	public static final int AGC_TARGET	=  0x60;
	//#define AGC_RX_MAX         (0x80) //-0x80~3.3V; //0x60~3.0V
	public static final int AGC_RX_MAX	=  0x80;
	//#define AGC_RX_MIN         (0x20) //-0x80~3.3V; //0x60~3.0V
	public static final int AGC_RX_MIN	=  0x20;
	//#define LPCD_PARAM0 0x8e
	public static final int LPCD_PARAM0	=  0x8e;
	//#define LPCD_PARAM1 0x8f
	public static final int LPCD_PARAM1	=  0x8f;
	//#define LPCD_PARAM2 0x90
	public static final int LPCD_PARAM2	=  0x90;
	//#define LPCD_PARAM3 0x91
	public static final int LPCD_PARAM3	=  0x91;
	//#define LPCD_TIME0  0x8a
	public static final int LPCD_TIME0	=  0x8a;
	//#define LPCD_TIME1  0x8b
	public static final int LPCD_TIME1	=  0x8b;
	//#define LPCD_TIME2  0x8d
	public static final int LPCD_TIME2	=  0x8d;
	//#define MI_AUTHERR                      (-4)
	public static final int MI_AUTHERR	=  -4;
	//#define PICC_READ             0x30               //读块
	public static final int PICC_READ	=  0x30;
	//#define PICC_WRITE				0xA0               //写块
	public static final int PICC_WRITE	=  0xA0;
	//#define MI_NOTAUTHERR                   (-10)
	public static final int MI_NOTAUTHERR	=  -10;
	//#define MI_CODEERR                      (-6)
	public static final int MI_CODEERR	=  -6;
	//#define MI_WRITEERR                     (-15)
	public static final int MI_WRITEERR	=  -15;
	//#define PICC_TRANSFER         0xB0               //保存缓冲区中数据
	public static final int PICC_TRANSFER	=  0xB0;
	//#define PICC_RESTORE          0xC2               //调块数据到缓冲区 
	public static final int PICC_RESTORE	=  0xC2;
	
	//public enum EXC_STEP//把枚举类型改成宏定义
    //ORG_IBLOCK = 1,
	public static final int ORG_IBLOCK	= 	1;
    //ORG_ACKBLOCK,
	public static final int ORG_ACKBLOCK	= 2;
    //ORG_NACKBLOCK,
	public static final int ORG_NACKBLOCK	= 3;
    //ORG_SBLOCK,
	public static final int ORG_SBLOCK	= 4;
    //ORG_TRARCV, /*transmitted and received*/
	public static final int ORG_TRARCV	= 5;
    //RCV_IBLOCK,
	public static final int RCV_IBLOCK	= 6;
    //RCV_RBLOCK,
	public static final int RCV_RBLOCK	= 7;
    //RCV_SBLOCK,
	public static final int RCV_SBLOCK	= 8;
    //RCV_INVBLOCK,/*protocol error*/
	public static final int RCV_INVBLOCK	= 9;
    //NON_EVENT /*end*/
	public static final int NON_EVENT	= 10;
	//#define LPCD_DETECT_PERIOD (0x08) //0x5 320ms; //0x8 500ms ; //0x10 1s
	public static final int LPCD_DETECT_PERIOD	= 0x08;
	//#define LPCD_DETECT_SENS   (0x02) //0x2 17mv; //0x4 35mv
	public static final int LPCD_DETECT_SENS	= 0x02;
	
	//蜂鸣器报错延时时间
	//static int BEEPDELAYTIME =300;
	public static final int BEEPDELAYTIME	= 300;
	
	//-----------------------------RD07------------------------------------//
	private int MY_CARD_TYPE; //添加一个全局变量,来判断所放的卡片和实际调用的API是否匹配。'A' 'B' 'V'  'F' 目前分为这4种卡片
	private int recv_temp;
	private int irq_user_flag;
	private int wkup_flag;
	private int recvdata = 0;
	private int recv_data = 0;
	private int demo_step;
	private int g_ret;
	private int card_A;
	private int card_B;
	private int card_F;
	private int card_V;
	//private int sak;
	private int[] sak=new int[2]; //c改JAVA 不能用&符号改为数组
	private int atqa;
	ST_PCDINFO gtPcdModuleInfo = new ST_PCDINFO();
	
	private int g_fwi =0x04;
	//tag_info  g_tag_info;
	tag_info  g_tag_info=new tag_info();
	statistics_t  g_statistics=new statistics_t();
	lpcd_cfg_t lpcd_cfg=new lpcd_cfg_t();

	private int agc_val;
	
	private int[] logBuf=new int[1024];
	private int[] ATQB=new int[16];
	
	//批量传输
	private int[] WriteCmdBuf=new int[256];
	private int WriteCmdBufLen;  ///每次加两个
	
	private int[] gausMaxFrameSizeTable =
	{
	    16,  24,  32,  40,  48,  64,  96,  128, 256,
	    256, 256, 256, 256, 256, 256, 256, 256, 256,
	};
	
	private int[] g_loop_buf=new int[60];//不支持太大的cos指令测试
	private int[] sendBuffer=new int[32];
	private int[] recvBuffer=new int[32];
	// 用唤醒的方法进行训卡
	private int g_ask_block_flag = 0;
	
	/***************************************************************************************************/

	private int sl2523_rfcfg_init;  // 0x26
	private int sl2523_rfcfg1_init;  // 0x45
	private int sl2523_cwgsp_init;  // 0x28
	private int sl2523_modgsp_init; // 0x29
	private int sl2523_cwgsp_exch; // 0x28
	private int sl2523_modgsp_exch; // 0x29

	private int sl2523_cwgsp_tran ; // 0x28
	private int sl2523_modgsp_tran; // 0x28
	private int sl2523_calc_init;   // 0x66

	private int sl2523_digit0_val = 0xf;  // 0x46
	private int sl2523_digit1_val = 0xcc;  // 0x49
	private int  sl2523_emv_mode;
	//volatile uint8_t irq_flag_io;
	private int irq_flag_io;
	
	private int transceive_outLen;	
	
	
	
	
	//---------------------------------------------------------------------------------------//
	
	private void  write_reg(int addr,int RegValue)//
	{
		
		WriteCmdBuf[WriteCmdBufLen++]=addr;
		WriteCmdBuf[WriteCmdBufLen++]=RegValue;
		if(WriteCmdBufLen>=58)//每个包最大放58个数据
		{
			clear();
			buildLen=0;
			PACKET[buildLen++]=0x08;	
			PACKET[buildLen++]=0x3A;				  //写指令
			PACKET[buildLen++]=(WriteCmdBufLen/2)%256;//写指令
			//System.arraycopy(PACKET, buildLen, WriteCmdBuf, 0, WriteCmdBufLen);
			System.arraycopy( WriteCmdBuf, 0, PACKET, buildLen,WriteCmdBufLen);
			buildLen+=WriteCmdBufLen;
			mUSBOp.sendDatarRD07Rfid(PACKET,buildLen);
			Arrays.fill(WriteCmdBuf, 0);
			WriteCmdBufLen=0;
		}
	}
	
	private int read_reg(int addr)
	{
		int iRet;
		if(WriteCmdBufLen>=0)//写缓冲区中有数据
		{
			clear();
			buildLen=0;
			PACKET[buildLen++]=0x08;	
			PACKET[buildLen++]=0x39;				  //写指令
			PACKET[buildLen++]=(WriteCmdBufLen/2)%256;//写指令
			//System.arraycopy(PACKET, buildLen, WriteCmdBuf, 0, WriteCmdBufLen);
			System.arraycopy(WriteCmdBuf, 0, PACKET, buildLen, WriteCmdBufLen);
			buildLen+=WriteCmdBufLen;
			PACKET[buildLen++]=addr;////
			mUSBOp.sendDatarRD07Rfid(PACKET,buildLen);
			Arrays.fill(WriteCmdBuf, 0);
			WriteCmdBufLen=0;
			clear();//////
			iRet=mUSBOp.readDataRD07Rfid(PACKET);
			//return iRet;
			return PACKET[0];
		}
		else
		{
		clear();
		buildLen=0;
		PACKET[buildLen++]=0x08;	
		PACKET[buildLen++]=0x39;				  //读指令
		PACKET[buildLen++]=0x00;
		PACKET[buildLen++]=addr;
		mUSBOp.sendDatarRD07Rfid(PACKET,buildLen);
		clear();
		iRet=mUSBOp.readDataRD07Rfid(PACKET);
		//return iRet;
		return PACKET[0];
		}
		
			
	}
	
	
	private void delay_1ms(int nMSTime )
	{
		if(WriteCmdBufLen>=0)//写缓冲区中有数据
		{
			clear();
			buildLen=0;
			PACKET[buildLen++]=0x08;	
			PACKET[buildLen++]=0x3A;				  //写指令
			PACKET[buildLen++]=(WriteCmdBufLen/2)%256;//写指令
			//System.arraycopy(PACKET, buildLen, WriteCmdBuf, 0, WriteCmdBufLen);
			System.arraycopy( WriteCmdBuf, 0, PACKET, buildLen,WriteCmdBufLen);
			buildLen+=WriteCmdBufLen;
			mUSBOp.sendDatarRD07Rfid(PACKET,buildLen);
			Arrays.fill(WriteCmdBuf, 0);
			WriteCmdBufLen=0;
		}
		SystemClock.sleep(nMSTime);
		
	}
	
	private int Get_Irq_Flag()
	{
		int iRet;
		clear();
		buildLen=0;
		PACKET[buildLen++]=0x08;	
		PACKET[buildLen++]=0x2C;				  //读指令

		mUSBOp.sendDatarRD07Rfid(PACKET,buildLen);
		clear();
		iRet=mUSBOp.readDataRD07Rfid(PACKET);
		//if(iRet>=0&&PACKET[1]==0x01)
		if((iRet>=0)&&(PACKET[0]!=0x00))
			return 1;
		else
			return 0;		
	}
	
	private void set_bit_mask(int reg, int mask)
	{
		int tmp;
		tmp = read_reg(reg);
		write_reg(reg, tmp | mask); 
		
		//int itmp=tmp | mask;
		//return;
		
	}
	
	private void clear_bit_mask(int reg, int mask) {
	    int  tmp;
	    tmp = read_reg(reg);
	    write_reg(reg, tmp & ~mask);  // clear bit mask
	    
	    //int itmp=tmp & ~mask;
	    //return;
	}
	
	private void  pcd_reset() {

	    write_reg(CommandReg, PCD_RESETPHASE);
	}
	
	private void  pcd_antenna_on() {
		
		// TxControlReg=0x14 
	    write_reg(TxControlReg, read_reg(TxControlReg) | 0x03); //Tx1RFEn=1 Tx2RFEn=1
	}
	private void pcd_antenna_off() {
	    write_reg(TxControlReg, read_reg(TxControlReg) & (~0x03));
	    //Memo1->Lines->Add("pcd_antenna_off is OK");
	}
	
	private void pcd_default_info()   // COS_TEST
	{

		gtPcdModuleInfo.clear();
		gtPcdModuleInfo.uiFsc = 32;
	    gtPcdModuleInfo.uiFwi = FWI_DEFAULT;
	    gtPcdModuleInfo.uiSfgi = 0;
	    gtPcdModuleInfo.ucNadEn = 0;
	    gtPcdModuleInfo.ucCidEn = 0;
	    gtPcdModuleInfo.ucWTXM = 1; //multi

	}
	
	private void pcd_set_tmo(int fwi) {
	    write_reg(TPrescalerReg, (TP_FWT_302us) & 0xFF);
	    write_reg(TModeReg, BIT7 | (((TP_FWT_302us) >> 8) & 0xFF));
	    write_reg(TReloadRegL, (1 << fwi)  & 0xFF);
	    write_reg(TReloadRegH, ((1 << fwi)  & 0xFF00) >> 8);
	    
	}
	private int pcd_com_transceive(transceive_buffer pi)
	{
		int[] tmpBuf=new int[5];
		int  recebyte;
		int  status;
		int  irq_en;
		int  wait_for;
		int  last_bits;
		int  j;
		int  val;
		int  err;
		int irq_inv;
		int  len_rest;
		int  len;
	    len = 0;
	    len_rest = 0;
	    err = 0;
	    recebyte = 0;
	    irq_en = 0;
	    wait_for = 0;
	    irq_flag_io = 0;
	    switch(pi.mf_command){
        case PCD_IDLE:
            irq_en   = 0x00;
            wait_for = 0x00;
            break;
        case PCD_AUTHENT:
            irq_en = IdleIEn | TimerIEn;
            wait_for = IdleIRq;
            break;
        case PCD_RECEIVE:
            irq_en   = RxIEn | IdleIEn;
            wait_for = RxIRq;
            recebyte = 1;
            break;
        case PCD_TRANSMIT:
            irq_en   = TxIEn | IdleIEn;
            wait_for = TxIRq;
            break;
        case PCD_TRANSCEIVE:
            irq_en = RxIEn | TimerIEn | TxIEn;
            //irq_en = RxIEn | IdleIEn | TxIEn;
            wait_for = RxIRq ;
            recebyte = 1;
            break;
        default:
            pi.mf_command = MI_UNKNOWN_COMMAND;
            break;
    }
    if((pi.mf_command != MI_UNKNOWN_COMMAND)
 	       && (((pi.mf_command == PCD_TRANSCEIVE || pi.mf_command == PCD_TRANSMIT) && pi.mf_length > 0)
 	           || (pi.mf_command != PCD_TRANSCEIVE && pi.mf_command != PCD_TRANSMIT))
 	      ) {
	 			//TRACE_LOG("pcd_com_transceive step 1 进选项");
	 	        write_reg(CommandReg, PCD_IDLE);
	 	        //irq_inv = read_reg(ComIEnReg) & BIT7; //zzy liao
	 			if(IRQ_MODE){
	 	            irq_inv = 0x00;//高电平触发
	 	        } else {
	 	            irq_inv = 0x80;//低电平触发
	 	        }
	 	        write_reg(ComIEnReg, irq_inv | irq_en | BIT0);
	 	        write_reg(ComIrqReg, 0x7F); //Clear INT
	 	        write_reg(DivIrqReg, 0x7F); //Clear INT
	 	        /*
	 	        //Flush Fifo
	 			try{
	 				set_bit_mask(FIFOLevelReg, BIT7);
	 			}
	 			catch (exception &e) {
	 				//TRACE_LOG("这里try进异常了");
	 			}*/
	 	       set_bit_mask(FIFOLevelReg, BIT7);
	 	        if(pi.mf_command == PCD_TRANSCEIVE || pi.mf_command == PCD_TRANSMIT || pi.mf_command == PCD_AUTHENT) {
	 	            

	 				//TRACE_LOG("pcd_com_transceive step 1-2");
	 	            len_rest = pi.mf_length;
	 	            if(len_rest >= FIFO_SIZE) {
	 	                len = FIFO_SIZE;
	 	            } else {
	 	                len = len_rest;	//zzy
	 	            }
	 	            for(j = 0; j < len; j++) {
	 	                write_reg(FIFODataReg, pi.mf_data[j]);
	 	            }
	 	            len_rest -= len;
	 	            if(len_rest != 0) {
	 	                write_reg(ComIrqReg, BIT2);   // clear LoAlertIRq
	 	                set_bit_mask(ComIEnReg, BIT2);// enable LoAlertIRq
	 	            }
	 	            write_reg(CommandReg, pi.mf_command);
	 	            if(pi.mf_command == PCD_TRANSCEIVE) {
	 	                set_bit_mask(BitFramingReg, 0x80);
	 	            }
	 	            while(len_rest != 0) {
	 	             

	 	                //while(irq_flag_io == 0);//第一次判此状态
	 					tmpBuf[0]=Get_Irq_Flag();
	 					//TRACE_DATA("Get_Irq_Flag 1 value  = ", tmpBuf, 1);
	 					while(Get_Irq_Flag()==0);
	 						irq_flag_io = 0;
	 	                if(len_rest > (FIFO_SIZE - WATER_LEVEL)) {
	 	                    len = FIFO_SIZE - WATER_LEVEL;
	 	                } else {
	 	                    len = len_rest;//zzy
	 	                }
	 	                for(j = 0; j < len; j++) {
	 	                    write_reg(FIFODataReg, pi.mf_data[pi.mf_length - len_rest + j]);
	 	                }
	 	                write_reg(ComIrqReg, BIT2);
	 	                len_rest -= len;
	 	                if(len_rest == 0) {
	 	                    clear_bit_mask(ComIEnReg, BIT2);// disable LoAlertIRq
	 	                }
	 	            }
	 	            if(pi.mf_command != PCD_AUTHENT) {

	 	                //irq_flag_io=1;//zzy 霸蛮
	 					tmpBuf[0]=Get_Irq_Flag();
	 					//TRACE_DATA("Get_Irq_Flag 1 value  = ", tmpBuf, 1);
	 	                //while(irq_flag_io == 0);
	 	                while(Get_Irq_Flag()==0);
	 	                irq_flag_io = 0;
	 					//TRACE_LOG("Get_Irq_Flag 1 过来了");
	 	                val = read_reg(ComIrqReg);
	 	               //val = read_reg(ComIrqReg);//ZZY
	 	                //if(val & TxIRq) {
	 	               if((val & TxIRq)!=0) {
	 	                    write_reg(ComIrqReg, TxIRq);
	 	                    val = 0x7F & read_reg(FIFOLevelReg);
	 	                }
	 	            }
	 	        }
	 	       if(PCD_RECEIVE == pi.mf_command) {
	 	            set_bit_mask(ControlReg, BIT6);// TStartNow
	 	        }
	 	      //val = read_reg(ComIrqReg);//ZZY
	 	        len_rest = 0; // bytes received
	 			irq_inv^=BIT7;
	 			write_reg(ComIEnReg, irq_inv);
	 			//val = read_reg(ComIrqReg);//ZZY
	 	        write_reg(ComIEnReg, irq_inv | irq_en | BIT0);
	 	       //val = read_reg(ComIrqReg);//ZZY
	 	        write_reg(ComIrqReg, BIT3);    // clear HoAlertIRq
	 	       //val = read_reg(ComIrqReg);//ZZY
	 	        set_bit_mask(ComIEnReg, BIT3); // enable HoAlertIRq
	 	       //val = read_reg(ComIrqReg);//ZZY
	 	        //while(1) {
	 	       while(true) {
	 	         
	 	            //irq_flag_io=1;//zzy 霸蛮
	 	            //while(irq_flag_io == 0);
	 				tmpBuf[0]=Get_Irq_Flag();
	 					//TRACE_DATA("Get_Irq_Flag 2 value  = ", tmpBuf, 1);
	 	            while(Get_Irq_Flag()==0);
	 					irq_flag_io = 0;
	 				//TRACE_LOG("Get_Irq_Flag 2 过来了");
	 	            val = read_reg(ComIrqReg);
	 	            //if((val & BIT3) && !(val & BIT5)) {
	 	           if(((val & BIT3)!=0) && ((val & BIT5)==0)) { //译留意
	 	                clear_bit_mask(ComIEnReg, RxIEn);
	 	                if(len_rest + FIFO_SIZE - WATER_LEVEL > 255) {
	 	                    break;
	 	                }
	 	                for(j = 0; j < FIFO_SIZE - WATER_LEVEL; j++) {
	 	                    pi.mf_data[len_rest + j] = read_reg(FIFODataReg);
	 	                }
	 	                write_reg(ComIrqReg, BIT3);
	 	                len_rest += FIFO_SIZE - WATER_LEVEL;
	 	                set_bit_mask(ComIEnReg, RxIEn);
	 	            } else {
	 	                clear_bit_mask(ComIEnReg, BIT3);//disable HoAlertIRq
	 	                break;
	 	            }
	 	        }
	 	        val = read_reg(ComIrqReg);
	 	       //val=100;
	 	        write_reg(ComIrqReg, val);
	 	       if((val & BIT0)!=0)     //非0为真
	 	    	{
	 	            status = MI_NOTAGERR;
	 	    	  //status = -105;
	 	        } else {
	 	            err = read_reg(ErrorReg);
	 	            //if(!(err & 0x7f)) {  // if not real errirq, except wrrE
	 	           if((err & 0x7f)==0) {
	 	                val &= ~ErrIRq;
	 	            }
	 	            err &= 0x7f;  // clear bit7 wrErr
	 	            status = MI_COM_ERR;
	 	            //if((val & wait_for) && (val & irq_en)) {
	 	           if(((val & wait_for)!=0) && ((val & irq_en)!=0)) {
	 	                //if(!(val & ErrIRq)) {
	 	        	  if((val & ErrIRq)==0) {
	 	                    status = MI_OK;
	 	                    //if(recebyte) {
	 	                   if(recebyte!=0) {
	 	                        val = 0x7F & read_reg(FIFOLevelReg);
	 	                        last_bits = read_reg(ControlReg) & 0x07;
	 	                        if(len_rest + val > MAX_TRX_BUF_SIZE) {
	 	                            status = MI_COM_ERR;
	 	                        } else {
	 	                            //if(last_bits && val) {
	 	                        	if((last_bits!=0) && (val!=0)) {
	 	                                pi.mf_length = (val - 1) * 8 + last_bits;
	 	                            } else {
	 	                                pi.mf_length = val * 8;
	 	                            }
	 	                            pi.mf_length += len_rest * 8;	 	                         
	 	                            if(val == 0) {
	 	                                val = 1;
	 	                            }
	 	                            for(j = 0; j < val; j++) {
	 	                                pi.mf_data[len_rest + j] = read_reg(FIFODataReg);
	 	                            }	 	         
	 	                        }
	 	                    }
	 	                } //else if((err & CollErr) && (!(read_reg(CollReg) & BIT5))) {
	 	        	  	else if(((err & CollErr)!=0) && ((read_reg(CollReg) & BIT5))==0) {
	 	                    //a bit-collision is detected
	 	                    status = MI_COLLERR;
	 	                    //if(recebyte) {
	 	                   if(recebyte!=0) {
	 	                        val = 0x7F & read_reg(FIFOLevelReg);
	 	                        last_bits = read_reg(ControlReg) & 0x07;
	 	                        if(len_rest + val > MAX_TRX_BUF_SIZE) {
	 	                            
	 	                        } else {
	 	                            //if(last_bits && val) {
	 	                        	if((last_bits!=0) && (val!=0)) {
	 	                                pi.mf_length = (val - 1) * 8 + last_bits;
	 	                            } else {
	 	                                pi.mf_length = val * 8;
	 	                            }
	 	                            pi.mf_length += len_rest * 8;	 	                          
	 	                            if(val == 0) {
	 	                                val = 1;
	 	                            }
	 	                            for(j = 0; j < val; j++) {
	 	                                pi.mf_data[len_rest + j + 1] = read_reg(FIFODataReg);
	 	                            }	 	                           
	 	                        }
	 	                    }
	 	                    pi.mf_data[0] = (read_reg(CollReg) & CollPos);
	 	                    if(pi.mf_data[0] == 0) {
	 	                        pi.mf_data[0] = 32;
	 	                    }	 	           
	 	                    pi.mf_data[0]--;
	 	                } //else if((err & CollErr) && (read_reg(CollReg) & BIT5)) {
	 	        	  	else if(((err & CollErr)!=0) && ((read_reg(CollReg) & BIT5))!=0) {
	 	                    
	 	                } //else if(err & (ProtocolErr)) {
	 	        	  	else if((err & (ProtocolErr))!=0) {
	 	                    status = MI_FRAMINGERR;
	 	                } //else if((err & (CrcErr | ParityErr)) && !(err & ProtocolErr)) {
	 	        	  	else if(((err & (CrcErr | ParityErr))!=0) && ((err & ProtocolErr)==0)) {
	 	                    //EMV  parity err EMV 307.2.3.4
	 	                    val = 0x7F & read_reg(FIFOLevelReg);
	 	                    last_bits = read_reg(ControlReg) & 0x07;
	 	                    if(len_rest + val > MAX_TRX_BUF_SIZE) {
	 	                        status = MI_COM_ERR;	 	                       
	 	                    } else {
	 	                        //if(last_bits && val) {
	 	                    	if((last_bits!=0) && (val!=0)) {
	 	                            pi.mf_length = (val - 1) * 8 + last_bits;
	 	                        } else {
	 	                            pi.mf_length = val * 8;
	 	                        }
	 	                        pi.mf_length += len_rest * 8;
	 	                    }
	 	                    
	 	                    
	 	                    status = MI_INTEGRITY_ERR;
	 	                } else {
	 	                   
	 	                    status = MI_INTEGRITY_ERR;
	 	                }
	 	            } else {
	 	                status = MI_COM_ERR;
	 	                
	 	            }
	 	        }
	 	        set_bit_mask(ControlReg, BIT7);// TStopNow =1
	 	        write_reg(ComIrqReg, 0x7F);
	 	        write_reg(DivIrqReg, 0x7F);
	 	        clear_bit_mask(ComIEnReg, 0x7F);
	 	        clear_bit_mask(DivIEnReg, 0x7F);
	 	        write_reg(CommandReg, PCD_IDLE);
    	}
    else {
        status = USER_ERROR;
    	}
    return status;		
	}

	//////////////////////////////////////////////////////////////////////
	//SLOT-MARKER  pcd插槽标记
	//////////////////////////////////////////////////////////////////////
	//int8_t WINAPI pcd_slot_marker(uint8_t N, uint8_t *ATQB)
	private int pcd_slot_marker(int N, int []ATQB)
	{
		int status;
		transceive_buffer  mf_com_data=new transceive_buffer();
		//transceive_buffer  *pi;
		//pi = &mf_com_data;
		
	
		pcd_set_tmo(5);
		
		//if(!N || N>15)
		if((N==0) || (N>15))
		{
		status = MI_WRONG_PARAMETER_VALUE;
		}
		else
		{
		mf_com_data.mf_command = PCD_TRANSCEIVE;
		mf_com_data.mf_length = 1;
		mf_com_data.mf_data[0] = 0x05 |(N << 4); // APn code
		
		//status = pcd_com_transceive(pi);
		status = pcd_com_transceive(mf_com_data);
		
		if(status != MI_OK && status != MI_NOTAGERR)
		{
			status = MI_COLLERR;
		}
		if(status == MI_OK && mf_com_data.mf_length != 96)
		{
			status = MI_COM_ERR;
		}
			if(status == MI_OK)
			{
			//memcpy(ATQB, &mf_com_data.mf_data[0], 16);
			System.arraycopy(mf_com_data.mf_data, 0, ATQB, 0, 16);
			pcd_set_tmo(ATQB[11]>>4); // set FWT
			g_fwi = ATQB[11]>>4;
			}
		}
		
		return status;
	}
	
	private void pcd_set_rate(int rate) {
	    int val, rxwait;
	    switch(rate) {
	        case 1:
	            clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
	            clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
	            write_reg(ModWidthReg, 0x26);//Miller Pulse Length
	            write_reg(RxSelReg, 0x88);
	            break;
	        case 2:
	            clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
	            set_bit_mask(TxModeReg, BIT4);
	            clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
	            set_bit_mask(RxModeReg, BIT4);
	            write_reg(ModWidthReg, 0x12);//Miller Pulse Length
	            val = read_reg(RxSelReg);
	            rxwait = ((val & 0x3F) * 2);
	            if(rxwait > 0x3F) {
	                rxwait = 0x3F;
	            }
	            write_reg(RxSelReg, (rxwait | (val & 0xC0)));
	            break;
	        case 3:
	            clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
	            set_bit_mask(TxModeReg, BIT5);
	            clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
	            set_bit_mask(RxModeReg, BIT5);
	            write_reg(ModWidthReg, 0x0A);//Miller Pulse Length
	            val = read_reg(RxSelReg);
	            rxwait = ((val & 0x3F) * 4);
	            if(rxwait > 0x3F) {
	                rxwait = 0x3F;
	            }
	            write_reg(RxSelReg, (rxwait | (val & 0xC0)));
	            break;
	        case 4:
	            clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
	            set_bit_mask(TxModeReg, BIT4 | BIT5);
	            clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
	            set_bit_mask(RxModeReg, BIT4 | BIT5);
	            write_reg(ModWidthReg, 0x05);//Miller Pulse Length
	            val = read_reg(RxSelReg);
	            rxwait = ((val & 0x3F) * 8);
	            if(rxwait > 0x3F) {
	                rxwait = 0x3F;
	            }
	            write_reg(RxSelReg, (rxwait | (val & 0xC0)));
	            break;
	        default:
	            clear_bit_mask(TxModeReg, BIT4 | BIT5 | BIT6);
	            clear_bit_mask(RxModeReg, BIT4 | BIT5 | BIT6);
	            write_reg(ModWidthReg, 0x26);//Miller Pulse Length
	            break;
	    }
		
	}
	//int8_t WINAPI pcd_transceive(uint8_t *in, uint8_t inlen, uint8_t *out, uint8_t *outlen)
	private int pcd_transceive(int[] in, int inlen, int[] out, int outlen)
	{
	    int status = -1;
		//int status = -10003;
		//transceive_buffer  mf_com_data;
	    transceive_buffer  mf_com_data=new transceive_buffer();
	    pcd_set_tmo(4);
	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length = inlen;
	    //memcpy(mf_com_data.mf_data, in, inlen);
	    System.arraycopy(in, 0, mf_com_data.mf_data, 0, inlen);
	    pcd_set_tmo(4);
	    status = pcd_com_transceive(mf_com_data);
	    if(status == MI_OK)
	    {
	        //*outlen = mf_com_data.mf_length / 8 + !!(mf_com_data.mf_length % 8);
	    	//transceive_outLen=mf_com_data.mf_length / 8 + !!(mf_com_data.mf_length % 8); //这个非非
	    	transceive_outLen=mf_com_data.mf_length / 8 + (mf_com_data.mf_length % 8);
	    	
	        //memcpy(out, mf_com_data.mf_data, *outlen);
	        System.arraycopy(mf_com_data.mf_data, 0, out, 0, transceive_outLen);
	    }
	    return status;
	}
	
	private int pcd_request_v(int[] uid) {
	    int[] command = {0x26, 0x01, 0x00};
	    int[] pbuf=new int[256];
	    int len=0;
	    //int status = pcd_transceive(command, sizeof(command), pbuf, &len);
	    int status = pcd_transceive(command, 3, pbuf, len);//这里的len实际没用，实际用全局变量transceive_outLen
	    if(status == MI_OK) {
	        int i = 1;
	        g_tag_info.uid_length = 8;
	        System.arraycopy(pbuf, 2, uid, 0, 8);
	        //memcpy(uid, &pbuf[2], 8);
	        /*printf("UID  : ");
	        for(i = 1; i < 9; i++) {
	            printf("%02x ", pbuf[len - i]);
	        }
	        printf("\r\n");
	        printf("AFI  : %02x\r\n", pbuf[0]);
	        printf("DSFID: %02x\r\n", pbuf[1]);*/
	    }
	    return status;
	}
	
	/**
	 * @brief  the config of type A and type B
	 * @param  type: 'A' A card ,  'B' B card
	 * @param
	 * @retval
	 */
	private int pcd_config(int type) {
	    if('A' == type) {
	        write_reg(0x3f, 0x1);   // 11 // CRC seed:6363
	        write_reg(EXT_TRMOD, 0x00);//12 //Tx Framing type-v
	        write_reg(0x3f, 0x0);//13 //Rx framing type-v
	        
	        write_reg(ModeReg, 0x3D);   // CRC seed:6363
	        write_reg(TxModeReg, 0x00); // Tx Framing A
	        write_reg(RxModeReg, 0x00); // Rx framing A
	        write_reg(TxASKReg, 0x40);  // typeA
	        write_reg(BitFramingReg, 0x00); //TxLastBits=0
	    } else if('B' == type) {
	        write_reg(0x3f, 0x1);   // 11 // CRC seed:6363
	        write_reg(EXT_TRMOD, 0x00);//12 //Tx Framing type-v
	        write_reg(0x3f, 0x0);//13 //Rx framing type-v

	        write_reg(ModeReg, 0x3F);   // CRC seed:FFFF
	        write_reg(TxModeReg, 0x83); // Tx Framing B
	        write_reg(RxModeReg, 0x83); // Rx framing B
	        write_reg(TxASKReg, 0x00);  // typeB
	        write_reg(BitFramingReg, 0x00); //TxLastBits=0
	        
	        write_reg(Status2Reg, 0x00);// clear MFCrypto1On
	        write_reg(GsNReg, 0xF8);
	        write_reg(AutoTestReg, 0x00);
	        write_reg(ModGsPReg, sl2523_modgsp_init);
	        /* improve efficiency of read B card */
	        write_reg(TypeBReg, 0x13);  // 0xc3
//	        write_reg(0x46,0x0f);
//	        write_reg(0x47,0x6f);
//	        write_reg(0x3f,0x0);
	    } else if('V' == type) {
	        write_reg(0x3f, 0x1);   // 11 // CRC seed:6363
	        write_reg(EXT_TRMOD, 0x22);//12 //Tx Framing type-v
	        write_reg(0x3f, 0x0);//13 //Rx framing type-v
	        
	        write_reg(ModeReg, 0x3D);   // CRC seed:6363
	        write_reg(TxModeReg, 0x82); // Tx Framing B
	        write_reg(RxModeReg, 0x82); // Rx framing B
	        write_reg(TxASKReg, 0x40);  // typeA
	        write_reg(BitFramingReg, 0x00); //TxLastBits=0
	    } /*else if('F' == type) {
	        write_reg(0x3f, 0x1);   // 11 //
	        write_reg(EXT_TRMOD, 0x11);//12 //
	        write_reg(0x3f, 0x0);//13 //
	        
	        write_reg(ModeReg, 0x3D);   // CRC seed:6363
	        write_reg(TxModeReg, 0x91); // txcrc_en
	        write_reg(RxModeReg, 0x91); // rxcrc_en
	        write_reg(TxASKReg, 0x00);  // typeA
	        write_reg(BitFramingReg, 0x00); //TxLastBits=0
	        // set_bit_mask(NFCPHY_FELICA_CTRL, GPIO6);  // stop receving by length in frame header, not by judging subcarrier off
	        
	        //write_reg(0x28,0x18);//GAIM  <6:4>
	        //write_reg(0x29,0x18);//GAIM  <6:4>
	        
	        write_reg(0x26,0x18);//GAIM  <6:4>
	        write_reg(0x3f, 0x1);
	        write_reg(0x45,0x40);//迟滞区间<6:4>
	        write_reg(0x61,0x12);//高通滤波器带宽<3:2>
	        write_reg(0x3f, 0x0);
	    } */else if('F' == type) {//廖总的文件
	        write_reg(0x3f, 0x1);
	        write_reg(EXT_TRMOD, 0x11);
	        write_reg(0x3f, 0x0);
	        write_reg(ModeReg, 0x3D);       //CRC seed:6363
	        write_reg(TxModeReg, 0x91);     //txcrc_en
	        write_reg(RxModeReg, 0x91);     //rxcrc_en
	        write_reg(TxASKReg, 0x00);      //typeA
	        write_reg(BitFramingReg, 0x00); //TxLastBits=0
			
	        write_reg(0x28, 0x24); //0x03
	        write_reg(0x29, 0x07); //0x03
	        write_reg(0x26, 0x54);//GAIM  <6:4>
	        write_reg(0x3f, 0x1);
	        write_reg(0x45, 0x44);//迟滞区间<6:4>
	        //write_reg(0x5f, 0x84); //开启AGC功能 0x88
	        write_reg(0x61, 0x16); //高通滤波器带宽<3:2>
	        write_reg(0x3f, 0x0);
			
	    }
		else {
	        return USER_ERROR;
	    }
	    return MI_OK;
	}
	
	
	private int pcd_request_f(int[] uid) {
	    int []command = {0x06, 0x00, 0xFF, 0xFF, 0x10, 0x00};
	    int []pbuf=new int[256];
	    int len=0;
	    int status = pcd_transceive(command, 6, pbuf, len);
	    if(status == MI_OK) {
	        g_tag_info.uid_length = 8;
	        //memcpy(uid, &pbuf[2], 8);
	        System.arraycopy(pbuf, 2, uid, 0, 8);
	        //PRINT_BUF("UID: ", pbuf + 2, 8);
	        //PRINT_BUF("PMM: ", pbuf + 10, 8);
	        //PRINT_BUF("Primary System Code: ", pbuf + 18, 2);
	    }
	    return status;
	}
	
	//////////////////////////////////////////////////////////////////////
	//函数原型:    char pcd_request_b(u8 req_code, u8 AFI, u8 N, u8 *ATQB)
	//函数功能:    B型卡请求
	//入口参数:    req_code                // 请求代码 ISO14443_3B_REQIDL 0x00 -- 空闲的卡
	////          ISO14443_3B_REQALL 0x08 -- 所有的卡
	//AFI                     // 应用标识符，0x00：全选
	//N                       // 时隙总数,取值范围0--4。
	//出口参数:    *ATQB                   // 请求应答，11字节
	//返 回 值:    STATUS_SUCCESS -- 成功；其它值 -- 失败。
	//说    明:    -
	//////////////////////////////////////////////////////////////////////
	private int pcd_request_b(int req_code, int AFI, int N, int []ATQB)
	{
		int  status;
		transceive_buffer  mf_com_data=new transceive_buffer();
		//transceive_buffer  *pi;
		//pi=&mf_com_data;
		
		
		pcd_set_tmo(5);
		
		mf_com_data.mf_command = PCD_TRANSCEIVE;
		mf_com_data.mf_length = 3;
		mf_com_data.mf_data[0] = ISO14443B_ANTICOLLISION;              // APf code
		mf_com_data.mf_data[1] = AFI;                //
		mf_com_data.mf_data[2] = (req_code & 0x08) | (N&0x07);  // PARAM
		
		status = pcd_com_transceive(mf_com_data);
		
		if(status  !=MI_OK && status !=MI_NOTAGERR)
		{
			status = MI_COLLERR;
		}
		if(status == MI_OK && mf_com_data.mf_length != 96)
		{
			status = MI_COM_ERR;
		}
		if(status == MI_OK)
		{
			//memcpy(ATQB, &mf_com_data.mf_data[0], 16);
			System.arraycopy(mf_com_data.mf_data, 0, ATQB, 0, 16);
			pcd_set_tmo(ATQB[11]>>4); // set FWT
			g_fwi = (ATQB[11]>>4);
		}
		return status;
	}
	
	private int reqb_wakeup(int []guid)
	{
		int  status=0;

		int  i;
		int  cnt;
		int  req_code;
//	    uint8_t dsi_dri; // param [1]
	    pcd_default_info();
	    req_code = 8;//pcmd[1];
	    cnt = 1;//应用中 可以使用轮询N次
	    status = -1;
	    //status = -100001;
	    //while(cnt--)
	    while((cnt--)>0)
	    {
	        status = pcd_request_b(req_code, 0, 0, ATQB);
	        if(status == MI_COLLERR)    // 有冲突，超过一张卡
	        {
	            if((status = pcd_request_b(req_code, 0, 2, ATQB)) != MI_OK)
	            {
	                for(i = 1; i < 4; i++)
	                {
	                    if((status = pcd_slot_marker(i, ATQB)) == MI_OK)
	                    {
	                        break;
	                    }
	                }
	                if(status == MI_OK)
	                {
	                    break;
	                }
	            }
	            else
	            {
	                break;
	            }
	        }
	        else if(status == MI_OK)
	        {
	            //PRINT_BUF("ATQB:", ATQB, 16);
	            //PRINT_BUF("UID :", ATQB + 1, 4);
	            g_tag_info.uid_length = UID_4;
	            //memcpy(guid, ATQB + 1, 4);
	            System.arraycopy(ATQB, 1, guid, 0, 4);
	            //PRINT_BUF("Application :", ATQB + 5, 4);
	            //PRINT_BUF("ProtocolInfo:", ATQB + 9, 3);
	            break;
	        }
	    }   
	    return status;
	}
	/**
	 ****************************************************************
	 * @brief pcd_request()
	 *
	 * 功    能：寻卡
	 *
	 * @param: req_code[IN]:寻卡方式
	 *                0x52 = 寻感应区内所有符合14443A标准的卡
	 *                0x26 = 寻未进入休眠状态的卡
	 * @param: ptagtype[OUT]：卡片类型代码
	 *                0x4400 = Mifare_UltraLight
	 *                0x4400 = Mifare_One(S50_0)
	 *                0x0400 = Mifare_One(S50_3)
	 *                0x0200 = Mifare_One(S70_0)
	                  0x4200 = Mifare_One(S70_3)
	 *                0x0800 = Mifare_Pro
	 *                0x0403 = Mifare_ProX
	 *                0x4403 = Mifare_DESFire
	 *
	 * @return: 成功返回MI_OK
	 * @retval:
	 ****************************************************************
	 */
	private int pcd_request(int req_code, int []ptagtype)
	{
	    int status;
	    transceive_buffer mf_com_data=new transceive_buffer();
	    //transceive_buffer  *pi;
	    //pi = &mf_com_data;

	    
	    write_reg(BitFramingReg,0x07);  // Tx last bytes = 7

	    clear_bit_mask(TxModeReg, BIT7); //不使能发送crc
	    clear_bit_mask(RxModeReg, BIT7); //不使能接收crc
	    clear_bit_mask(Status2Reg, BIT3);//清零MF crypto1认证成功标记
	    pcd_set_tmo(4);

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length = 1;
	    mf_com_data.mf_data[0] = req_code;
		//TRACE_LOG("pcd_com_transceive 1");
	    status = pcd_com_transceive(mf_com_data);
		//TRACE_LOG("pcd_com_transceive 2");

	    if((status==0) && (mf_com_data.mf_length != 0x10))
	    {
	        status = MI_BITCOUNTERR;
	    }
	    //*ptagtype = mf_com_data.mf_data[0];
	    //*(ptagtype + 1) = mf_com_data.mf_data[1];
	    ptagtype[0]=mf_com_data.mf_data[0];
	    ptagtype[1]=mf_com_data.mf_data[1];

	

	    return status;
	}
	
	
	/**
	 ****************************************************************
	 * @brief pcd_select()
	 *
	 * 选定一张卡
	 * @param: select_code    0x93  cascaded level 1
	 *                        0x95  cascaded level 2
	 *                        0x97  cascaded level 3
	 * @param: psnr 存放序列号(4byte)的内存单元首地址
	 * @return: status 值为MI_OK:成功
	 * @retval: psnr  得到的序列号放入指定单元
	 * @retval: psak  得到的Select acknolege 回复
	 *
	 *            sak:
	 *            Corresponding to the specification in ISO 14443, this function
	 *            is able to handle extended serial numbers. Therefore more than
	 *            one select_code is possible.
	 *
	 *            Select codes:
	 *
	 *            +----+----+----+----+----+----+----+----+
	 *            | b8 | b7 | b6 | b5 | b4 | b3 | b2 | b1 |
	 *            +-|--+-|--+-|--+-|--+----+----+----+-|--+
	 *              |    |    |    |  |              | |
	 *                                |              |
	 *              1    0    0    1  | 001..std     | 1..bit frame anticoll
	 *                                | 010..double  |
	 *                                | 011..triple  |
	 *
	 *            SAK:
	 *
	 *            +----+----+----+----+----+----+----+----+
	 *            | b8 | b7 | b6 | b5 | b4 | b3 | b2 | b1 |
	 *            +-|--+-|--+-|--+-|--+-|--+-|--+-|--+-|--+
	 *              |    |    |    |    |    |    |    |
	 *                        |              |
	 *                RFU     |      RFU     |      RFU
	 *
	 *                        1              0 .. UID complete, ATS available
	 *                        0              0 .. UID complete, ATS not available
	 *                        X              1 .. UID not complete
	 *
	 ****************************************************************
	 */
	private int pcd_cascaded_select(int select_code, int[] psnr,int[] psak)
	{
	    int i;
	    int status;
	    int snr_check;
	    transceive_buffer  mf_com_data=new transceive_buffer();
	    //transceive_buffer  *pi;
	    //pi = &mf_com_data;
	    snr_check = 0;
	
	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	    set_bit_mask(TxModeReg, BIT7); //使能发送crc
	    set_bit_mask(RxModeReg, BIT7); //使能接收crc

	    pcd_set_tmo(4);

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length = 7;
	    mf_com_data.mf_data[0] = select_code;
	    mf_com_data.mf_data[1] = 0x70;
	    for(i = 0; i < 4; i++)
	    {
	        //snr_check ^= *(psnr + i);
	        //mf_com_data.mf_data[i + 2] = *(psnr + i);
	    	snr_check ^= psnr[i];
	    	mf_com_data.mf_data[i + 2] = psnr[i];
	    }
	    mf_com_data.mf_data[6] = snr_check;

	    status = pcd_com_transceive(mf_com_data);

	    if(status == MI_OK)
	    {
	        if(mf_com_data.mf_length != 0x8)
	        {
	            status = MI_BITCOUNTERR;
	        }
	        else
	        {
	            //*psak = mf_com_data.mf_data[0];
	        	psak[0]=mf_com_data.mf_data[0]; //留意
	        }
	    }
	
	    return status;
	}
	
	/**
	 ****************************************************************
	 * @brief pcd_anticoll()
	 *
	 * 防冲撞函数
	 * @param: select_code    0x93  cascaded level 1
	 *                        0x95  cascaded level 2
	 *                        0x97  cascaded level 3
	 * @param: psnr 存放序列号(4byte)的内存单元首地址
	 * @return: status 值为MI_OK:成功
	 * @retval: psnr  得到的序列号放入指定单元
	 ****************************************************************
	 */
	private int pcd_cascaded_anticoll(int select_code, int coll_position, int[] psnr)
	{
		int status;
		int i;
		int temp;
		int bits;
		int bytes;
		int snr_check;
		int []snr=new int[5];
	    transceive_buffer  mf_com_data=new transceive_buffer();
	    //transceive_buffer  *pi;

	    //pi = &mf_com_data;
	    snr_check = 0;
	    coll_position = 0;
	    //memset(snr, 0, sizeof(snr));
	    Arrays.fill(snr, 0);
	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	    clear_bit_mask(TxModeReg, BIT7); //不使能发送crc
	    clear_bit_mask(RxModeReg, BIT7); //不使能接收crc
	    pcd_set_tmo(4);

	    do
	    {
	        bits = coll_position % 8;
	        if(bits != 0)
	        {
	            bytes = coll_position / 8 + 1;
	            clear_bit_mask(BitFramingReg, TxLastBits | RxAlign);
	            set_bit_mask(BitFramingReg, (TxLastBits & (bits)) | (RxAlign & (bits << 4))); // tx lastbits , rx align
	        }
	        else
	        {
	            bytes = coll_position /8;
	        }
	        mf_com_data.mf_command = PCD_TRANSCEIVE;
	        mf_com_data.mf_data[0] = select_code;
	        mf_com_data.mf_data[1] = 0x20 + ((coll_position / 8) << 4) + (bits & 0x0F);

	        for(i = 0; i < bytes; i++)
	        {
	            mf_com_data.mf_data[i + 2] = snr[i];
	        }
	        mf_com_data.mf_length = bytes + 2;

	        status = pcd_com_transceive(mf_com_data);

	//printf("MI_COLLERR befor status = 0x%2x\r\n",status);
	        temp = snr[coll_position / 8];
	        if(status == MI_COLLERR)
	        {

	            for(i = 0; (5 >= coll_position / 8) && (i < 5 - (coll_position / 8)); i++)
	            {
	                snr[i + (coll_position / 8)] = mf_com_data.mf_data[i + 1];
	            }
	            snr[(coll_position / 8)] |= temp;
	            if(mf_com_data.mf_data[0] >= bits)
	            {
	                coll_position += mf_com_data.mf_data[0] - bits;
	            }
	            else
	            {

	            }


	            //保留冲突位以前的有效位
	            snr[(coll_position / 8)] &= (0xff >> (8 - (coll_position % 8)));
	            //选择冲突bit位为1或是0的卡
	            snr[(coll_position / 8)] |=  1 << (coll_position % 8);//选择bit=1的卡
	            //snr[(coll_position / 8)] &=  ~(1 << (coll_position % 8));//选择bit=0的卡
	            coll_position++; //冲突bit位增1
	        }
	        else if(status == MI_OK)
	        {
	            for(i=0; i < (mf_com_data.mf_length / 8) && (i <= 4); i++)  //增加(i <= 4)防止snr[4-i]溢出
	            {
	                snr[4 - i] = mf_com_data.mf_data[mf_com_data.mf_length / 8 - i - 1];
	            }
	            snr[(coll_position / 8)] |= temp;
	        }

	    }
	    while(status == MI_COLLERR);

	    if(status == MI_OK)
	    {
	        for(i = 0; i < 4; i++)
	        {
	            //*(psnr + i) = snr[i];
	        	psnr[i] = snr[i];
	            snr_check ^= snr[i];
	        }
	        if(snr_check != snr[i])
	        {
	            status = MI_COM_ERR;
	        }
	    }

	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	
	    return status;
	}
	
	private int reqa_wakeup(int[] guid)
	{
	    int[] serial_num=new int[12];
	    int[] serial_num1=new int[12];//从4开始
	    int[] serial_num2=new int[12];//从8开始
	    int status;
		//TRACE_LOG("pcd_default_info 1");
	    pcd_default_info();
		//TRACE_LOG("pcd_default_info 2");
	    //delay_1ms(1);zzy20230629
		//TRACE_LOG("pcd_request 1");
	    status =   pcd_request(WAKEUPA_CMD, g_tag_info.tag_type_bytes); //ZZY 20230712 唤醒方式寻卡0x52的指令  
		//TRACE_LOG("pcd_request 2");
	    //一次防冲突及选卡
	    if(status == MI_OK)
	    {
	        
	        atqa = (g_tag_info.tag_type_bytes[0] << 8) + g_tag_info.tag_type_bytes[1];
	        if(atqa == 0x000c)
	        {
	            return MI_OK;    //TAG1
	        }
	        g_tag_info.uid_length = UID_4;
	        //make_packet(COM_PKT_CMD_CARD_TYPE, g_tag_info.tag_type_bytes, sizeof(g_tag_info.tag_type_bytes));
			//TRACE_LOG("pcd_cascaded_anticoll 1  -  1");
	        status = pcd_cascaded_anticoll(PICC_ANTICOLL1, 0, serial_num);
			//TRACE_LOG("pcd_cascaded_anticoll 1  -  2");
	        //printf("UID << %02x %02x %02x %02x: ", g_tag_info.serial_num[0],g_tag_info.serial_num[1],g_tag_info.serial_num[2],g_tag_info.serial_num[3]);
	        if(status == MI_OK)
	        {
	            //printf("anticoll success:UID: 0x%02x 0x%02x 0x%02x 0x%02x\r\n", g_tag_info.serial_num[0], g_tag_info.serial_num[1], g_tag_info.serial_num[2], g_tag_info.serial_num[3]);
	            //TRACE_LOG("pcd_cascaded_select  -  1");
				status = pcd_cascaded_select(PICC_ANTICOLL1, serial_num, sak);
				//TRACE_LOG("pcd_cascaded_select  -  2");
	            //if(!status)
				if(status==0)
	            {

	                //if(sak & 0x20)
					if((sak[0] & 0x20)!=0)
	                {
	                    g_ask_block_flag = 1;    //cpu卡
	                }
	            }
	        }
	    }
	    //二次防冲突及选卡
	    //if(status == MI_OK && (sak & BIT2))
	    if((status == MI_OK) && ((sak[0] & BIT2)!=0))
	    {
	        //printf("SAK: 0x%x\r\n", sak);
	        g_tag_info.uid_length = UID_7;
			//TRACE_LOG("pcd_cascaded_anticoll 2  -  1");
	        status = pcd_cascaded_anticoll(PICC_ANTICOLL2, 0, serial_num1);
			//TRACE_LOG("pcd_cascaded_anticoll 2  -  2");
	        if(status == MI_OK)
	        {
	            //printf("anticoll success:UID: 0x%02x 0x%02x 0x%02x 0x%02x\r\n", g_tag_info.serial_num[4], g_tag_info.serial_num[5], g_tag_info.serial_num[6], g_tag_info.serial_num[7]);
	            //printf("secend anticoll successful\r\n");
	            status = pcd_cascaded_select(PICC_ANTICOLL2, serial_num1, sak);
	            //if(!status)
	            if(status==0)
	            {
	            	System.arraycopy(serial_num1, 0, serial_num, 4, 4);//java 添加的
	            }
	        }
	    }
	    //回复uid
	    //三次防冲突及选卡
	    //if(status == MI_OK && (sak & BIT2))
	    if((status == MI_OK) && ((sak[0] & BIT2)!=0))
	    {
	        //printf("SAK: 0x%x\r\n",sak);
	        g_tag_info.uid_length = UID_10;
			//TRACE_LOG("pcd_cascaded_anticoll 3  -  1");
	        status = pcd_cascaded_anticoll(PICC_ANTICOLL3, 0, serial_num2);
			//TRACE_LOG("pcd_cascaded_anticoll 3  -  2");
	        if(status == MI_OK)
	        {
	            //printf("3rd anticoll successful");
	            status = pcd_cascaded_select(PICC_ANTICOLL3, serial_num2, sak);
	            //if(!status)
	            if(status==0)
	            {
	                //printf("3rd anticoll successful: SAK: 0x%02x\r\n", sak);
	            	System.arraycopy(serial_num2, 0, serial_num, 8, 4);//java 添加的
	            }
	        }
	    }
	    //回复uid
	    if(status == MI_OK)
	    {
	        int i;
	        guid[10] = g_tag_info.uid_length;
	        if(g_tag_info.uid_length == UID_4)
	        {
	            //memcpy(guid, &serial_num[0], 4);
	        	System.arraycopy(serial_num, 0, guid, 0, 4);
	        }
	        else if(g_tag_info.uid_length == UID_7)
	        {
	            //memcpy(guid, &serial_num[1], 3);
	            //memcpy(guid + 3, &serial_num[4], 4);
	        	System.arraycopy(serial_num, 0, guid, 0, 7);
	        }
	        else if(g_tag_info.uid_length == UID_10)
	        {
	            //memcpy(guid, &serial_num[1], 3);
	            //memcpy(guid + 3, &serial_num[5], 3);
	            //memcpy(guid + 6, &serial_num[8], 4);
	        	System.arraycopy(serial_num, 0, guid, 0, 10);
	        }
	        
	    }
	    if(status == MI_OK)
	    {
	    }
	    else
	    {
	        g_statistics.reqa_fail++;
	        delay_1ms(8);
	    }
	    return status;
	}
	
	private int pcd_polling(int[] guid) {
	    int  status;
	    status = -1;
	    //status = -10002;
	    if(card_A==0)   //card_A
	    {

	        pcd_config('A');
	        pcd_antenna_on();
	        delay_1ms(5);//zzy20230629
	        /*#ifdef AGC_CALIB_EN
	        adc_init();
	        agc_calib();
	        write_reg(0x3f, 0x0);
	        #endif*/
			//TRACE_LOG("reqa_wakeup 1");
	        status = reqa_wakeup(guid);
			//TRACE_LOG("reqa_wakeup 2");
	        if(status  == MI_OK)
	        {
	            MY_CARD_TYPE='A';
				card_A = 1;
	            //printf("reques typeA successful!\r\n");
	            //Memo1->Lines->Add("pcd_polling reques typeA successful!");
	            delay_1ms(2);
	            return status;
	        }
	    }
	    if(card_B==0)
	    {
	        pcd_config('B');
	        pcd_antenna_on();
	        delay_1ms(5);
	        /*#ifdef AGC_CALIB_EN
	        adc_init();
	        agc_calib();
	        write_reg(0x3f, 0x0);
	        #endif*/
			//TRACE_LOG("reqa_wakeup B 1");
	        status = reqb_wakeup(guid);
			//TRACE_LOG("reqa_wakeup B 2");
	        if(status  == MI_OK)
	        {
	            card_B = 1;
				MY_CARD_TYPE='B';
	            //printf("reques typeB successful!\r\n");
	            //Memo1->Lines->Add("pcd_polling reques typeB successful!");
	            delay_1ms(2);
	            return status;
	        }
	    }
	    if(card_F==0) {
	        pcd_config('F');
	        pcd_antenna_on();
	        delay_1ms(5);
			//TRACE_LOG("pcd_request_f F 1");
	        status = pcd_request_f(guid);
			//TRACE_LOG("pcd_request_f F 2");
	        if(status  == MI_OK) {
				MY_CARD_TYPE='F';
	            card_F = 1;
	            //printf("reques typeF successful!\r\n");
	            //Memo1->Lines->Add("pcd_polling reques typeF successful!");
	            delay_1ms(2);
	            return status;
	        }
	    }
	    if(card_V==0) {
	        pcd_config('V');
	        pcd_antenna_on();
	        delay_1ms(5);
	        //status = pcd_request_v(0x26, 0, 0, NULL);
			//TRACE_LOG("pcd_request_v v 1");
	        status = pcd_request_v(guid);
			//TRACE_LOG("pcd_request_v v 2");
	        if(status  == MI_OK) {
				MY_CARD_TYPE='V';
	            card_V = 1;
	            //printf("reques typeV successful!\r\n");
	        //    Memo1->Lines->Add("pcd_polling reques typeV successful!");
	            delay_1ms(2);
	            return status;
	        }
	    }
	    return status;
	}
	
	
	private int get_idcard_num(int[] pid)
	{
	    int  status;
	    transceive_buffer  mf_com_data=new transceive_buffer();
	    //transceive_buffer  *pi;
	    //pi = &mf_com_data;

	    //pcd_set_tmo(5);

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length  =5;
	    mf_com_data.mf_data[0] =0x00; //ISO14443B_ANTICOLLISION;               // APf code
	    mf_com_data.mf_data[1] =0x36;// AFI;                //
	    mf_com_data.mf_data[2] =0x00; //((req_code<<3)&0x08) | (N&0x07);  // PARAM
	    mf_com_data.mf_data[3] =0x00;
	    mf_com_data.mf_data[4] =0x08;

	    status = pcd_com_transceive(mf_com_data);
	    if(status == MI_OK)
	    {
	        //memcpy(pid, &mf_com_data.mf_data[0], 10);
	    	System.arraycopy(mf_com_data.mf_data, 0, pid, 0, 10);
	    }
	    return status;
	}
	
	
	private void pcd_sleep() {
	    write_reg(CommandReg, PCD_CMD_SLEEP);
	}
	private void pcd_sleep_exit() {
	    write_reg(CommandReg, PCD_IDLE);

	    delay_1ms(1);
	}

	private int  pcd_read_block_v(int block, int[] uid, int[] out, int outlen) {
	    int[] command = {0x22, 0x20, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x00};//一次读取4块 16字节 = 4 * 4  block = 4byte
	    //command[sizeof(command) - 1] = block;
	    command[11 - 1] = block;
	    //memcpy(command + 2, uid, 8);
	    System.arraycopy(uid, 0, command, 2, 8);
	    return pcd_transceive(command, 11, out, outlen);
	}

	private int pcd_read_n_block_v(int start, int number, int[] uid, int[] out, int outlen) {
	    int[] command = {0x22, 0x23, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x00, 0x1B};//一次读取4块 16字节 = 4 * 4  block = 4byte
	    command[12 - 2] = start;
	    command[12 - 1] = number;
	    //memcpy(command + 2, uid, 8);
	    System.arraycopy(uid, 0, command, 2, 8);
	    return pcd_transceive(command, 12, out, outlen);
	}

	private int pcd_read_n_block_lock_state_v(int start, int number, int[] uid, int[] out, int outlen) {
	    int[] command = {0x22, 0x2C, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x00, 0x1B};//一次读取4块 16字节 = 4 * 4  block = 4byte
	    command[12 - 2] = start;
	    command[12 - 1] = number;
	    //memcpy(command + 2, uid, 8);
	    System.arraycopy(uid, 0, command, 2, 8);
	    return pcd_transceive(command, 12, out, outlen);
	}

	private int pcd_read_n_lock_state_block_v(int start, int number, int[] uid, int[] out, int outlen) {
	    int[] command = {0x62, 0x23, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x00, 0x1B};//一次读取4块 16字节 = 4 * 4  block = 4byte
	    command[12 - 2] = start;
	    command[12 - 1] = number;
	    //memcpy(command + 2, uid, 8);
	    System.arraycopy(uid, 0, command, 2, 8);
	    return pcd_transceive(command, 12, out, outlen);
	}
	
	private int pcd_read_block_f(int block, int[] uid, int[] out, int outlen) {
	    int[] command = {0x10, 0x06, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x0B, 0x00, 0x01, 0x80, 0x00};//一次读取4块 16字节 = 4 * 4  block = 4byte
	    command[command[0] - 1] = block;
	    //memcpy(command + 2, uid, 8);
	    System.arraycopy(uid, 0, command, 2, 8);
	    return pcd_transceive(command, command[0], out, outlen);
	}

	private int pcd_read_n_block_f(int start, int number, int[] uid, int[] out, int outlen) {
	    int i;
	    int[] command = new int[64];
	    //{0x16, 0x06, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x09, 0x00, 0x04, 0x80, 0x01, 0x80, 0x02, 0x80, 0x03, 0x80, 0x04};//一次读取4块 16字节 = 4 * 4  block = 4byte
	    command[0] = 0x16;
	    command[1] = 0x06; 
	    command[2] = 0x11;
	    command[3] = 0x11;
	    command[4] = 0x11;
	    command[5] = 0x11;
	    command[6] = 0x11;
	    command[7] = 0x11;
	    command[8] = 0x11;
	    command[9] = 0x11;
	    command[10] = 0x01;
	    command[11] = 0x09; 
	    command[12] = 0x00;
	    command[13] = 0x04;
	    command[14] = 0x80;
	    command[15] = 0x01;
	    command[16] = 0x80;
	    command[17] = 0x02;
	    command[18] = 0x80;
	    command[19] = 0x03;
	    command[20] = 0x80;
	    command[21] = 0x04; 
	    
	    command[0] = 14 + number * 2;
	    command[12] = 0x00;
	    command[13] = number;
	    //memcpy(command + 2, uid, 8);
	    System.arraycopy(uid, 0, command, 2, 8);
	    for(i = 0 ; i < number ; i++) {
	        command[14 + 2 * i] = 0x80;
	        command[15 + 2 * i] = start + i;
	    }
	    return pcd_transceive(command, command[0], out, outlen);
	}
	
	private void pcd_delay_sfgi(int sfgi) {
	    //SFGT = (SFGT+dSFGT) = [(256 x 16/fc) x 2^SFGI] + [384/fc x 2^SFGI]
	    //dSFGT =  384 x 2^FWI / fc
		//TRACE_LOG("pcd_delay_sfgi 1");
	    write_reg(TPrescalerReg, (TP_FWT_302us + TP_dFWT) & 0xFF);
		//TRACE_LOG("pcd_delay_sfgi 2");
	    write_reg(TModeReg, BIT7 | (((TP_FWT_302us + TP_dFWT) >> 8) & 0xFF));
	    if(sfgi > 14 || sfgi < 1) {
	        //FDTA,PCD,MIN = 6078 * 1 / fc
	        sfgi = 1;
	    }
		//TRACE_LOG("pcd_delay_sfgi 3");
	    write_reg(TReloadRegL, (1 << sfgi) & 0xFF);
	    //TRACE_LOG("pcd_delay_sfgi 4");
		write_reg(TReloadRegH, ((1 << sfgi) >> 8) & 0xFF);
	    //TRACE_LOG("pcd_delay_sfgi 5");
		clear_bit_mask(TModeReg, BIT7); // clear TAuto
	    //TRACE_LOG("pcd_delay_sfgi 6");
		set_bit_mask(ControlReg, BIT6); // set TStartNow
		//TRACE_LOG("pcd_delay_sfgi 7");
	    while((read_reg(ComIrqReg)  & TimerIRq)==0);//zzy 改成不循环试试
		//if((read_reg(ComIrqReg)  & TimerIRq))
		//delay_1ms(5);
		//read_reg1(ComIrqReg);
		read_reg(ComIrqReg);
		//TRACE_LOG("pcd_delay_sfgi 8");
	    write_reg(ComIrqReg, TimerIRq);  //clear tmr
	}
	
	//////////////////////////////////////////////////////////////////////
	//ISO14443 DESELECT
	//////////////////////////////////////////////////////////////////////
	private int iso14443_4_deselect(int CID)
	{
	    int status;
	    transceive_buffer  mf_com_data=new transceive_buffer();
	    //transceive_buffer  *pi;
	    //pi = &mf_com_data;
	    pcd_set_tmo(4);
	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length  = 2;
	    mf_com_data.mf_data[0] = 0xca;
	    mf_com_data.mf_data[1] = CID & 0x0f;
	    status = pcd_com_transceive(mf_com_data);
	    return status;
	}
	private int ISO14443_4_HalfDuplexExchange(ST_PCDINFO ptPcdInfo,int[] pucSrc,int iTxN,int[] pucDes,int[] piRxN)
	{
		//int	status;
		int	status=0;//改为初始化
		int iTxNum = 0; /*the length of transmitted datas*/
		int iTxLmt = 0; /*the maxium I-block length*/
		int iTxCn = 0; /*the last transmitted I block length*/
		transceive_buffer  mf_com_data=new transceive_buffer();
		int[] aucTxBuf=new int[255];
		int[] aucRxBuf=new int[255];
		int[] pucTxBuf=new int[255];
		int[] pucRxBuf=new int[255];
		int[] tmpBuf=new int[255];
		System.arraycopy(mf_com_data.mf_data, 0, aucTxBuf, 0, MAX_TRX_BUF_SIZE);
		System.arraycopy(mf_com_data.mf_data, 0, aucRxBuf, 0, MAX_TRX_BUF_SIZE);
		System.arraycopy(mf_com_data.mf_data, 0, pucTxBuf, 0, MAX_TRX_BUF_SIZE);
		System.arraycopy(mf_com_data.mf_data, 0, pucRxBuf, 0, MAX_TRX_BUF_SIZE);
		int aucTxBufLen=0;
		int aucRxBufLen=0;
		int pucTxBufLen=0;
		int pucRxBufLen=0;
		int pucDesLen=0;
		int pucSrcLen=0;
		
		int	iSRetry   = 0; /*continuous s block count*/
	    int	iIRetry   = 0; /*i block re-transmission count*/
	    int	iErrRetry = 0; /*error cause re-transmission count*/
	    int uiWtx     = 1;/*the extra wait time in communication*/
	    int s_swt_limit_count; 
	    int eExStep;//C枚举改为java的整形变量
	    eExStep   = ORG_IBLOCK;
	    piRxN[0]    = 0;
	    uiWtx     = 1;
	    iSRetry   = 0;
	    iIRetry   = 0;
	    iErrRetry = 0;
	    
	    if(ptPcdInfo.uiFsc < 16)
	    {
	        ptPcdInfo.uiFsc = 32;
	    }
	    iTxLmt = ptPcdInfo.uiFsc - 3;/*PCB and CRC16*/
	    if(ptPcdInfo.ucNadEn!=0)
	    {
	        iTxLmt--;
	    }
	    if(ptPcdInfo.ucCidEn!=0)
	    {
	        iTxLmt--;
	    }
	    /*the number of transmitted data bytes*/
	    iTxNum = iTxN;
	    s_swt_limit_count = 0;
	    
	    do
	    {
	    	switch(eExStep)
	        {
	        case ORG_IBLOCK:/*I block*/
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 1");
                /*point to block buffer header*/
                pucTxBuf  = aucTxBuf;
                //System.arraycopy(aucTxBuf, 0, pucTxBuf, 0, MAX_TRX_BUF_SIZE);//////zzy
                /*ready for I block*/
                if(iTxNum > iTxLmt)   /*chaninning block*/
                {
                    //TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 1 1-1");
					aucTxBuf[0] = 0x12 | (ptPcdInfo.ucPcdPcb & ISO14443_CL_PROTOCOL_ISN);  /*serial number and chaninning flag*/
                    if(ptPcdInfo.ucCidEn!=0)
                    {
                        aucTxBuf[0] |= ISO14443_CL_PROTOCOL_CID;    /*support CID*/
                    }
                    if(ptPcdInfo.ucNadEn!=0)
                    {
                        aucTxBuf[0] |= ISO14443_CL_PROTOCOL_NAD;    /*support NAD*/
                    }
                    //pucTxBuf++;
                    pucTxBufLen++;
                    if(ptPcdInfo.ucCidEn!=0)
                    {
                    	pucTxBufLen++;
                    	pucTxBuf[pucTxBufLen] = ptPcdInfo.ucCid & 0x0F;
                    }
                    if(ptPcdInfo.ucNadEn!=0)
                    {
                        //*pucTxBuf++ = ptPcdInfo.ucNad & 0x0F;
                    	pucTxBufLen++;
                    	pucTxBuf[pucTxBufLen] = ptPcdInfo.ucCid & 0x0F;
                    }
                    iTxCn    = iTxLmt;
                    iTxNum  -= iTxLmt;
					//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 1 1-2");
                }
                else     /*non-chaninning block*/
                {
                    //TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 1 1 2-1");
					aucTxBuf[0] = 0x02 | (ptPcdInfo.ucPcdPcb & ISO14443_CL_PROTOCOL_ISN);  /*serial number and non-chaninning flag*/
                    if(ptPcdInfo.ucCidEn!=0)
                    {
                        aucTxBuf[0] |= ISO14443_CL_PROTOCOL_CID;    /*support CID*/
                    }
                    if(ptPcdInfo.ucNadEn!=0)
                    {
                        aucTxBuf[0] |= ISO14443_CL_PROTOCOL_NAD;    /*support NAD*/
                    }
                    //pucTxBuf++;
                    pucTxBufLen++;
                    if(ptPcdInfo.ucCidEn!=0)
                    {
                    	pucTxBufLen++;
                    	pucTxBuf[pucTxBufLen] = (ptPcdInfo.ucCid & 0x0F);
                    }
                    if(ptPcdInfo.ucNadEn!=0)
                    {
                    	pucTxBufLen++;
                    	pucTxBuf[pucTxBufLen] = (ptPcdInfo.ucNad & 0x0F);
                    }
                    iTxCn   = iTxNum;
                    iTxNum  = 0;
					//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 1 1 2-2");
                }
                //memcpy(pucTxBuf, pucSrc, iTxCn);  /*the INF of I block*/
                System.arraycopy(pucSrc, pucSrcLen, pucTxBuf, pucTxBufLen, iTxCn);
                pucTxBufLen+=iTxCn;
                pucSrcLen+=iTxCn;
                //pucTxBuf += iTxCn;
                //pucSrc   += iTxCn;/*the pointer of datas*/ //不知道怎么翻译 待定
                ptPcdInfo.ucPcdPcb = aucTxBuf[0]; //by xlwang
                //   ptPcdInfo->ucPcdPcb = 0x0b;
                eExStep = ORG_TRARCV;
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 1 1 2-2 -1");
                break;
	        case ORG_ACKBLOCK:/*R(ACK) response*/
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 2");
                pucTxBuf  = aucTxBuf;  //注意
	        	//System.arraycopy(aucTxBuf, 0, pucTxBuf, 0, MAX_TRX_BUF_SIZE);//////zzy
                //aucTxBuf[0] = 0xA2 | (ptPcdInfo.ucPcdPcb & 1);
                aucTxBuf[0] = 0xA2 | (ptPcdInfo.ucPcdPcb & 1);
                if(ptPcdInfo.ucCidEn!=0)
                {
                    aucTxBuf[0] |= ISO14443_CL_PROTOCOL_CID;    /*support CID*/
                }
                //pucTxBuf++;
                pucTxBufLen++;
                if(ptPcdInfo.ucCidEn!=0)
                {
                	pucTxBufLen++;
                	pucTxBuf[pucTxBufLen] = (ptPcdInfo.ucCid & 0x0F);
                }
                eExStep = ORG_TRARCV;
                break;
	        case ORG_NACKBLOCK:/*R(NACK) reponse*/
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 3");

                pucTxBuf  = aucTxBuf;
	        	//System.arraycopy(aucTxBuf, 0, pucTxBuf, 0, MAX_TRX_BUF_SIZE);//////zzy
                aucTxBuf[0] = 0xB2 | (ptPcdInfo.ucPcdPcb & 1);
                if(ptPcdInfo.ucCidEn!=0)
                {
                    aucTxBuf[0] |= ISO14443_CL_PROTOCOL_CID;    /*support CID*/
                }
                //pucTxBuf++;
                pucTxBufLen++;
                if(ptPcdInfo.ucCidEn!=0)
                {
                	pucTxBufLen++;
                	pucTxBuf[pucTxBufLen] = (ptPcdInfo.ucCid & 0x0F);
                }
                eExStep = ORG_TRARCV;
                break;
	        case ORG_SBLOCK:/*S(uiWtx) block response*/
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 4");

                pucTxBuf  = aucTxBuf;
                pucTxBufLen++;
                pucTxBuf[pucTxBufLen] = 0xF2;
                pucTxBuf[pucTxBufLen] = uiWtx & 0x3F;
                eExStep = ORG_TRARCV;
                break;
            case ORG_TRARCV:
                iErrRetry++;
                if(uiWtx > 59)
                {
                    uiWtx = 1;/* EMV CL TB417.6 */
                    eExStep = NON_EVENT;
                    status = ISO14443_4_ERR_PROTOCOL;
                    break;
                }
                //ptPcdInfo->uiFwi = uiWtx * ptPcdInfo->uiFwi;
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 5-1");
                gtPcdModuleInfo.ucWTXM = uiWtx;
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 5-1-1");
                pcd_delay_sfgi(ptPcdInfo.uiSfgi);						//此处有错误20230710
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 5-1-2");
                pcd_set_tmo(ptPcdInfo.uiFwi);
                //mf_com_data.mf_length = pucTxBuf[pucTxBufLen] - aucTxBuf[aucTxBufLen];
                mf_com_data.mf_length=pucTxBufLen-aucTxBufLen;//这里源码为地址相减
                //memcpy(pi->mf_data, aucTxBuf, pi->mf_length);
                if(mf_com_data.mf_length<0)
                	mf_com_data.mf_length=0;
                System.arraycopy(aucTxBuf, 0, mf_com_data.mf_data, 0, mf_com_data.mf_length);
                mf_com_data.mf_command = PCD_TRANSCEIVE;
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 5-1-4");
                status = pcd_com_transceive(mf_com_data);
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 5-2");
                if((status == MI_OK) || (status == 0x9a))
                {
                    ptPcdInfo.uiPcdTxRNum = mf_com_data.mf_length / 8 + (mf_com_data.mf_length % 8);
                    //memcpy(aucRxBuf, pi->mf_data, ptPcdInfo->uiPcdTxRNum);
                    System.arraycopy(mf_com_data.mf_data, 0, aucRxBuf, 0, ptPcdInfo.uiPcdTxRNum);
                    ptPcdInfo.uiPcdTxRLastBits = mf_com_data.mf_length % 8;
                    status = MI_OK;
                }
                if(status != MI_OK)
                {
                    //TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 5-4");
					if(iErrRetry > ISO14443_PROTOCOL_RETRANSMISSION_LIMITED ||
                       iSRetry > ISO14443_PROTOCOL_RETRANSMISSION_LIMITED)
                    {
                        eExStep = RCV_INVBLOCK;/*consider as protocol error*/
                    }
                    else
                    {
                        if((ptPcdInfo.ucPiccPcb & ISO14443_CL_PROTOCOL_CHAINED)!=0)   /*<$10.3.5.6> & <$10.3.5.8>*/
                        {
                            eExStep = ORG_ACKBLOCK;
                        }
                        else     /*<$10.3.5.3> & <$10.3.5.5>*/
                        {
                            eExStep = ORG_NACKBLOCK;
                        }
                    }
                }
                else
                {
                    //TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 5-5");
					iErrRetry = 0;
                    //if( !iRev )/*transmission without error*/
                    if(status == MI_OK)
                    {
                        /*<$10.3.5.4 & $10.3.5.7>*/
                        pucRxBuf = aucRxBuf;
                        /*class the block type to process*/
                        if(0x02 == (aucRxBuf[0] & 0xE2))
                        {
                            if((0 == (aucRxBuf[0] & 0x2)) || (ptPcdInfo.uiPcdTxRNum > 254) ||
                               (((ISO14443_CL_PROTOCOL_CID | ISO14443_CL_PROTOCOL_NAD) & aucRxBuf[0])!=0)
                              )
                            {
                                /*EMV CL requirements <$10.3.5.4 & $10.3.5.7>,ptPcdInfo's ifsd=256*/
                                // __printf( "invblock1\r\n" );
                                eExStep = RCV_INVBLOCK;
                            }
                            else
                            {
                                iSRetry = 0;
                                eExStep  = RCV_IBLOCK;
                            }
                        }
                        else if(0xA0 == (aucRxBuf[0] & 0xE0))
                        {
                            /*EMV CL requirements <$10.3.5.4 & $10.3.5.7>*/
                            if((((ISO14443_CL_PROTOCOL_CID | ISO14443_CL_PROTOCOL_NAD) & aucRxBuf[0])!=0) ||
                               (ptPcdInfo.uiPcdTxRNum > 2)
                              )   /*EMV CL requirements <$10.3.5.4 & $10.3.5.7>*/
                            {
                                //  __printf( "invblock2\r\n" );
                                eExStep = RCV_INVBLOCK;
                            }
                            else
                            {
                                iSRetry = 0;
                                eExStep  = RCV_RBLOCK;
                            }
                        }
                        else if(0xC0 == (aucRxBuf[0] & 0xC0))
                        {
                            /*EMV CL requirements <$10.3.5.4 & $10.3.5.7>*/
                            if((((ISO14443_CL_PROTOCOL_CID | ISO14443_CL_PROTOCOL_NAD) & aucRxBuf[0])!=0) ||
                               (ptPcdInfo.uiPcdTxRNum > 2)
                              )   /*EMV CL requirements <$10.3.5.4 & $10.3.5.7>*/
                            {
                                //   __printf( "invblock3\r\n" );
                                eExStep = RCV_INVBLOCK;
                            }
                            else
                            {
                                iSRetry++;
                                eExStep = RCV_SBLOCK;
                            }
                        }
                        else
                        {
                            //__printf( "invblock4\r\n" );
                            eExStep = RCV_INVBLOCK;/*protocol error*/
                        }
                    }
                    else
                    {
                        /*PICC enter auto-recovery step*/
                    }
                }
                //ptPcdInfo->uiFwi /= uiWtx;
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 5-6");
                uiWtx       = 1;/*restore the FWT*/
                gtPcdModuleInfo.ucWTXM = uiWtx;
                break;
            case RCV_IBLOCK:
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 6");
                if(iTxNum!=0)   /*command datas has be not completed*/
                {
                    //__printf( "invblock5\r\n" );
                    eExStep = RCV_INVBLOCK;
                }
                else
                {
                	pucRxBufLen++;
                	//pucRxBuf++;/*jump over PCB*/
                    /*the serail number equal to ptPcdInfo's current serial number*/
                    if((ptPcdInfo.ucPcdPcb & ISO14443_CL_PROTOCOL_ISN) ==
                       (aucRxBuf[0] & ISO14443_CL_PROTOCOL_ISN))
                    {
                        ptPcdInfo.ucPiccPcb = aucRxBuf[0];/*the last receipt of a block*/
                        if((aucRxBuf[0] & ISO14443_CL_PROTOCOL_CHAINED)!=0)   /*chainning block*/
                        {
                            if((aucRxBuf[0] & ISO14443_CL_PROTOCOL_CID)!=0)
                            {
                                //pucRxBuf++;    /*jump over CID*/
                            	pucRxBufLen++;
                            }
                            if((aucRxBuf[0] & ISO14443_CL_PROTOCOL_NAD)!=0)
                            {
                                //pucRxBuf++;    /*jump over NAD*/
                            	pucRxBufLen++;
                            }
                            /*R(ACK)*/
                            eExStep = ORG_ACKBLOCK;
                        }
                        else     /*non-chaninning block*/
                        {
                            if((aucRxBuf[0] & ISO14443_CL_PROTOCOL_CID)!=0)
                            {
                                //pucRxBuf++;    /*jump over CID*/
                            	pucRxBufLen++;
                            }
                            if((aucRxBuf[0] & ISO14443_CL_PROTOCOL_NAD)!=0)
                            {
                                //pucRxBuf++;    /*jump over NAD*/
                            	pucRxBufLen++;
                            }
                            eExStep = NON_EVENT;
                        }
                        /*process received datas informations*/
                        //if(ptPcdInfo->uiPcdTxRNum >= (pucRxBuf - aucRxBuf))
                        if(ptPcdInfo.uiPcdTxRNum >= (pucRxBufLen - aucRxBufLen))
                        {
                            //memcpy(pucDes, pucRxBuf, (ptPcdInfo->uiPcdTxRNum - (pucRxBuf - aucRxBuf)));
                        	//Arrays.fill(tmpBuf,0);
                        	//System.arraycopy(pucRxBuf, pucRxBufLen, tmpBuf, 0, (ptPcdInfo.uiPcdTxRNum - (pucRxBuf[pucRxBufLen] - aucRxBuf[aucRxBufLen])));
                        	//System.arraycopy(pucRxBuf, pucRxBufLen, tmpBuf, 0, (ptPcdInfo.uiPcdTxRNum - (pucRxBufLen - aucRxBufLen)));///地址 注意
                        	System.arraycopy(pucRxBuf, pucRxBufLen, pucDes, pucDesLen, (ptPcdInfo.uiPcdTxRNum - (pucRxBufLen - aucRxBufLen)));///地址 注意
                        	pucDesLen=ptPcdInfo.uiPcdTxRNum - (pucRxBufLen - aucRxBufLen);
                        	//pucDes  += ptPcdInfo->uiPcdTxRNum - (pucRxBuf - aucRxBuf);
                        	//System.arraycopy(tmpBuf, ptPcdInfo.uiPcdTxRNum - (pucRxBuf[pucRxBufLen] - aucRxBuf[aucRxBufLen]), 
                        	//		pucDes, 0, (ptPcdInfo.uiPcdTxRNum - (pucRxBuf[pucRxBufLen] - aucRxBuf[aucRxBufLen])));
                            //*piRxN  += ptPcdInfo->uiPcdTxRNum - (pucRxBuf - aucRxBuf);
                        	/*System.arraycopy(tmpBuf, ptPcdInfo.uiPcdTxRNum - (pucRxBufLen - aucRxBufLen),
                        			pucDes, 0, (ptPcdInfo.uiPcdTxRNum - (pucRxBufLen - aucRxBufLen)));*/
                        	//piRxN[0]  += ptPcdInfo.uiPcdTxRNum - (pucRxBuf[pucRxBufLen] - aucRxBuf[aucRxBufLen]);
                        	piRxN[0]  += ptPcdInfo.uiPcdTxRNum - (pucRxBufLen - aucRxBufLen);
                        }
                        /*toggle serial number <$10.3.3.3>*/
                        ptPcdInfo.ucPcdPcb ^= ISO14443_CL_PROTOCOL_ISN;
                    }
                    else     /*the serail number don't equal to ptPcdInfo's current serial number*/
                    {
                        //__printf( "invblock6\r\n" );
                        eExStep = RCV_INVBLOCK;
                    }
                }
                break;
            case RCV_RBLOCK:
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 7");
                if((aucRxBuf[0] & 0x10)!=0)   /*R(NAK) <$10.3.4.6>*/
                {
                    //__printf( "invblock7\r\n" );
                    eExStep = RCV_INVBLOCK;
                }
                else
                {
                    /*the serail number equal to ptPcdInfo's current serial number*/
                    if((ptPcdInfo.ucPcdPcb & ISO14443_CL_PROTOCOL_ISN) ==
                       (aucRxBuf[0] & ISO14443_CL_PROTOCOL_ISN))
                    {
                        /*receiving R(ACK), send the next i block <$10.3.4.5>*/
                        if((ptPcdInfo.ucPcdPcb & ISO14443_CL_PROTOCOL_CHAINED)!=0)
                        {
                            /*toggle serial number <$10.3.3.3>*/
                            ptPcdInfo.ucPcdPcb ^= ISO14443_CL_PROTOCOL_ISN;
                            iIRetry = 0;
                            eExStep = ORG_IBLOCK;
                        }
                        else
                        {
                            //__printf( "invblock8\r\n" );
                            eExStep = RCV_INVBLOCK;
                        }
                    }
                    else     /*re-transmitted last i block <$10.3.4.4>*/
                    {
                        iIRetry++;
                        if(iIRetry > ISO14443_PROTOCOL_RETRANSMISSION_LIMITED)
                        {
                            //__printf( "invblock9\r\n" );
                            eExStep = RCV_INVBLOCK;/*protocol error*/
                        }
                        else
                        {
                            iTxNum += iTxCn;
                            //pucSrc -= iTxCn;
                            //pucSrc[0] -= iTxCn;
                            pucSrcLen-=iTxCn;
                            eExStep = ORG_IBLOCK;
                        }
                    }
                }
                break;
            case RCV_SBLOCK:/*S(uiWtx) request*/
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 8");
                if(0xF2 != (aucRxBuf[0] & 0xF7))    /*receiving the S(uiWtx)*/
                {
                    //__printf( "invblock10\r\n" );
                    eExStep = RCV_INVBLOCK;
                }
                else
                {
                    //pucRxBuf = aucRxBuf + 1;
                	System.arraycopy(aucRxBuf, 1, pucRxBuf, 0, 255-1);
                    if((aucRxBuf[0] & ISO14443_CL_PROTOCOL_CID)!=0)
                    {
                        //pucRxBuf++;    /*jump over CID*/
                    	pucRxBufLen++;
                    }
                    if(0 == (pucRxBuf[pucRxBufLen] & 0x3F))
                    {
                        //__printf( "invblock11\r\n" );
                        eExStep = RCV_INVBLOCK;
                    }
                    else
                    {
                        s_swt_limit_count++; /*add by nt 20121224 for zhoujie paypass 3.0*/
                        uiWtx = (pucRxBuf[pucRxBufLen] & 0x3F);  /*response S(uiWtx) using the same value with PICC*/
                        eExStep = ORG_SBLOCK;
                    }
                }
                break;
            case RCV_INVBLOCK:/*protocol error*/
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 9");
                //if( ISO14443_HW_ERR_COMM_TIMEOUT != iRev && iRev != ISO14443_PCD_ERR_USER_CANCEL)/* modify by nt 2013/03/11 */
                //  iRev = ISO14443_4_ERR_PROTOCOL;
                if(MI_NOTAGERR != status)
                {
                    //status = ISO14443_4_ERR_PROTOCOL;
                    status=-10005;//zzy
                }
                eExStep = NON_EVENT;/*ready to return*/
                //PcdCarrierOff( ptPcdInfo );/*Liubo 2011-11-30*/
                //PcdCarrierOn( ptPcdInfo );
                break;
            default:
				//TRACE_LOG("ISO14443_4_HalfDuplexExchange 1 case 10");
                break;
	        }
	    }while(NON_EVENT != eExStep);
		
	    return status;
	}
	private int Rfid_exchange(int[] senddata, int tx_len, int[] recdata, int[] rx_len) {
	    return(ISO14443_4_HalfDuplexExchange(gtPcdModuleInfo, senddata, tx_len, recdata, rx_len));//这地方倒数第二个参数重点留意。上面有地址偏移
	    //return M_ExchangeByteStream( senddata, tx_len,recdata, rx_len );
	}
	
	private void Iblock_getrand(int rate) {
		int testcnd;
		int okcnt;
		int cur_rate;
		int len;
		//int uiRecLen;
		int[] uiRecLen=new int[2];
	    okcnt = 0;
	    testcnd = 10;
	    int[] tmpBuf=new int[32];
	    if(g_ask_block_flag == 0) {
	        return;
	    }
	    while(testcnd>0) {
	        testcnd--;
	        len = 5;
	        sendBuffer[0] = 0x00;
	        sendBuffer[1] = 0x84;
	        sendBuffer[2] = 0x00;
	        sendBuffer[3] = 0x00;
	        sendBuffer[4] = 0x04;
	        if(Rfid_exchange(sendBuffer, len, recvBuffer, uiRecLen) == MI_OK) {

				//tmpBuf[0]=g_ret;
				//memset(tmpBuf,0x00,sizeof(tmpBuf));
	        	Arrays.fill(tmpBuf,0);
				//memcpy(tmpBuf,recvBuffer,uiRecLen);
	        	System.arraycopy(recvBuffer, 0, tmpBuf, 0, uiRecLen[0]);
				//TRACE_DATA("Iblock_getrand case1_1 pcd_active value  = ", tmpBuf, uiRecLen);
	            okcnt ++;
	            //printf("block_exchange successful \r\n");
	            continue;
	        } else {
	            //printf("block_exchange fail %d times \r\n", testcnd);//不打印看看会不会报错
	            break;
	        }
	    }
	    if(rate == 1) {
	        cur_rate = 106;
	    } else if(rate == 2) {
	        cur_rate = 212;
	    } else if(rate == 3) {
	        cur_rate = 424;
	    } else if(rate == 4) {
	        cur_rate = 848;
	    }
	    iso14443_4_deselect(0);
	    delay_1ms(10);
	}
	
	private void NFC_V_Tag5_Test(int[] uid) {
	    int[] pbuf=new int[256];
	    //int[] len=new int[2];//长度要返回的都改成数组
	    int len=0;
	    pcd_read_block_v(0, uid, pbuf, len);
	    pcd_read_n_block_lock_state_v(0, 27, uid, pbuf, len); //一共28块
	    pcd_read_n_block_v(0, 27, uid, pbuf, len);       //一共28块
	    pcd_read_n_lock_state_block_v(0, 27, uid, pbuf, len); //一共28块
	    //printf("## tag5 test end##\r\n\r\n");
	}
	private void NFC_F_Tag3_Test(int[] uid) {
	    int[] pbuf=new int[256];
	    int len=0;
	    pcd_read_block_f(0, uid, pbuf, len);
	    pcd_read_n_block_f(1, 4, uid, pbuf, len);
	}
	
	
	private int nfc_demo()
	{
	        int[] tmpBuf=new int[12];
			int u_temp = 0, card_det = 0;
	        int rate = 0;
	        demo_step = 0;
	        while(true) {
	        switch(demo_step) {
	            case 0://询卡
	                //ShowMessage("进了0");
	                card_A = 0;
	                card_B = 0;
	                card_F = 0;
	                card_V = 0;
	                g_ask_block_flag = 0;
	                pcd_antenna_off();
	                delay_1ms(3);

					//TRACE_LOG("pcd_set_rate 1");
	                pcd_set_rate(1);
					//TRACE_LOG("pcd_set_rate 2");

	                //////////////////////////////////////////////////////////
					//TRACE_LOG("pcd_polling 1");
	                g_ret = pcd_polling(g_tag_info.serial_num);
					//TRACE_LOG("pcd_polling 2");
					tmpBuf[0]=g_ret;
					//TRACE_DATA("pcd_polling value  = ", tmpBuf, 1);
	                if(g_ret == 0) {
	                    demo_step = 1;
	                    //    rate = rate%4;
	                    //    rate++;
	                    rate = 1;
	                    
	                    //return 0; //zzy  20230704 将下面的部分代码放开，注释掉此句直接返回代码
	                    break;
	                } else {
	                    demo_step = 0;
	                    card_det = 0;
	                    return card_det;
	                }
	            case 1:
	                //ShowMessage("进了1");
					//TRACE_LOG("nfc_demo case1");
					
	                if(card_A!=0) {
	                    if((sak[0] == 0x08) || (sak[0] == 0x18)) { //MIFARE
	                        //NFC_A_Mifare_Test(g_tag_info.serial_num);
	                        demo_step = 0;
	                        break;
	                    }
	                    if(atqa == 0x000c) {
	                        //NFC_A_Tag1_Test(g_tag_info.serial_num);
	                        demo_step = 0;
	                        break;
	                    }
	                    if(atqa == 0x4400) {
	                        //NFC_A_Tag2_Test(g_tag_info.serial_num);
	                        demo_step = 0;
	                        break;
	                    }
	                }
					//TRACE_LOG("nfc_demo case1_1");
	                g_ret = pcd_active(rate);
					tmpBuf[0]=g_ret;
					//TRACE_DATA("nfc_demo case1_1 pcd_active value  = ", tmpBuf, 1);
	                if(g_ret == 0) {
	                    demo_step = 2;
	                } else {
	                    demo_step = 0;
	                }

	                break;
	            case 2:
	               // ShowMessage("进了2");
					//TRACE_LOG("nfc_demo case2");
	                if(card_A!=0) {
						//TRACE_LOG("nfc_demo case2-A");
	                    //Iblock_getrand(rate);//注释掉原来的随机数测试
	                }
	                if(card_B!=0) {
						//TRACE_LOG("nfc_demo case2-B");
	                    g_ask_block_flag = 1;
	                    g_ret = get_idcard_num(g_tag_info.serial_num);
	                    if(g_ret == 0) {
							//TRACE_LOG("nfc_demo case2-B-1");
	                        g_tag_info.uid_length = 10;
	                        //ShowMessage((char*)g_tag_info.serial_num);
	                        //PRINT_BUF("ID_NUM: ", g_tag_info.serial_num, g_tag_info.uid_length);
	                        //printf("## test end##\r\n\r\n");
	                    } else {
							//TRACE_LOG("nfc_demo case2-B-2");
	                        //Iblock_getrand(rate);
	                    }
	                }
	                if(card_F!=0) {
						//TRACE_LOG("nfc_demo case2-F");
	                    //NFC_F_Tag3_Test(g_tag_info.serial_num);
	                }
	                if(card_V!=0) {
						//TRACE_LOG("nfc_demo case2-V");
	                    //NFC_V_Tag5_Test(g_tag_info.serial_num);
	                }
	                demo_step = 0;
	                //  return 1;  
	                break;
	            default:
	                //ShowMessage("进了3");
					//TRACE_LOG("nfc_demo case3");
	                pcd_set_rate(1);
	                pcd_antenna_off();
	                delay_1ms(3);
	                pcd_antenna_on();
	                demo_step = 0;
	                break;
	        }
	        if(demo_step == 0) {
	            //delay_1ms(1000);
	            card_det = 1;
	            break;
	        }
	        if(1 == u_temp) {
	            card_det = 1;
	            break;
	        }
	    }
	    return card_det;
	}
	
	
	private int pcd_active(int rate) {
	    int status = 1;
	    int[] ats=new int[30];
	    transceive_buffer  mf_com_data=new transceive_buffer();
	    if(card_A!=0) { //card_A
	        status = pcd_rats_a(0, ats);
	        if(status == MI_OK) {
	            pcd_set_tmo(4);
	            // status =
	            status = pcd_pps_rate(mf_com_data, ats, 0, rate) ;
	            if(status == MI_OK) {
	            }
	        }
	    } else if(card_B!=0) {
	        status =  pcd_active_b(rate);
	        if(status == MI_OK) {
	            pcd_set_rate(rate);
	        }
	    } else if(card_F!=0) {
	        status = MI_OK;
	    } else if(card_V!=0) {
	        status = MI_OK;
	    }
	    return status;
	}
	
	private int pcd_rats_a(int CID, int[] ats)
	{
	    int status = MI_OK;



	    int[] ATS=new int[255];
	    int ta,tb,tc;
	    transceive_buffer  mf_com_data=new transceive_buffer();
	    ta = tb = tc  = 0;

	    /*initialiszed the PCB*/
	    gtPcdModuleInfo.ucPcdPcb  = 0x02;
	    gtPcdModuleInfo.ucPiccPcb = 0x03;
	    gtPcdModuleInfo.ucCid = CID;
	    gtPcdModuleInfo.uiFwi = 4*3;

	    pcd_delay_sfgi(gtPcdModuleInfo.uiSfgi);

	    pcd_set_tmo(4); //according to 14443-4 4.8ms

	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	    set_bit_mask(TxModeReg, BIT7); //使能发送crc
	    set_bit_mask(RxModeReg, BIT7); //使能接收crc

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length  = 2;
	    mf_com_data.mf_data[0] = PICC_RATS; //Start byte
	    mf_com_data.mf_data[1] = (FSDI << 4) + CID; //Parameter

	    status = pcd_com_transceive(mf_com_data);

	    if((status == MI_OK && (mf_com_data.mf_length % 8) != 0)
	       || (status == MI_COLLERR)
	       || (status == MI_INTEGRITY_ERR && mf_com_data.mf_length / 8 < 4)
	       || (status == MI_INTEGRITY_ERR && mf_com_data.mf_length / 8 >= 4 && (mf_com_data.mf_length % 8) != 0)
	      )
	    {
	        //printf("2\n");
	        status = MI_NOTAGERR;
	    }

	    if(MI_OK == status)
	    {
	        //ATS
	        //ATS = pi->mf_data;
	        System.arraycopy(mf_com_data.mf_data, 0, ATS, 0, mf_com_data.mf_length); 
	        if(ATS[0] > 254)   //  ISO14443 form8-1
	        {
	            return -1;
	        	//return -10004;
	        }
	        //memcpy(ats, ATS, ATS[0]);
	        System.arraycopy(ATS, 0, ats, 0, ATS[0]); 
	        //printf("3\n");
	        if(mf_com_data.mf_length / 8 < 1 || ATS[0] != mf_com_data.mf_length / 8)
	        {
	            //at least 1bytes, and TL = length

	            //printf("4\n");
	            return PROTOCOL_ERR;
	        }

	        if((ATS[0] < (2 + ((ATS[1]&BIT4)>>4) + ((ATS[1]&BIT5)>>5) + ((ATS[1]&BIT6)>>6))))
	        {
	            //ERR:TL length
	            //return PROTOCOL_ERR;
	        }
	        else
	        {
	            if((ATS[1]&BIT7)==0)
	            {
	                //T0.7 = 0
	                if((ATS[1]&BIT4)!=0)
	                {
	                    ta = 1;
	                }
	                if((ATS[1]&BIT5)!=0)
	                {
	                    tb = 1;
	                    gtPcdModuleInfo.uiFwi = (ATS[2+ta] & 0xF0) >> 4;
	                    gtPcdModuleInfo.uiSfgi = ATS[2+ta] & 0x0f;

	                }
	                if((ATS[1]&BIT6)!=0)
	                {
	                    tc = 1;
	                    gtPcdModuleInfo.ucCidEn = 0; //?
	                    gtPcdModuleInfo.ucNadEn = 0; //?
	                    // gtPcdModuleInfo.ucNadEn = ATS[2]&BIT0; //?
	                    //gtPcdModuleInfo.ucCidEn = ATS[2]&BIT1; //?

	                }
	                gtPcdModuleInfo.uiFsc = gausMaxFrameSizeTable[ATS[1] & 0x0F];

	                //FSC
	                if(mf_com_data.mf_length/8 < (2 + ta + tb + tc))
	                {
	                    //长度错误
	                    return PROTOCOL_ERR;
	                }

	                pcd_set_tmo(gtPcdModuleInfo.uiFwi);

	            }
	            else
	            {
	                //status = PROTOCOL_ERR;
	            }

	        }

	    }


	    return status;
	}
	
	private int pcd_active_b(int rate) {
	    int  status;
	    int[] tmpBuf=new int[16];//为了缓存ATQB
	    S_ATTRIB attrib_val=new S_ATTRIB();
	    switch(rate) {
	        case 0x1:
	            //dsi_dri =0x0;
	            attrib_val.param[1] = 0x0;
	            break;
	        case 0x2:
	            //   dsi_dri=0x5;
	            attrib_val.param[1] = 0x5;
	            break;
	        case 0x3:
	            attrib_val.param[1] = 0xa;
	            break;
	        case 0x04:
	            attrib_val.param[1] = 0xf;
	            break;
	        default:
	            attrib_val.param[1] = 0x0;
	            break;
	    }
	    System.arraycopy(ATQB,1,tmpBuf, 0,16-1);
	    status = pcd_attri_b(tmpBuf, attrib_val.param[1], ATQB[10] & 0x0f, PICC_CID, ATQB);
	    if(status == MI_OK) {
	    	//System.arraycopy(tmpBuf, 0, ATQB, 1, 16-1);
	        ATQB[0] = 0x50;//恢复默认值
	        pcd_set_rate(rate);
	    } else {
	        if(rate == 1) {
	            //printf("106K,Unsupport\n");
	        } else if(rate == 2) {
	           // printf("212K,Unsupport\n");
	        } else if(rate == 3) {
	           // printf("424K,Unsupport\n");
	        } else if(rate == 4) {
	           // printf("848K,Unsupport\n");
	        }
	        //g_statistics.reqb_fail++;
	       
	    }
	    return status;
	}
	
	
	private int pcd_pps_rate(transceive_buffer pi, int[] ATS, int CID, int rate)
	{
	    int DRI, DSI;
	    int status = MI_OK;

	    DRI = 0;
	    DSI = 0;

	    if((ATS[0]>1) && ((ATS[1]& BIT4)!=0))
	    {
	        //TA(1) transmited
	        if(rate == 1)
	        {

	        }
	        else if(rate == 2)
	        {
	            if(((ATS[2]&BIT0)!=0) && ((ATS[2]&BIT4)!=0))
	            {

	                // DS=2,DR=2 supported 212kbps
	                DRI = 1;
	                DSI = 1;
	            }
	            else
	            {
	                return USER_ERROR;
	            }
	        }
	        else if(rate == 3)
	        {
	            if(((ATS[2]&BIT1)!=0) && ((ATS[2]&BIT5)!=0))
	            {
	                // DS=4,DR=4 supported 424kbps
	                DRI = 2;
	                DSI = 2;
	            }
	            else
	            {
	                return USER_ERROR;
	            }
	        }
	        else if(rate == 4)
	        {
	            if(((ATS[2]&BIT2)!=0) && ((ATS[2]&BIT6)!=0))
	            {
	                // DS=4,DR=4 supported 424kbps
	                DRI = 3;
	                DSI = 3;
	            }
	            else
	            {
	                return USER_ERROR;
	            }
	        }
	        else
	        {
	            return USER_ERROR;
	        }
	        write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	        set_bit_mask(TxModeReg, BIT7); //使能发送crc
	        set_bit_mask(RxModeReg, BIT7); //使能接收crc

	        pi.mf_command = PCD_TRANSCEIVE;
	        pi.mf_length  = 3;
	        pi.mf_data[0] = (0x0D << 4) + CID; //Start byte
	        pi.mf_data[1] = 0x01 | BIT4; //PPS0 ;BIT4:PPS1 transmited
	        pi.mf_data[2] = (DSI << 2) | DRI; //PPS1
	        status = pcd_com_transceive(pi);
	        if(status == MI_OK)
	        {
	            if(pi.mf_length == 8 && pi.mf_data[0] == ((0x0D << 4) + CID))
	            {
	                pcd_set_rate(rate);
	            }


	        }
	
	    }
	    return status;
	}
	
	
	private int pcd_attri_b(int[] PUPI, int dsi_dri, int pro_type, int CID, int[] answer)
	{
	    int  status;
	    transceive_buffer  mf_com_data=new transceive_buffer();
	    pro_type = pro_type;

	    pcd_set_tmo(g_fwi);

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length  = 9;
	    mf_com_data.mf_data[0] = ISO14443B_ATTRIB;
	    //memcpy(&mf_com_data.mf_data[1], PUPI, 4);
	    
	    //System.arraycopy(PUPI, 0, mf_com_data.mf_data, 0, 4);
	    System.arraycopy(PUPI, 0, mf_com_data.mf_data, 1, 4);

	    mf_com_data.mf_data[5] = 0x00;          // EOF/SOF required, default TR0/TR1
	    //EOFTEST

	    mf_com_data.mf_data[6] = ((dsi_dri << 4) | FSDI); //FSDI; // Max frame 64
	    mf_com_data.mf_data[7] = 0x01; //pro_type & 0x0f;  //;  ISO/IEC 14443-4 compliant?;
	    mf_com_data.mf_data[8] = (CID & 0x0f); //           // CID ,0 - 14,若不支持CID，则设置为0000

	    status  = pcd_com_transceive(mf_com_data);

	    if(status == MI_OK)
	    {
	        answer[0] = mf_com_data.mf_data[0];
	    }


	    return status;
	}
	
	private void gtag_init()
	{
		g_tag_info.clear();
		g_statistics.clear();
		//memset(&g_tag_info, 0, sizeof(g_tag_info));
	    //memset(&g_statistics, 0, sizeof(g_statistics));
//	    
	}
	private void Rfid_Init()
	{
	    pcd_init();//初始化pcd寄存器
	    pcd_antenna_off(); // 关闭场强
	    gtag_init();//初始化rfid本地变量
	//  write_reg(TxSelReg,0x14);
	// write_reg(RxSelReg,0x48);
	}
	private void pcd_init() {
	    int tmpagc_val=0;

	    //Sleep(50);
	    write_reg(0x3f, 0x01);
	    //Sleep(50);           //20230612
	    agc_val=0x00;//加入调试 //不给初值会报错
	    agc_val = read_reg(0x5f);
	    //tmpagc_val = read_reg(0x5f);
	    //Sleep(50);           //20230612


	    write_reg(0x3f, 0x00);
	    //memset(logBuf,0x00,sizeof(logBuf));
	    //sprintf(logBuf,"agc_val:0x%x\r\n",    agc_val);

	    write_reg(0x01, 0x00);
	    //delay_1ms(1);
	    //Sleep(1);
	    SystemClock.sleep(1);
	    pcd_reset();
	    //delay_1ms(1);
	    //Sleep(1);
	    SystemClock.sleep(1);
	    clear_bit_mask(Status2Reg, BIT3);

	    if(IRQ_MODE){
	        write_reg(ComIEnReg, 0x00);
	    } else {
	        write_reg(ComIEnReg, 0x80);
	    }                                   //20230608   IRQ_MODE 宏定义例程中为0 此处亦如此
	    write_reg(DivIEnReg, 0x80);   // irqpin  cmos output
	    write_reg(WaterLevelReg, 0x10);//  set waterlevel 16byte
	    write_reg(RxSelReg, 0x88);//RxWait
	    //write_reg(CWGsPReg, sl2523_cwgsp_init);  //20230609 注释掉
	    //write_reg(RFCfgReg, sl2523_rfcfg_init); //20230609 注释掉
	    write_reg(ControlReg, 0x10);  //default val
	    slm_arm(0); ///20230609到此处
	    write_reg(0x3f, 0x1);
	    //write_reg(0x45, sl2523_rfcfg1_init);

	    set_bit_mask(0x43, 0x80);
	    //write_reg(0x46, 0x68);
	    write_reg(0x61, 0x1a);//SL_WR_REG(0x61, 0x1a); //zzy 试着避开重新宏定义
	    //write_reg(0x68, 0x8);
	    //#ifdef _ENABLE_LPCD_
	    write_reg(0x5f, agc_val); // 读卡可以以此值作为参考，并固定该值
	    write_reg(0x3f, 0x0);

	    //Sleep(50);
	    //**************20230610*************************

	    write_reg(0x3f,0x01);
	    //memset(logBuf,0x00,sizeof(logBuf));
	    write_reg(0x3f, 0x00);
   
	}//20230801 可以用的
	
	private void slm_arm(int type) {
	    if('A' == type) {
	        write_reg(0x28, 0x24); //0x03
	        write_reg(0x26, 0x44);//GAIM  <6:4>
	        write_reg(0x3f, 0x1);
	        write_reg(0x45, 0x24);//迟滞区间<6:4>
	        //write_reg(0x5f, 0x84); //开启AGC功能 0x88
	        write_reg(0x61, 0x1e); //高通滤波器带宽<3:2>
	        write_reg(0x3f, 0x0);
	    } else if('B' == type) {
	        write_reg(0x28, 0x24); //0x03
	        write_reg(0x29, 0x07); //0x03
	        write_reg(0x26, 0x54);//GAIM  <6:4>
	        write_reg(0x3f, 0x1);
	        write_reg(0x45, 0x24);//迟滞区间<6:4>
	        //write_reg(0x5f, 0x84); //开启AGC功能 0x88
	        write_reg(0x61, 0x1e); //高通滤波器带宽<3:2>
	        write_reg(0x3f, 0x0);
	    } else if('F' == type) {
	        write_reg(0x28, 0x24); //0x03
	        write_reg(0x29, 0x07); //0x03
	        write_reg(0x26, 0x54);//GAIM  <6:4>
	        write_reg(0x3f, 0x1);
	        write_reg(0x45, 0x44);//迟滞区间<6:4>
	        //write_reg(0x5f, 0x84); //开启AGC功能 0x88
	        write_reg(0x61, 0x16); //高通滤波器带宽<3:2>
	        write_reg(0x3f, 0x0);
	    } else if('V' == type) {
	        write_reg(0x28, 0x06); //0x03
	        write_reg(0x26, 0x74);
	        write_reg(0x3f, 0x1);
	        write_reg(0x45, 0x34); //迟滞区间<6:4>
	        //write_reg(0x5f, 0x89); //开启AGC功能 0x88
	        write_reg(0x61, 0x16); //高通滤波器带宽<3:2>
	        write_reg(0x3f, 0x0);
	    } else {
	        write_reg(0x28, 0x24); //0x03
	        write_reg(0x29, 0x07); //0x03
	        write_reg(0x26, 0x44);//GAIM  <6:4>
	        write_reg(0x3f, 0x1);
	        write_reg(0x45, 0x24);//迟滞区间<6:4>
	        //write_reg(0x5f, 0x84); //开启AGC功能 0x88
	        write_reg(0x61, 0x1e); //高通滤波器带宽<3:2>
	        write_reg(0x3f, 0x0);
	    }
	}
	
	private int osc_calib() {

	    int val;
	    write_reg(0x3f, 0x00);
	    write_reg(0x01, 0x00);
	    delay_1ms(2);
	    write_reg(0x01, 0x06);
	    delay_1ms(10);
	    write_reg(0x3f, 0x01);
	    val = read_reg(0x4e);
	    write_reg(0x3f, 0x00);
	    if((val & 0x80)!=0) {
	        return 0;
	    } else {
	        return 1;
	    }
	}
	
	private int lpcd_exit() {
	    int adc_value;
	    int reg_val;
	    write_reg(0x01, 0x00);// wait wakeup done
	    delay_1ms(1);
	    reg_val = read_reg(0x5); //added by xlwang,20220805,for debug
	    if((reg_val & 0x60)!=0) {
	        //printf("LPCD_IRQ: irq=%x\r\n", reg_val);
	    } else {
	        //printf("Error: irq=%x, This is not LPCD IRQ\r\n", reg_val);
	    }
	    write_reg(0x3f, 0x2);
	    adc_value = read_reg(ADC_VALUE0);
	    //printf("after lpcd: read_adc_value=%x\r\n", adc_value);
	    write_reg(0x3f, 0x0);
	    return adc_value;
	}
	
	private void lpcd_entry() {
	    write_reg(0x01, 0x10);
	}
	
	private void adc_init() {
	    //     write_reg(LPCD_CTRL2,0x80); //adc_sample_time
	    write_reg(0x3f, 0x1); //
	    clear_bit_mask(0x65, 0x80);
	    write_reg(0x66, 0x00); // clkdelay=0s
	    write_reg(0x3f, 0x2); //
	    write_reg(LPCD_CTRL0, 0x13); //lpcd_out_inv=1,lpcd_envsel=0;lpcd_sample=0;lpcd_en=1;lpcd_work=1
	    write_reg(ADC_TIME0, ADC_TIME0_VAL); //adc_sample_time
	    write_reg(ADC_TIME1, 0x50); //adc_compare_time
	};
	
	private int get_adc_value() {
	    write_reg(0x3f, 0x2);
	    write_reg(ADC_CTRL, 0x1);
	    //delay_10us(8);
	    delay_1ms(1);
	    return read_reg(ADC_VALUE0);
	};
	
	private void agc_test(){
	    
	}
	
	private int agc_calib() {
	    int adc_value;
	    int agc_ctrl;
	    agc_test();

	    agc_ctrl = 0xd;
	    while(true) {
	        write_reg(0x3f, 0x1);
	        write_reg(AGC_CTRL, 0x80 + agc_ctrl);
	        //delay_10us(50);
	        delay_1ms(1);
	        write_reg(0x3f, 0x2);
	        adc_value = get_adc_value();
	        
	        if(adc_value > AGC_TARGET && agc_ctrl > 0x1) {
	            agc_ctrl--;
	        } else if(adc_value > AGC_RX_MAX && agc_ctrl == 0x0) {
	            //printf("Warning: The field is overflow! agc can not reduce it!\r\n");
	            break;
	        } else {
	            
	            break;
	        }
	    }
	    write_reg(0x3f, 0x00);
	    //printf("[0x28 = %x 0x29 = %x],agc_ctrl=%x,adc_value=%x\r\n", read_reg(0x28),read_reg(0x29),agc_ctrl, adc_value);
	    agc_val = 0x80 + agc_ctrl;
	    return agc_ctrl;

	};
	
	private void lpcd_init(int fst_en, int adc_value) {
	    if((fst_en == 0) && (adc_value > AGC_RX_MAX)) {
	        fst_en = 1;
//	        write_reg(0x3f, 0x1); //
//	        write_reg(0x5f,read_reg(0x5f)-1);
//	        write_reg(0x3f, 0x0); //
	        //printf("Warning! adc_value is too large, agc need rejust\r\n");
	    }
	    if((fst_en == 0) && (adc_value < AGC_RX_MIN)) {
	        //   write_reg(0x3f, 0x1); //
	        //    write_reg(0x5f,read_reg(0x5f)+1);
	        //    write_reg(0x3f, 0x0); //
	        fst_en = 1;
	        //printf("Warning! adc_value is too low, agc need rejust\r\n");
	    }
	    //if(fst_en) osc_calib();
	    write_reg(0x3f, 0x0); // adc_sample time
	    write_reg(DivIEnReg, 0xe0); // fst lpcd int en, lpcd_int_en;
	    write_reg(ComIrqReg, 0x7f); // clear irq
	    write_reg(DivIrqReg, 0x7f); // clear irq
	    write_reg(TxASKReg, 0x00)    ;
	    write_reg(0x28, 0x3f);
	    write_reg(0x29, 0x3f);
	    write_reg(0x3f, 0x01);
	    write_reg(0x61, 0x10); //set vmid res as min=1.4k, default=3.3K, bqliu 20211124
	    write_reg(0x5f, agc_val); // 读卡可以以此值作为参考，并固定该值
	    write_reg(0x3f, 0x00);
	    if(fst_en!=0) {
	        pcd_antenna_on(); //open field
	        delay_1ms(5);
	        adc_init();
	        agc_calib();
	    }
	    write_reg(0x3f, 0x2); // adc_sample time
	    write_reg(LPCD_CTRL0, 0x11 | (fst_en << 5)); //adc_sample_time,0x13->0x11, to turn off vmid(1mA), during spd mode in lpcd, bqliu 20211124
	    //write_reg(LPCD_PARAM0, adc_value + lpcd_cfg.sense); // config abs feild strength low threshold
	    //write_reg(LPCD_PARAM1, adc_value - lpcd_cfg.sense); // config abs feild strength high threshold
	    write_reg(LPCD_PARAM0, adc_value+1);
	    write_reg(LPCD_PARAM1, adc_value-1);
	    write_reg(LPCD_PARAM2, lpcd_cfg.sense); // config relative feild strength threshold
	    write_reg(LPCD_PARAM3, 0x2c); //lpcd_fst_en=1,lpcd_out_inv=1;lpcd_envsel=0;lpcd_en=1;lpcd_work=1;
	    // write_reg(LPCD_CTRL2,0x03);//ant_stable_time
	    write_reg(LPCD_TIME0, 0x2); //ant_stable_time
	    write_reg(LPCD_TIME1, lpcd_cfg.t1); //lpcd_piorid
	    write_reg(LPCD_TIME2, 0x2); //start up time
	    write_reg(0x3f, 0x0); //
	}
	
	
	private void lpcd_demo()
	{
	    int adc_value;
	    int wait_cnt = 0;
	    irq_flag_io = 0;
	    lpcd_cfg.t1 = LPCD_DETECT_PERIOD;      //0x12 300ms; //0x20 500ms ; //0x38 800ms
	    lpcd_cfg.sense = LPCD_DETECT_SENS;
	    //printf("******* start lpcd_demo() *******\r\n");
	    osc_calib();
	    lpcd_init(1, 0x40); //fst_en
	    lpcd_entry();
	    while(true) {
	        if(irq_flag_io!=0)
	        {
	            //printf("...................................\r\n");
	            //printf("\r\n******* l、pcd_exit() *********\r\n");
	            irq_flag_io = 0;
	            adc_value = lpcd_exit();
	            Rfid_Init();
	            //printf("******* 2、nfc_demo() *********\r\n");
	            lpcd_init(nfc_demo(), adc_value);
	            pcd_antenna_off();
	            //printf("******* 3、lpcd_entry() *******\r\n");
	            lpcd_entry();
	        } else {
	            wait_cnt++;
	            if(wait_cnt == 500000) {
	                wait_cnt = 0;
	                //printf(".");
	                //Memo1->Lines->Add("lpcd_demo has loop 500000");
	            }
	        }
	    }
	}
	
	private int pcd_auth_state(int auth_mode, int block, int[] psnr,int[] pkey)
	{
	    int status;
	    int i;
	    transceive_buffer  mf_com_data=new transceive_buffer();


	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	//  clear_bit_mask(RxModeReg, BIT7); //不使能接收crc， added by xlwang,20180404

	    pcd_set_tmo(4);

	    mf_com_data.mf_command = PCD_AUTHENT;
	    mf_com_data.mf_length = 12;
	    mf_com_data.mf_data[0] = auth_mode;
	    mf_com_data.mf_data[1] = block;
	    for(i = 0; i < 6; i++)
	    {
	        mf_com_data.mf_data[2+i] = pkey[i];
	    }
	    //memcpy(&mf_com_data.mf_data[8], psnr, 4);
	    System.arraycopy(psnr, 0, mf_com_data.mf_data, 8, 4);
	    status = pcd_com_transceive(mf_com_data);

	    if(MI_OK == status)
	    {
	        if((read_reg(Status2Reg) & BIT3)!=0)  //MFCrypto1On
	        {
	            status = MI_OK;
	        }
	        else
	        {
	            status = MI_AUTHERR;
	        }
	    }

	    return status;

	}
	
	private int pcd_read(int addr,int[] preaddata)
	{
	    int status;
	    transceive_buffer  mf_com_data=new transceive_buffer();

	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	    set_bit_mask(TxModeReg, BIT7); //使能发送crc
	    set_bit_mask(RxModeReg, BIT7); //使能接收crc
	    pcd_set_tmo(4);

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length  = 2;
	    mf_com_data.mf_data[0] = PICC_READ;
	    mf_com_data.mf_data[1] = addr;

	    status = pcd_com_transceive(mf_com_data);
	    if(status == MI_OK)
	    {
	        if(mf_com_data.mf_length != 0x80)
	        {
	            status = MI_BITCOUNTERR;
	        }
	        else
	        {
	            //memcpy(preaddata, &mf_com_data.mf_data[0], 16);
	        	System.arraycopy(mf_com_data.mf_data, 0, preaddata, 0, 16);
	        }
	    }

	    return status;
	}
	
	private int pcd_write(int addr,int[] pwritedata)
	{
	    int status;
	    transceive_buffer  mf_com_data=new transceive_buffer();


	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	    set_bit_mask(TxModeReg, BIT7); //使能发送crc
	    clear_bit_mask(RxModeReg, BIT7); //不使能接收crc

	    pcd_set_tmo(5);

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length  = 2;
	    mf_com_data.mf_data[0] = PICC_WRITE;
	    mf_com_data.mf_data[1] = addr;

	    status = pcd_com_transceive(mf_com_data);
	//   ret= read_reg(BitFramingReg);  // // Tx last bits = 0, rx align = 0
	//   ret= read_reg(ControlReg);  // // Tx last bits = 0, rx align = 0
	    if(status != MI_NOTAGERR)
	    {
	        if(mf_com_data.mf_length != 4)
	        {
	            status=MI_BITCOUNTERR;
	        }
	        else
	        {
	            mf_com_data.mf_data[0] &= 0x0F;
	            switch(mf_com_data.mf_data[0])
	            {
	                case 0x00:
	                    status = MI_NOTAUTHERR;
	                    break;
	                case 0x0A:
	                    status = MI_OK;
	                    break;
	                default:
	                    status = MI_CODEERR;
	                    break;
	            }
	        }
	    }
	    if(status == MI_OK)
	    {
	        pcd_set_tmo(5);

	        mf_com_data.mf_command = PCD_TRANSCEIVE;
	        mf_com_data.mf_length  = 16;
	        //memcpy(&mf_com_data.mf_data[0], pwritedata, 16);
	        System.arraycopy(pwritedata, 0, mf_com_data.mf_data, 0, 16);

	        status = pcd_com_transceive(mf_com_data);
	        if(status != MI_NOTAGERR)
	        {
	            mf_com_data.mf_data[0] &= 0x0F;
	            switch(mf_com_data.mf_data[0])
	            {
	                case 0x00:
	                    status = MI_WRITEERR;
	                    break;
	                case 0x0A:
	                    status = MI_OK;
	                    break;
	                default:
	                    status = MI_CODEERR;
	                    break;
	            }
	        }
	        pcd_set_tmo(4);
	    }
	    return status;
	}

	
	private int pcd_value(int dd_mode,int addr,int[] pValue)
	{

	    int status;
	    transceive_buffer  mf_com_data=new transceive_buffer();

	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	    set_bit_mask(TxModeReg, BIT7); //使能发送crc
	    clear_bit_mask(RxModeReg, BIT7); //不使能接收crc

	    pcd_set_tmo(5);

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length  = 2;
	    mf_com_data.mf_data[0] = dd_mode;
	    mf_com_data.mf_data[1] = addr;

	    status = pcd_com_transceive(mf_com_data);
	    if(status != MI_NOTAGERR)
	    {
	        if(mf_com_data.mf_length != 4)
	        {
	            status=MI_BITCOUNTERR;
	        }
	        else
	        {
	            mf_com_data.mf_data[0] &= 0x0F;
	            switch(mf_com_data.mf_data[0])
	            {
	                case 0x00:
	                    status = MI_NOTAUTHERR;
	                    break;
	                case 0x0A:
	                    status = MI_OK;
	                    break;
	                default:
	                    status = MI_CODEERR;
	                    break;
	            }
	        }
	    }
	    if(status == MI_OK)
	    {
	        pcd_set_tmo(5);

	        mf_com_data.mf_command = PCD_TRANSCEIVE;
	        mf_com_data.mf_length  = 4;
	        //memcpy(&mf_com_data.mf_data[0], pValue, 4);
	        System.arraycopy(pValue, 0, mf_com_data.mf_data, 0, 4);
	        status = pcd_com_transceive(mf_com_data);
	        if(status == MI_NOTAGERR || status == MI_OK)
	        {
	            status = MI_OK;
	        }

	    }

	    if(status == MI_OK)
	    {
	        mf_com_data.mf_command = PCD_TRANSCEIVE;
	        mf_com_data.mf_length  = 2;
	        mf_com_data.mf_data[0] = PICC_TRANSFER;
	        mf_com_data.mf_data[1] = addr;
	        pcd_set_tmo(5);
	        status = pcd_com_transceive(mf_com_data);
	        if(status != MI_NOTAGERR)
	        {
	            if(mf_com_data.mf_length != 4)
	            {
	                status=MI_BITCOUNTERR;
	            }
	            else
	            {
	                mf_com_data.mf_data[0] &= 0x0F;
	                switch(mf_com_data.mf_data[0])
	                {
	                    case 0x00:
	                        status = MI_NOTAUTHERR;
	                        break;
	                    case 0x0A:
	                        status = MI_OK;
	                        break;
	                    default:
	                        status = MI_CODEERR;
	                        break;
	                }
	            }
	        }
	    }
	    return status;
	}
	
	
	private int pcd_bak_value(int sourceaddr, int goaladdr)
	{
	    int status;
	    transceive_buffer  mf_com_data=new transceive_buffer();

	    write_reg(BitFramingReg,0x00);  // // Tx last bits = 0, rx align = 0
	    set_bit_mask(TxModeReg, BIT7); //使能发送crc
	    clear_bit_mask(RxModeReg, BIT7); //不使能接收crc

	    pcd_set_tmo(5);

	    mf_com_data.mf_command = PCD_TRANSCEIVE;
	    mf_com_data.mf_length  = 2;
	    mf_com_data.mf_data[0] = PICC_RESTORE;
	    mf_com_data.mf_data[1] = sourceaddr;

	    status = pcd_com_transceive(mf_com_data);
	    if(status != MI_NOTAGERR)
	    {
	        if(mf_com_data.mf_length != 4)
	        {
	            status=MI_BITCOUNTERR;
	        }
	        else
	        {
	            mf_com_data.mf_data[0] &= 0x0F;
	            switch(mf_com_data.mf_data[0])
	            {
	                case 0x00:
	                    status = MI_NOTAUTHERR;
	                    break;
	                case 0x0A:
	                    status = MI_OK;
	                    break;
	                default:
	                    status = MI_CODEERR;
	                    break;
	            }
	        }
	    }
	    if(status == MI_OK)
	    {
	        pcd_set_tmo(5);

	        mf_com_data.mf_command = PCD_TRANSCEIVE;
	        mf_com_data.mf_length  = 4;
	        //memset(&mf_com_data.mf_data[0], 0, 4);
	        Arrays.fill(mf_com_data.mf_data, 0);

	        status = pcd_com_transceive(mf_com_data);
	        if(status != MI_NOTAGERR)
	        {
	            mf_com_data.mf_data[0] &= 0x0F;
	            switch(mf_com_data.mf_data[0])
	            {
	                case 0x00:
	                    status = MI_WRITEERR;
	                    break;
	                case 0x0A:
	                    status = MI_OK;
	                    break;
	                default:
	                    status = MI_CODEERR;
	                    break;
	            }
	        }

	    }
	    if(status == MI_OK)
	    {
	        pcd_set_tmo(4);
	        mf_com_data.mf_command = PCD_TRANSCEIVE;
	        mf_com_data.mf_length  = 2;
	        mf_com_data.mf_data[0] = PICC_TRANSFER;
	        mf_com_data.mf_data[1] = goaladdr;

	        status = pcd_com_transceive(mf_com_data);
	        if(status != MI_NOTAGERR)
	        {
	            if(mf_com_data.mf_length != 4)
	            {
	                status=MI_BITCOUNTERR;
	            }
	            else
	            {
	                mf_com_data.mf_data[0] &= 0x0F;
	                switch(mf_com_data.mf_data[0])
	                {
	                    case 0x00:
	                        status = MI_NOTAUTHERR;
	                        break;
	                    case 0x0A:
	                        status = MI_OK;
	                        break;
	                    default:
	                        status = MI_CODEERR;
	                        break;
	                }
	            }
	        }
	    }
	    return status;
	}
	
	
	private int InitBlock(int addr,int[] value)
	{
	    //unsigned char tmp[16],i;
		//unsigned char tmp[16];//这里可以问一下
		//unsigned char i;
		int []tmp=new int[16];
		int i;
	    int status;
	    for(i=0; i<4; i++)
	    {
	        tmp[i]=value[i];
	        tmp[i+4]=255-value[i];
	        tmp[i+8]=value[i];
	    }
	    tmp[12]=addr;
	    tmp[13]=255-addr;
	    tmp[14]=tmp[12];
	    tmp[15]=tmp[13];
	    status=pcd_write(addr,tmp);
	    return status;
	}
	
	
	//控制蜂鸣器 BEEP 0 = 短响  1 = 长响
	public void PS_RF_Beep(int inBeep)
	{
		clear();
		buildLen=0;
		
		//int[] tmpBuf1=new int[256];
		//int tmpBufLen1=0;
		//memset(tmpBuf1,0x00,sizeof(tmpBuf1));
		//Arrays.fill(tmpBuf1,0x00);
		//tmpBufLen1=0;
		PACKET[buildLen++]=0x08;
		PACKET[buildLen++]=0x26;
		//PACKET[buildLen++]=0x00;
		//PACKET[buildLen++]=0x01;
		if(inBeep==1)
			PACKET[buildLen++]=0x01;
		else
			PACKET[buildLen++]=0x00;
		//if(HIDHandle==NULL)
		//	return;
		//HID_WRITE(tmpBuf1,tmpBufLen1,1);
		mUSBOp.sendDatarRD07Rfid(PACKET,buildLen);

	}
	
	//////上面为添加代码///////////////////////////////////////////////
	///////////////////////////////////////////////////////////////
	private static void clear() {
		Arrays.fill(PACKET, 0);
	}
	public USBOp mUSBOp;
	///20190822 添加构造函数
	public RFIDPnPacket(UsbManager manager,Context context, String AppName)
	{
			
			mUSBOp= new USBOp(manager,context,AppName);
			if(!mUSBOp.UsbFeatureSupported()){
				Log.e("jack", "设备不支持USBHOST");
			}
			//mUSBOp.SetUsbConfig( 0x7580,0x10c4, 10, 0x00, 0x00);
			//mUSBOp.SetUsbConfig( 0x8238,0x10c4, 0x03, 0x01, 0x02);
			mUSBOp.SetUsbConfig( 0x8238,0x1A86, 0x03, 0x01, 0x02);
			
			//--------------------------------------------------------------
		    /////////////加入状态的初始化////////////////////////////
			agc_val=0;
		    sl2523_rfcfg_init=0x54;
		    sl2523_rfcfg1_init=0x24;
		    sl2523_cwgsp_init=0x24;
		    sl2523_emv_mode=1;
		    sl2523_modgsp_init=0x07;
			/////////////////////////////////////////////////////////
			
			//LIFCMDNUM=3;
			//批量传输
			WriteCmdBufLen=0;//WriteCmdBuf
			Arrays.fill(WriteCmdBuf,0x00);
	}
	//设置串口波特率
/*	public void SetBaud()
	{
		mUSBOp.setUsbBaud();
	}*/
	
/////////////////数据打包/////////////////////////////////////////////////////////////////////////
	public static int buildPackage(int[] inTFI,int inTFILen,int[] inData, int inDataLen)
	{
		
		int[] tmpPACKET = new int[1024];
		Arrays.fill(tmpPACKET, 0);
		clear();
		int i = 0, j = 0;
		int ch;
		int tmpChk;
		//序列头
		tmpPACKET[i++] = 0x00;
		//起始号
		tmpPACKET[i++] = 0x00;
		tmpPACKET[i++] = 0xFF;
		//判断TFI+数据 是否大于256
	    //长度和长度校验和
		if((inTFILen+inDataLen)<256)//TFI 和 data数据小于256个
		{
			ch=(inTFILen+inDataLen)%256;
			tmpPACKET[i++] = ch;
			tmpPACKET[i++] = (0x100-ch)%256;
			
		}
		else
		{
			tmpPACKET[i++] = 0xFF;
			tmpPACKET[i++] = 0xFF;
			ch=((inTFILen+inDataLen)%(256*256))/256;//长度高位
			tmpPACKET[i++] = ch;
			tmpChk=ch;
			ch=(inTFILen+inDataLen)%256;
			tmpPACKET[i++]	= ch;
			tmpChk=tmpChk+ch;
			tmpPACKET[i++]=(0x100-(tmpChk%256))%256;	
		}
		//TFI
		System.arraycopy(inTFI, 0, tmpPACKET, i, inTFILen);
		i=i+inTFILen;
		//数据
		System.arraycopy(inData, 0, tmpPACKET, i, inDataLen);
		i=i+inDataLen;
		//数据校验和
		tmpChk=0;
		for(j=0;j<inTFILen;j++)
			tmpChk+=inTFI[j];
		for(j=0;j<inDataLen;j++)
			tmpChk+=inData[j];
		tmpPACKET[i++] = (0x100-(tmpChk%256))%256;
		//序列结尾
		tmpPACKET[i++]=0x00;
		j=0;
		PACKET[j++]=0x08;
		PACKET[j++]=0x41;
		PACKET[j++]=i/256;
		PACKET[j++]=i%256;
		System.arraycopy(tmpPACKET, 0, PACKET, j, i);
		j=j+i;
		return j;
	}
	
	public static int resolveConfigData(int[] configData) {
		int[] GOALBUF=new int[9];
		int i,configDataLen;
		int tmpDataLen=0;
		int tmpChk;
		int ch;
		GOALBUF[0]=0x00;
		GOALBUF[1]=0x00;
		GOALBUF[2]=0xFF;
		GOALBUF[3]=0x00;
		GOALBUF[4]=0xFF;
		GOALBUF[5]=0x00;
		GOALBUF[6]=0x00;
		GOALBUF[7]=0x00;
		GOALBUF[8]=0xFF;
		for(i=0;i<9;i++)
		{
			if(GOALBUF[i]!=PACKET[i])
				break;
		}
		if(i<9)
			return FORMART_ERR;
		//获取到实际读取数据长度
		for(i=1024;i>=0;i--)
		{
			if(PACKET[i]!=0)
				break;	
		}
		configDataLen=i+1+1;//这里要加上最后的一个0
		//从长度判断格式是否正确
		//大于255长度的情况判断
		if(configDataLen>(255+9+3+1))
		{
			tmpDataLen=PACKET[9+2]*256;
			tmpDataLen=tmpDataLen+PACKET[9+3];
			if(configDataLen!=(tmpDataLen+9+3+2+1))//比起小于等于255的情况多了两个0xFF 0xFF
				return FORMART_ERR;
			//加入判断长度校验
			tmpChk=PACKET[11]+PACKET[12];
			ch=(0x100-(tmpChk)%256)%256;
			if(ch!=PACKET[13])
				return FORMART_ERR;
			//加入判断数据校验
			tmpChk=0;
			for(i=0;i<tmpDataLen;i++)
			{
				tmpChk+=PACKET[14+i];	
				
			}
			ch=(0x100-(tmpChk%256))%256;
			if(PACKET[configDataLen-2]!=ch)//倒数第二个数为校验
				return FORMART_ERR;
			System.arraycopy(PACKET, 14, configData, 0, tmpDataLen);
			return tmpDataLen;		
		}
		else
		{
			tmpDataLen=PACKET[9];
			if(configDataLen!=(tmpDataLen+9+3+1))
				return FORMART_ERR;
			//加入判断长度校验
			ch=(0x100-PACKET[9])%256;
			if(ch!=PACKET[10])
				return FORMART_ERR;
			//加入判断数据校验
			tmpChk=0;
			for(i=0;i<tmpDataLen;i++)
				tmpChk+=PACKET[11+i];
			ch=(0x100-(tmpChk%256))%256;
			if(PACKET[configDataLen-2]!=ch)//倒数第二个数为校验
				return FORMART_ERR;
			///加入判断应用程序级错误The syntax error frame is used to inform the host controller that the PN532 has detected 
			//an error at the application level
			//00 00 FF 01 FF 7F 81 00
			if((tmpDataLen==1)&&(PACKET[11]==0x7F))
				return FORMART_ERR;
			System.arraycopy(PACKET, 11, configData, 0, tmpDataLen);
			return tmpDataLen;
			
		}
		
		
		
	}
	
	public static String convert2String(int data[], int length) {
		if (length < 1)
			return "";
		StringBuilder sb = new StringBuilder();
		for (int n = 0; n < length; n++) {
			String hex = Integer.toHexString(data[n] & 0xFF);
			if (hex.length() == 1)
				hex = '0' + hex;

			sb.append(hex.toUpperCase() + " ");
			if (n > 0 && (n + 1) % 16 == 0) {
				sb.append("\n");
			}
		}
		return sb.toString();
	}
	
	/**
	 * 功能：打开或者关闭天线
	 * @param inMode 0 = 打开天线      1 = 关闭天线
	 * @return 
	 * 		  0  =   SUCCESS
	 * 		      其他值错误
	 */
	public int PN_RF_Set(int inMode)
	//public String PS_RF_Set(int inMode)
	{
		
		/*int buildLen;
		int []tmpBuf=new int [128];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x32,0x01,0x00};
		int iRet;
		if(inMode==0)//唤醒模块
		{
			clear();
			buildLen=0;
			PACKET[buildLen++]=0x08;
			PACKET[buildLen++]=0x41;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x1C;
			PACKET[buildLen++]=0x55;
			PACKET[buildLen++]=0x55;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=0x00;
			PACKET[buildLen++]=(byte)0xFF;
			PACKET[buildLen++]=0x03;
			PACKET[buildLen++]=(byte)0xFD;
			PACKET[buildLen++]=(byte)0xD4;
			PACKET[buildLen++]=0x14;
			PACKET[buildLen++]=0x01;
			PACKET[buildLen++]=0x17;
			PACKET[buildLen++]=0x00;
			//buildLen=28;
			iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
			if (iRet < 0)
				return READ_ERR;
			iRet=resolveConfigData(tmpBuf);
			if(iRet<0)
				return READ_ERR; 
			//recBuf=convert2String(PACKET,18);
				
		}
		else//关闭天线
		{
			buildLen=buildPackage(tmpTFI,1,tmpData,3);
			iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
			if (iRet < 0)
			return READ_ERR;
			iRet=resolveConfigData(tmpBuf);
			if(iRet<0)
				return READ_ERR; 
			//return SUCCESS;
			//recBuf=convert2String(PACKET,18);
			
		}*/
		int ret;
		int wait_for=0;
	    //memset(&g_tag_info,0x00,sizeof(g_tag_info));
		g_tag_info.clear();
		if(mUSBOp.UsbOpenflag==false)
			return COMOPEN_ERR;

		if(inMode==0)
		{

			write_reg(0x01,0x0f);//PCD复位 //write_reg(CommandReg, PCD_RESETPHASE);//20230704

			ret=read_reg(0x37);//20230704

			Rfid_Init();//zzy 20230704


			pcd_default_info();
			irq_flag_io=0;
		}
		else
		{
			//Rfid_Init();//zzy 20230704	
			pcd_init();
		}
		return SUCCESS;
		//return recBuf;
		
	}
	//int32_t WINAPI RFID_M1_Search(int8_t * outCardNo,int32_t *outCardNoLen,int32_t inTimeOut)
	/**
	 * 函数功能： 寻卡
	 * @param outCardNoData
	 * @return
	 */
	public int PN_RF_M1_Search(int []outCardNoData,int[] outCardNoLen,int inTimeOut)
	{
		/*int buildLen;
		int cardNoDataLen;
		int []tmpBuf=new int [128];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x4A,0x01,0x00};// 01 表示读一张卡 00表示106kbps
		int iRet;	
		buildLen=buildPackage(tmpTFI,1,tmpData,3);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet==-1005)
			iRet=-1005;
		if(iRet<0)
			return iRet;
		cardNoDataLen=iRet;
		System.arraycopy(tmpBuf, cardNoDataLen-4, outCardNoData, 0, 4);//除掉型号和密钥类型
		return 4;*/
		//PS_RF_Beep(0);
		long startTime;
		startTime=System.currentTimeMillis();
		while(true)
		{
		
			
			Rfid_Init();
			//pcd_init();//初始化pcd寄存器
			pcd_default_info();
			//TRACE_LOG("RFID_M1_Search 1");
			nfc_demo();//这里源码有个循环
			//TRACE_LOG("RFID_M1_Search 2");
			if(g_tag_info.uid_length>0)
			{
				PS_RF_Beep(0);
				if(MY_CARD_TYPE!='A')//如果实际操作卡片和非M1卡报卡片类型不匹配错误
					return CARD_OF_OTHER_TYPE;
				outCardNoLen[0]=g_tag_info.uid_length;
				//memcpy(outCardNo,g_tag_info.serial_num,g_tag_info.uid_length);
				System.arraycopy(g_tag_info.serial_num, 0, outCardNoData, 0, g_tag_info.uid_length); //方向要反过来
				//return SUCCESS;
				return 0;
			}
			
			if((System.currentTimeMillis()-startTime)/1000>=5)
				break;
			SystemClock.sleep(10);
			//else
			//	break;
			//else
			//	return READ_CARD_ERR;
			//tEnd = clock();
			//if( ((tEnd - tStart) / CLK_TCK) > inTimeOut ) 
			 // 	break;
			//PeekMessage(&Msg, 0, 0, 0, PM_REMOVE); 
			//TranslateMessage(&Msg);
			//DispatchMessage(&Msg);
		}
		PS_RF_Beep(0);
		SystemClock.sleep(BEEPDELAYTIME);
		PS_RF_Beep(1);
		return READ_CARD_ERR;
			
	}
	//int32_t WINAPI RFID_M1_Authentication(int32_t inBlock,int8_t* inCardNo,int32_t inCardNoLen,int8_t* inCardKey,int32_t inCardKeyLen,int32_t inTimeOut)
	/**
	 * 函数功能：	验证秘钥
	 * @param inBlock 		块号			--input			
	 * @param inCardNo		卡号			--input
	 * @param inCardNoLen	卡号长度		--input
	 * @param inCardKey		秘钥			--input
	 * @param inCardKeyLen	秘钥长度		--input
	 * @param inTimeOut		超时			--input
	 * @return
	 * 			成功			0
	 *			失败			负数
	 */
	public int PN_RF_M1_Authentication(int inBlock,int []inCardNo,int inCardNoLen,int []inCardKey,int inCardKeyLen,int inTimeOut)
	{
		/*int []tmpBuf=new int [128];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x40,0x01,0x60};// 01 表示读一张卡 00表示106kbps
		int tmpBufLen=0;
		int buildLen=0;
		int iRet;
		//验证秘钥
		//加入块号
		Arrays.fill(tmpBuf, 0);
		System.arraycopy(tmpData, 0, tmpBuf, tmpBufLen, 3);
		tmpBufLen=tmpBufLen+3;
		tmpBuf[tmpBufLen++]=inBlock%256;
	
		//加入秘钥
		System.arraycopy(inCardKey, 0, tmpBuf, tmpBufLen, inCardKeyLen);
		tmpBufLen=tmpBufLen+inCardKeyLen;
		//加入卡号
		System.arraycopy(inCardNo, 0, tmpBuf, tmpBufLen, inCardNoLen);
		tmpBufLen=tmpBufLen+inCardNoLen;
		//打包
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		//if(tmpBuf[iRet-1]!=0x00)
		if(tmpBuf[2]!=0x00)
			return AUTHENTICATION_ERR;
		
		return SUCCESS;*/
		
		int uRet;
		int[] mUID=new int[256];
	    int[] mKey=new int[32];
		int mBlock;
		//memset(mUID,0x00,sizeof(mUID));
	    //memset(mKey,0x00,sizeof(mKey));
		if(inBlock<256)//为了兼容S70的卡片 将数据块的最大上限设置为256
			mBlock=inBlock;
		else
			mBlock=0;//都默认操作的第0扇区第0块

		//memcpy(mUID,inCardNo,inCardNoLen);
		//memcpy(mKey,inCardKey,inCardKeyLen);
		System.arraycopy(inCardNo, 0, mUID, 0, inCardNoLen);
		System.arraycopy(inCardKey, 0, mKey, 0, inCardKeyLen);

		uRet=pcd_auth_state(0x60,inBlock%256,mUID,mKey);//固定用A秘钥验证方式
		if(uRet==MI_OK)
			return SUCCESS;
		else 
			return AUTHENTICATION_ERR;		
			
	}
	/**
	 * 函数功能：	读S50卡片
	 * @param inBlock		块号
	 * @param outCardNoData	读到的卡片数据
	 * @param inTimeOut		读卡超时
	 * @return
	 * 		  失败				负数
	 * 		  成功				返回读到的卡片数据长度
	 */
	public int PN_RF_M1_Read(int inBlock,int []outCardNoData,int inTimeOut)
	{
		/*int []tmpBuf=new int [128];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x40,0x01,0x30};// 01 表示读一张卡 00表示106kbps
		int tmpBufLen=0;
		int buildLen=0;
		int iRet;
		//int cardNoDataLen;
		
		//读卡
		//加入块号
		Arrays.fill(tmpBuf, 0);
		System.arraycopy(tmpData, 0, tmpBuf, tmpBufLen, 3);
		tmpBufLen=tmpBufLen+3;
		tmpBuf[tmpBufLen++]=inBlock%256;
		//打包
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		//cardNoDataLen=iRet;
		if(tmpBuf[2]!=0x00)
			return READ_CARD_ERR;
		System.arraycopy(tmpBuf, 3, outCardNoData, 0, 16);//除掉型号和密钥类型
		return 16;	*/
		
		int uRet;
		int[] mCardData=new int[64];
		int mBlock;
		//memset(mCardData,0x00,sizeof(mCardData));
		Arrays.fill(mCardData, 0);
		mBlock=inBlock;
		uRet=pcd_read(mBlock,mCardData);
		if(uRet==MI_OK)
		{
			//outCardDataLen[0]=16;//此处读到的卡片数据固定长度为16
			//memcpy(outCardData,mCardData,16);
			System.arraycopy(mCardData, 0, outCardNoData, 0, 16); 
			//return SUCCESS;
			return 16;
		}
		else 
			return READ_CARD_ERR;	
	}
	//int32_t WINAPI RFID_M1_Write(int32_t inBlock,int8_t * inCardData,int32_t inCardDataLen,int32_t inTimeOut)
	/**
	 * 函数功能： 写卡
	 * @param inBlock			写卡块号
	 * @param inCardNoData		写入的数据
	 * @param inCardNoDataLen	写入的数据长度
	 * @param inTimeOut			超时
	 * @return 
	 * 		 	成功 				0
	 * 			失败				负数	
	 */
	public int PN_RF_M1_Write(int inBlock,int []inCardNoData,int inCardNoDataLen,int inTimeOut)
	{
		/*int []tmpBuf=new int [128];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x40,0x01,0xA0};// 01 表示读一张卡 00表示106kbps
		int tmpBufLen=0;
		int buildLen=0;
		int iRet;
		
		//写卡
		//加入块号
		Arrays.fill(tmpBuf, 0);
		System.arraycopy(tmpData, 0, tmpBuf, tmpBufLen, 3);
		tmpBufLen=tmpBufLen+3;
		tmpBuf[tmpBufLen++]=inBlock%256;
		//加入块数据
		System.arraycopy(inCardNoData, 0, tmpBuf, tmpBufLen, inCardNoDataLen);
		tmpBufLen+=inCardNoDataLen;
		
		//打包
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		if(tmpBuf[2]!=0x00)
			return READ_CARD_ERR;
		return SUCCESS;*/
		
		int uRet;
		int[] mCardData=new int[64];
		int mBlock;
		Arrays.fill(mCardData, 0);
		mBlock=inBlock;
		//memcpy(mCardData,inCardData,inCardDataLen);
		System.arraycopy(inCardNoData, 0, mCardData, 0, inCardNoDataLen);
		uRet=pcd_write(mBlock,mCardData);
		if(uRet==MI_OK)
			return SUCCESS;
		else
			return WRITE_ERR;
			
	}
	//int32_t WINAPI RFID_M1_PurseInit(int32_t inBlock,int32_t inAmount,int32_t inTimeOut)
	/**
	 * 函数功能：	初始化电子钱包
	 * @param inBlock		用作钱包的块号
	 * @param inAmount		初始化钱包金额
	 * @param inTimeOut		超时
	 * @return 
	 * 		 	成功 				0
	 * 			失败				负数	
	 */
	public int PN_RF_M1_PurseInit(int inBlock,long inAmount,int inTimeOut)
	{
		/*int []tmpBuf=new int [128];
		int tmpBufLen;
		int iRet;
		//判断初始化钱包的值是否超标
		if(inAmount<0||inAmount>0xFFFFFFFFL)
			return FORMART_ERR;
		if(inBlock<=0||(inBlock%4==3))//第0块和 每个扇区的第3块不能当钱包使用
			return FORMART_ERR;
		
		//初始化
		Arrays.fill(tmpBuf, 0);
		tmpBufLen=0;
		tmpBuf[tmpBufLen++]=(int)inAmount%256;	
		tmpBuf[tmpBufLen++]=(int)(inAmount/256)%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256))%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256*256))%256;
		tmpBuf[tmpBufLen++]=~tmpBuf[0];
		tmpBuf[tmpBufLen++]=~tmpBuf[1];
		tmpBuf[tmpBufLen++]=~tmpBuf[2];
		tmpBuf[tmpBufLen++]=~tmpBuf[3];
		tmpBuf[tmpBufLen++]=tmpBuf[0];
		tmpBuf[tmpBufLen++]=tmpBuf[1];
		tmpBuf[tmpBufLen++]=tmpBuf[2];
		tmpBuf[tmpBufLen++]=tmpBuf[3];
		tmpBuf[tmpBufLen++]=(inBlock%256);
		tmpBuf[tmpBufLen++]=~(inBlock%256);
		tmpBuf[tmpBufLen++]=(inBlock%256);
		tmpBuf[tmpBufLen++]=~(inBlock%256);
		
		iRet=PN_RF_M1_Write(inBlock,tmpBuf,tmpBufLen,inTimeOut);
		return iRet;	*/
		int[] tmpBuf=new int[16];
		int tmpBufLen;
		int mBlock;
		int cRet;
		mBlock=inBlock;
		Arrays.fill(tmpBuf, 0);
		//memset(tmpBuf,0x00,sizeof(tmpBuf));
		tmpBufLen=0;
		tmpBuf[tmpBufLen++]=(int)inAmount%256;	
		tmpBuf[tmpBufLen++]=(int)(inAmount/256)%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256))%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256*256))%256;
		cRet=InitBlock(inBlock,tmpBuf);
		if(cRet==MI_OK)
			return SUCCESS;
		else
			return TRANSFER_ERR;
	}
	//int32_t WINAPI RFID_M1_PurseBalance(int32_t inBlock,int32_t *inAmount,int32_t inTimeOut)
	/**
	 * 函数功能：	读取钱包余额
	 * @param inBlock		钱包的块号
	 * @param inTimeOut		读钱包余额超时
	 * @return
	 * 		  >=0		    钱包金额
	 * 		  <0		    读取钱包余额失败
	 */
	public long PN_RF_M1_PurseBalance(int inBlock,int inTimeOut)
	{
		int []tmpBuf=new int [128];
		int tmpBufLen;
		int tmpCh;
		int iRet,i;
		if(inBlock<=0||(inBlock%4==3))//第0块和 每个扇区的第3块不能当钱包使用
			return FORMART_ERR;
		
		//初始化
		Arrays.fill(tmpBuf, 0);
		tmpBufLen=0;
		
		iRet=PN_RF_M1_Read(inBlock,tmpBuf,inTimeOut);
		if(iRet<0)
			return iRet;
		//验证读到的钱包格式
		//验金额反码
		for(i=0;i<4;i++)
		{
			tmpCh=~tmpBuf[i];
			tmpCh&=0xFF;
			if(tmpCh!=tmpBuf[i+4])
				return PURSE_FORMAT_ERR;
			
		}
		//验证金额正码
		for(i=0;i<4;i++)
		{
			if(tmpBuf[i]!=tmpBuf[i+8])
				return PURSE_FORMAT_ERR;	
		}
		//验证地址正码
		tmpCh=inBlock%256;
		if((tmpCh!=tmpBuf[12])||(tmpCh!=tmpBuf[14]))
			return PURSE_FORMAT_ERR;
		//验证地址反码
		tmpCh=~tmpCh;
		tmpCh&=0xFF;
		if((tmpCh!=tmpBuf[13])||(tmpCh!=tmpBuf[15]))
			return PURSE_FORMAT_ERR;
		//返回金额
		return tmpBuf[0]+tmpBuf[1]*256+tmpBuf[2]*256*256+tmpBuf[3]*256*256*256;
		
	}
	//int32_t WINAPI RFID_M1_PurseIncrease(int32_t inBlock,int32_t inAmount,int32_t inTimeOut)
	/**
	 * 函数功能：	电子钱包充值
	 * @param inBlock		钱包的块号
	 * @param inAmount		充值金额
	 * @param inTimeOut		钱包充值超时
	 * @return
	 * 		 	成功 				0
	 * 			失败				负数	
	 */
	public int PN_RF_M1_PurseIncrease(int inBlock,long inAmount,int inTimeOut)
	{
	/*	int []tmpBuf=new int [128];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x40,0x01,0xC1};// 01 表示读一张卡 00表示106kbps
		int tmpData1[]={0x40,0x01,0xB0};// 
		int tmpBufLen=0;
		int buildLen=0;
		int iRet;
		
		//加入块号
		Arrays.fill(tmpBuf, 0);
		tmpBufLen=0;
		System.arraycopy(tmpData, 0, tmpBuf, tmpBufLen, 3);
		tmpBufLen=tmpBufLen+3;
		//加入块号
		tmpBuf[tmpBufLen++]=inBlock%256;
		//加入金额
		tmpBuf[tmpBufLen++]=(int)inAmount%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/256)%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256))%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256*256))%256;
		
		//打包
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		
		if(tmpBuf[2]!=0x00)
			return READ_FORMAT_ERR;
		
		//接着发送传输指令B0
		//加入块号
		Arrays.fill(tmpBuf, 0);
		tmpBufLen=0;
		System.arraycopy(tmpData1, 0, tmpBuf, tmpBufLen, 3);
		tmpBufLen=tmpBufLen+3;
		tmpBuf[tmpBufLen++]=inBlock%256;
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		if(tmpBuf[2]!=0x00)
			return READ_FORMAT_ERR;
		return SUCCESS;*/
		int[] tmpBuf=new int[16];
		int tmpBufLen;
		int mBlock;
		int cRet;
		mBlock=inBlock;
		Arrays.fill(tmpBuf, 0);
		//memset(tmpBuf,0x00,sizeof(tmpBuf));
		tmpBufLen=0;
		tmpBuf[tmpBufLen++]=(int)inAmount%256;	
		tmpBuf[tmpBufLen++]=(int)(inAmount/256)%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256))%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256*256))%256;
		//char pcd_value(unsigned char dd_mode,unsigned char addr,unsigned char *pValue)
		cRet=pcd_value(0xC1,mBlock,tmpBuf);
		if(cRet==MI_OK)
			return SUCCESS;
		else
			return TRANSFER_ERR;
	}
	
	/**
	 * 函数功能：	电子钱包扣款
	 * @param inBlock		钱包的块号
	 * @param inAmount		扣款金额
	 * @param inTimeOut		钱包扣款超时
	 * @return
	 * 		 	成功 				0
	 * 			失败				负数	
	 */
	public int PN_RF_M1_PurseDecrease(int inBlock,long inAmount,int inTimeOut)
	{
		/*int []tmpBuf=new int [128];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x40,0x01,0xC0};// 01 表示读一张卡 00表示106kbps
		int tmpData1[]={0x40,0x01,0xB0};// 01 表示读一张卡 00表示106kbps
		int tmpBufLen=0;
		int buildLen=0;
		int iRet;
		
		Arrays.fill(tmpBuf, 0);
		tmpBufLen=0;
		System.arraycopy(tmpData, 0, tmpBuf, tmpBufLen, 3);
		tmpBufLen=tmpBufLen+3;
		//加入块号
		tmpBuf[tmpBufLen++]=inBlock%256;
		//加入金额
		tmpBuf[tmpBufLen++]=(int)inAmount%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/256)%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256))%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256*256))%256;
		
		//打包
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		if(tmpBuf[2]!=0x00)
			return READ_FORMAT_ERR;
		
		//接着发送传输指令B0
		//加入块号
		Arrays.fill(tmpBuf, 0);
		tmpBufLen=0;
		System.arraycopy(tmpData1, 0, tmpBuf, tmpBufLen, 3);
		tmpBufLen=tmpBufLen+3;
		tmpBuf[tmpBufLen++]=inBlock%256;
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		if(tmpBuf[2]!=0x00)
			return READ_FORMAT_ERR;
		
		
		return SUCCESS;*/
		
		int[] tmpBuf=new int[16];
		int tmpBufLen;
		int mBlock;
		int cRet;
		mBlock=inBlock;
		//memset(tmpBuf,0x00,sizeof(tmpBuf));
		Arrays.fill(tmpBuf, 0);
		tmpBufLen=0;
		tmpBuf[tmpBufLen++]=(int)inAmount%256;	
		tmpBuf[tmpBufLen++]=(int)(inAmount/256)%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256))%256;
		tmpBuf[tmpBufLen++]=(int)(inAmount/(256*256*256))%256;
		//char pcd_value(unsigned char dd_mode,unsigned char addr,unsigned char *pValue)
		cRet=pcd_value(0xC0,mBlock,tmpBuf);
		if(cRet==MI_OK)
			return SUCCESS;
		else
			return TRANSFER_ERR;
	}
	
	//int32_t WINAPI RFID_CPU_Search(int32_t inTimeOut)
	/**
	 * 函数功能： 寻非接CPU卡
	 * @param inTimeOut 		寻卡超时时间
	 * @return
	 * 		 	成功 				0
	 * 			失败				负数	
	 */
	public int PN_RF_CPU_Search(int inTimeOut)
	{
		
		/*int []tmpBuf=new int [128];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x4A,0x01,0x00};// 01 表示读一张卡 00表示106kbps
		int tmpBufLen=0;
		int buildLen=0;
		int iRet;
		
		//寻号
		Arrays.fill(tmpBuf, 0);
		System.arraycopy(tmpData, 0, tmpBuf, tmpBufLen, 3);
		tmpBufLen=tmpBufLen+3;
		
		//打包
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		return SUCCESS;*/
		long startTime;
		startTime=System.currentTimeMillis();
		while(true)
		{
		
			
			Rfid_Init();
			//pcd_init();//初始化pcd寄存器
			pcd_default_info();
			//TRACE_LOG("RFID_M1_Search 1");
			nfc_demo();//这里源码有个循环
			//TRACE_LOG("RFID_M1_Search 2");
			if(g_tag_info.uid_length>0)
			{
				PS_RF_Beep(0);
				if(MY_CARD_TYPE!='A')//如果实际操作卡片和非M1卡报卡片类型不匹配错误
					return CARD_OF_OTHER_TYPE;
				//outCardNoLen[0]=g_tag_info.uid_length;
				//memcpy(outCardNo,g_tag_info.serial_num,g_tag_info.uid_length);
				//System.arraycopy(g_tag_info.serial_num, 0, outCardNoData, 0, g_tag_info.uid_length); //方向要反过来
				//return SUCCESS;
				return 0;
			}
			if((System.currentTimeMillis()-startTime)/1000>=5)
				break;
			SystemClock.sleep(10);
			//else
			//	break;
			//else
			//	return READ_CARD_ERR;
			//tEnd = clock();
			//if( ((tEnd - tStart) / CLK_TCK) > inTimeOut ) 
			 // 	break;
			//PeekMessage(&Msg, 0, 0, 0, PM_REMOVE); 
			//TranslateMessage(&Msg);
			//DispatchMessage(&Msg);
		}
		PS_RF_Beep(0);
		SystemClock.sleep(BEEPDELAYTIME);
		PS_RF_Beep(1);
		return READ_CARD_ERR;
		
	}
	
	public int PN_RF_CPU_APDU(int []inApduData,int inApduDataLen,int []outApduData,int inTimeOut)
	{
		/*int []tmpBuf=new int [1024];
		int tmpTFI[]={0xD4};
		int tmpData[]={0x40,0x01};// 01 表示读一张卡 00表示106kbps
		int tmpBufLen=0;
		int buildLen=0;
		int iRet;
		
		//加入APDU数据
		Arrays.fill(tmpBuf, 0);
		System.arraycopy(tmpData, 0, tmpBuf, tmpBufLen, 2);
		tmpBufLen=tmpBufLen+2;
		System.arraycopy(inApduData, 0, tmpBuf, tmpBufLen, inApduDataLen);
		tmpBufLen=tmpBufLen+inApduDataLen;
		
		//打包
		buildLen=buildPackage(tmpTFI,1,tmpBuf,tmpBufLen);
		iRet = mUSBOp.sendDatarRfid(PACKET, buildLen);
		if (iRet < 0)
			return READ_ERR;
		iRet=resolveConfigData(tmpBuf);
		if(iRet<0)
			return iRet;
		System.arraycopy(tmpBuf, 0, outApduData, 0, iRet);
		return iRet;*/
	    int len;
	    int[]uiRecLen=new int[1];
	    //okcnt = 0;
	    //testcnd = 10;
	    int[] tmpBuf=new int[32];
	    if(g_ask_block_flag == 0) {
	        return READ_FORMAT_ERR;
	    }//试试
		if(inApduDataLen>32) //当APDU指令超过32位时
			return READ_FORMAT_ERR;
		//Arrays.fill(uiRecLen, 0);
		//memcpy(sendBuffer,inApduData,inApduDataLen);
		System.arraycopy(inApduData, 0, sendBuffer, 0, inApduDataLen);
		len=inApduDataLen;
		if(Rfid_exchange(sendBuffer, len, recvBuffer, uiRecLen) == MI_OK) {
		//if(Rfid_exchange(sendBuffer, len, outApduData,outApduDataLen) == MI_OK) {

			//tmpBuf[0]=g_ret;
			Arrays.fill(tmpBuf, 0);
			
			//memset(tmpBuf,0x00,sizeof(tmpBuf));
			//memcpy(tmpBuf,recvBuffer,uiRecLen);
			//memcpy(tmpBuf,recvBuffer,uiRecLen);
			System.arraycopy(recvBuffer, 0, tmpBuf, 0, uiRecLen[0]);
			//memset(outApduData,0x00,sizeof(outApduData));
			Arrays.fill(outApduData, 0);
			//memcpy(outApduData,recvBuffer,uiRecLen[0]);
			System.arraycopy(recvBuffer, 0, outApduData, 0, uiRecLen[0]);
			//outApduDataLen[0]=uiRecLen[0];
			//TRACE_LOG("Rfid_exchange SUC");	
			//return SUCCESS;
			return uiRecLen[0];
	        //okcnt ++;
	        //printf("block_exchange successful \r\n");
	        //continue;
	    } else {
	        //printf("block_exchange fail %d times \r\n", testcnd);
	        //break;
			//TRACE_LOG("Rfid_exchange ERR");	
			return READ_ERR;
	    }
		
		
	}
	//int32_t WINAPI RFID_CPU_APDU(int8_t * inApduData,int32_t inApduDataLen,int8_t* outApduData,int32_t *outApduDataLen,int32_t inTimeOut)
	/*public int PN_RF_Open(String mDevice)
	{
		try{
			mSerialPort = new SerialPort(new File(mDevice), 115200, 0);
		}catch (IOException e) {
			Log.d(TAG, "Open Error!");
			e.printStackTrace();	
		}
		
		return SUCCESS;	
	}*/
	public int PN_RF_14443B_Search(int inTimeOut)
	{
		long startTime;
		startTime=System.currentTimeMillis();
		while(true)
		{
		
			
			Rfid_Init();
			//pcd_init();//初始化pcd寄存器
			pcd_default_info();
			//TRACE_LOG("RFID_M1_Search 1");
			nfc_demo();//这里源码有个循环
			//TRACE_LOG("RFID_M1_Search 2");
			if(g_tag_info.uid_length>0)
			{
				PS_RF_Beep(0);
				if(MY_CARD_TYPE!='B')//如果实际操作卡片和非M1卡报卡片类型不匹配错误
					return CARD_OF_OTHER_TYPE;
				//outCardNoLen[0]=g_tag_info.uid_length;
				//memcpy(outCardNo,g_tag_info.serial_num,g_tag_info.uid_length);
				//System.arraycopy(g_tag_info.serial_num, 0, outCardNoData, 0, g_tag_info.uid_length); //方向要反过来
				//return SUCCESS;
				return 0;
			}
			if((System.currentTimeMillis()-startTime)/1000>=5)
				break;
			SystemClock.sleep(10);
			//else
			//	break;
			//else
			//	return READ_CARD_ERR;
			//tEnd = clock();
			//if( ((tEnd - tStart) / CLK_TCK) > inTimeOut ) 
			 // 	break;
			//PeekMessage(&Msg, 0, 0, 0, PM_REMOVE); 
			//TranslateMessage(&Msg);
			//DispatchMessage(&Msg);
		}
		PS_RF_Beep(0);
		SystemClock.sleep(BEEPDELAYTIME);
		PS_RF_Beep(1);
		return READ_CARD_ERR;
		
	}
	
	public int PN_RF_14443B_APDU(int[] inApduData,int inApduDataLen,int[] outApduData,int inTimeOut)
	{
		
	    int len;
	    //int uiRecLen;
	    int[]uiRecLen=new int[1];
	    //okcnt = 0;
	    //testcnd = 10;
	    int[] tmpBuf=new int[32];
	    int iret;
	   /* if(g_ask_block_flag == 0) {
	        return READ_FORMAT_ERR;
	    }*/
		if(inApduDataLen>32) //当APDU指令超过32位时
			return READ_FORMAT_ERR;
		//memcpy(sendBuffer,inApduData,inApduDataLen);
		System.arraycopy(inApduData, 0, sendBuffer, 0, inApduDataLen);
		len=inApduDataLen;
		iret=Rfid_exchange(sendBuffer, len, recvBuffer, uiRecLen);
		if(iret==MI_OK){
		//if(Rfid_exchange(sendBuffer, len, recvBuffer, uiRecLen) == MI_OK) {
		//if(Rfid_exchange(sendBuffer, len, outApduData,outApduDataLen) == MI_OK) {

			//tmpBuf[0]=g_ret;
			//memset(tmpBuf,0x00,sizeof(tmpBuf));
			Arrays.fill(tmpBuf, 0);
			//memcpy(tmpBuf,recvBuffer,uiRecLen);
			System.arraycopy(recvBuffer, 0, tmpBuf, 0, uiRecLen[0]);
			//memset(outApduData,0x00,sizeof(outApduData));
			Arrays.fill(outApduData, 0);
			//memcpy(outApduData,recvBuffer,uiRecLen);
			System.arraycopy(recvBuffer, 0, outApduData, 0, uiRecLen[0]);
			//outApduDataLen[0]=uiRecLen[0];
			//TRACE_LOG("Rfid_exchange SUC");	
			//return SUCCESS;
			return uiRecLen[0];
	        //okcnt ++;
	        //printf("block_exchange successful \r\n");
	        //continue;
	    } else {
	        //printf("block_exchange fail %d times \r\n", testcnd);
	        //break;
			//TRACE_LOG("Rfid_exchange ERR");	
	    	return iret;
			//return READ_ERR;
	    }
	}
	public int PN_RF_F_Search(int inTimeOut)
	{
		long startTime;
		startTime=System.currentTimeMillis();
		while(true)
		{
		
			
			Rfid_Init();
			//pcd_init();//初始化pcd寄存器
			pcd_default_info();
			//TRACE_LOG("RFID_M1_Search 1");
			nfc_demo();//这里源码有个循环
			//TRACE_LOG("RFID_M1_Search 2");
			if(g_tag_info.uid_length>0)
			{
				PS_RF_Beep(0);
				if(MY_CARD_TYPE!='F')//如果实际操作卡片和非M1卡报卡片类型不匹配错误
					return CARD_OF_OTHER_TYPE;
				//outCardNoLen[0]=g_tag_info.uid_length;
				//memcpy(outCardNo,g_tag_info.serial_num,g_tag_info.uid_length);
				//System.arraycopy(g_tag_info.serial_num, 0, outCardNoData, 0, g_tag_info.uid_length); //方向要反过来
				//return SUCCESS;
				return 0;
			}
			if((System.currentTimeMillis()-startTime)/1000>=5)
				break;
			SystemClock.sleep(10);
			//else
			//	return READ_CARD_ERR;
			//tEnd = clock();
			//if( ((tEnd - tStart) / CLK_TCK) > inTimeOut ) 
			 // 	break;
			//PeekMessage(&Msg, 0, 0, 0, PM_REMOVE); 
			//TranslateMessage(&Msg);
			//DispatchMessage(&Msg);
		}
		PS_RF_Beep(0);
		SystemClock.sleep(BEEPDELAYTIME);
		PS_RF_Beep(1);
		return READ_CARD_ERR;
		
	}
	public int PN_RF_F_Read_Block(int inBlock, int[] outCardData,int[] outCardDataLen)
	{
		
		int[] pbuf=new int[256];
	    int tmplen=0;
		/*if(g_ask_block_flag == 0) {
	        return READ_FORMAT_ERR;
	    }*////20230724注释掉
		pcd_read_block_f(inBlock, g_tag_info.serial_num, pbuf, tmplen);
		if(transceive_outLen>0)
		{
			//memcpy(outCardData,pbuf,transceive_outLen);
			System.arraycopy(pbuf, 0, outCardData, 0, transceive_outLen); ///这是
			outCardDataLen[0]=transceive_outLen;	
			return SUCCESS;
		}
		else
			return READ_CARD_ERR;
	}
	public int PN_RF_F_Read_Block_EX(int inStartBlock,int inBlockNum, int[] outCardData,int[] outCardDataLen)
	{


		int[] pbuf=new int[256];
	    //int len;
	    int tmplen=0;
		/*if(g_ask_block_flag == 0) {
	        return READ_FORMAT_ERR;
	    }*///20230724注释掉
		pcd_read_n_block_f(inStartBlock,inBlockNum, g_tag_info.serial_num, pbuf, tmplen);
		if(transceive_outLen>0)
		{
			//memcpy(outCardData,pbuf,len);
			System.arraycopy(pbuf, 0, outCardData, 0, transceive_outLen);
			outCardDataLen[0]=transceive_outLen;	
			return SUCCESS;
		}
		else
			return READ_CARD_ERR;
	}
	public int PN_RF_V_Search(int inTimeOut)
	{
		long startTime;
		startTime=System.currentTimeMillis();
		while(true)
		{
		
			
			Rfid_Init();
			//pcd_init();//初始化pcd寄存器
			pcd_default_info();
			//TRACE_LOG("RFID_M1_Search 1");
			nfc_demo();//这里源码有个循环
			//TRACE_LOG("RFID_M1_Search 2");
			if(g_tag_info.uid_length>0)
			{
				PS_RF_Beep(0);
				if(MY_CARD_TYPE!='V')//如果实际操作卡片和非M1卡报卡片类型不匹配错误
					return CARD_OF_OTHER_TYPE;
				//outCardNoLen[0]=g_tag_info.uid_length;
				//memcpy(outCardNo,g_tag_info.serial_num,g_tag_info.uid_length);
				//System.arraycopy(g_tag_info.serial_num, 0, outCardNoData, 0, g_tag_info.uid_length); //方向要反过来
				//return SUCCESS;
				return 0;
			}
			if((System.currentTimeMillis()-startTime)/1000>=5)
				break;
			SystemClock.sleep(10);
			//else
			//	break;
			//else
			//	return READ_CARD_ERR;
			//tEnd = clock();
			//if( ((tEnd - tStart) / CLK_TCK) > inTimeOut ) 
			 // 	break;
			//PeekMessage(&Msg, 0, 0, 0, PM_REMOVE); 
			//TranslateMessage(&Msg);
			//DispatchMessage(&Msg);
		}
		PS_RF_Beep(0);
		SystemClock.sleep(BEEPDELAYTIME);
		PS_RF_Beep(1);
		return READ_CARD_ERR;	
	}
	
	public int PN_RF_V_Read_Block(int inBlock, int[] outCardData,int[] outCardDataLen)
	{
		
		int[] pbuf=new int[256];
	    //uint8_t len;
	    int tmplen=0;
		/*if(g_ask_block_flag == 0) {
	        return READ_FORMAT_ERR;
	    }*///20230724注释掉
		pcd_read_block_v(inBlock, g_tag_info.serial_num, pbuf, tmplen);
		if(transceive_outLen>0)
		{
			//memcpy(outCardData,pbuf,len);
			System.arraycopy(pbuf, 0, outCardData, 0, transceive_outLen);
			//outCardDataLen[0]=len;
			outCardDataLen[0]=transceive_outLen;	
			return SUCCESS;
		}
		else
			return READ_CARD_ERR;
	}
	
	public int PN_RF_V_Read_Block_EX(int inStartBlock,int inBlockNum, int[] outCardData,int[] outCardDataLen)
	{


		int[] pbuf=new int[256];
		int tmplen=0;
	    //uint8_t len;
		/*if(g_ask_block_flag == 0) {
	        return READ_FORMAT_ERR;
	    }*///20230724注释掉
		pcd_read_n_block_v(inStartBlock,inBlockNum, g_tag_info.serial_num, pbuf, tmplen);
		if(transceive_outLen>0)
		{
			//memcpy(outCardData,pbuf,len);
			System.arraycopy(pbuf, 0, outCardData, 0, transceive_outLen);
			outCardDataLen[0]=transceive_outLen;	
			//outCardDataLen[0]=len;	
			return SUCCESS;
		}
		else
			return READ_CARD_ERR;
	}
	
	public int PN_RF_V_Read_Block_lock_state(int inStartBlock,int inBlockNum, int[] outCardData,int[] outCardDataLen)
	{

		//pcd_read_n_block_lock_state_v(0, 27, uid, pbuf, &len); //一共28块
		int[] pbuf=new int[256];
		int tmplen=0;
		/*if(g_ask_block_flag == 0) {
	        return READ_FORMAT_ERR;
	    }*///20230724注释掉
		pcd_read_n_block_lock_state_v(inStartBlock,inBlockNum, g_tag_info.serial_num, pbuf, tmplen);
		if(transceive_outLen>0)
		{
			//memcpy(outCardData,pbuf,len);
			//outCardDataLen[0]=len;
			System.arraycopy(pbuf, 0, outCardData, 0, transceive_outLen);
			outCardDataLen[0]=transceive_outLen;	
			return SUCCESS;
		}
		else
			return READ_CARD_ERR;
	}
	
	public int PN_RF_V_Read_lock_state_block(int inStartBlock,int inBlockNum, int[] outCardData,int[] outCardDataLen)
	{

		//pcd_read_n_lock_state_block_v(0, 27, uid, pbuf, &len); //一共28块
		int[] pbuf=new int[256];
		int tmplen=0;
		/*if(g_ask_block_flag == 0) {
	        return READ_FORMAT_ERR;
	    }*///20230724注释掉
		pcd_read_n_lock_state_block_v(inStartBlock,inBlockNum, g_tag_info.serial_num, pbuf, tmplen);
		if(transceive_outLen>0)
		{
			//memcpy(outCardData,pbuf,len);
			//outCardDataLen[0]=len;
			System.arraycopy(pbuf, 0, outCardData, 0, transceive_outLen);
			outCardDataLen[0]=transceive_outLen;	
			return SUCCESS;
		}
		else
			return READ_CARD_ERR;
	}


}
