using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MifareOneSample
{
     
    //MasterRD.dll类方法
    public class masterRD
    {
        private const int LIB_SUCCESS = 0;        //success
        private const int LIB_PARAM_ERR = -1;      //parameter error
        private const int LIB_OS_ERR = -2;         //os error
        private const int LIB_NETWORK_ERR = -3;    //network error
        private const int LIB_TIMEOUT = -4;        //operator timeout
        private const int LIB_FAILED = -5;        //failure


        /******** FUNCTION：Get Dll Version  2Byte *******************/
        // PARAMETERS: nothing
        //  RETURNED : success - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "lib_ver",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int lib_ver(
            uint Ver
            );

        /******** FUNCTION：DES Encrypt  *************************/
        //  PARAMETERS: szOut - DES resulted data
        //               szIn -   encrypted data
        //              inlen -    encrypted data length 
        //                key -    key
        //             keylen:    key length
        //    RETURNED : SUCCESS -0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "des_encrypt",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int des_encrypt(
            byte szOut,
            byte szIn,
            uint inlen,
            byte key,
            uint keylen
            );
        /******** FUNCTION：  DES Decrypt *************************/
        //  PARAMETERS：szOut:  DECRYPT resulted data
        //        szIn:   decrypt data
        //        inlen:  decypt data length
        //        key:    key
        //        keylen: key length
        //  RETURNED:  SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "des_decrypt",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int des_decrypt(
            byte szOut,
            byte szIn,
            uint inlen,
            byte key,
            uint keylen
            );
        /*********************************************************
	    FUNCTION：Inialize device ID 
	    PARAMETERS：icdev - hiByte=icdev(return by rf_init_com)
	                        loByte= devId;
	    RETURNED : SUCCESS - 0

        for example:
		    if you want to set current reader devId to 9 you need to :
		    BYTE devId=9;
		    icdev &= 0xff00;
		    icdev |= devid;
		    int ret= rf_init_device_number(icdev);

	    note:
		    you need to save original icdev(return by rf_init_com),if function 
		    rf_init_device_number execute success, you can use new icdev(contain devId).

        *********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_init_device_number",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_init_device_number(
            ushort icdev
            );
        /********************************************************
	    FUNCTION : get device id;
	    PARAMETERS：icdev- (return value by rf_init_com)
	    RETURNED：SUCCESS - 0
    	
	    for example:
	    USHORT icdev; //(return value by rf_init_com)
	    if(rf_get_device_number(&icdev) == 0)
	    {
		    BYTE devId= (icdev & 0x00FF);
	    }

        *********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_get_device_number",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_get_device_number(
            ref ushort icdev
            );
        /******** FUNCTION：Get hardware version , 2 bytes ***********/
        //  PARAMETERS：icdev -   device address
        //        Version - version information
        //  RETURNED : SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_get_model",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_get_model(
            ushort icdev,
            ref ushort Version
            );
        /******** FUNCTION：Get serial number of hardware,8 bytes ***********/
        //  PARAMETERS：icdev - device address
        //        Snr：  return serial number
        //  RETURNED  : SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_get_snr",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_get_snr(
            ushort icdev,
            ref byte[] Snr
            );
        /******** FUNCTION：SET  sam card baud rate*************/
        //  PARAMETERS：icdev - device address
        //        bound: sam card boud rate , value : 9600、38400
        //  RETURNED：SUCCESS - 0
        //  MEMO：bound=0:9600
        //        bound=1:38400
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_init_sam",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_init_sam(
            ushort icdev,
            byte bound
            );
        /******* FUNCTION：reset sam card ********************************/
        //  PARAMETERS：icdev  - device address
        //        pData -  return reset information
        //        pMsgLg -reset data length
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_sam_rst",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_sam_rst(
            ushort icdev,
            byte[] pData,
            ref byte pMsgLg
            );
        /******** FUNCTION：send COS command to SAM card *********************/
        //  PARAMETERS：icdev -  device address
        //        command - cos command
        //        cmdLen -  cos length
        //        pDat -  return data from card , include SW1、SW2
        //        pMsgLg -  return data length
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_sam_cos",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_sam_cos(
            ushort icdev,
            byte[] command,
            byte cmdLen,
            byte[] pData,
            ref byte Length
            );
        /*******  FUNCTION：cloase or open antenna***************/
        //  PARAMETERS：icdev - device address
        //        model - antenna mode
        //  RETURNED：SUCCESS - 0
        //  Note：model=0 - close antenna
        //        model=1 - open antenna
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_antenna_sta",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_antenna_sta(
            ushort icdev,
            byte mode
            );
        /****** FUNCTION：download Mifare One key from hardware******************/
        //one key = every 6 bytes，0～15 block arrange
        //PARAMETERS：icdev - device address
        //Mode - key type ,value = ‘A’OR ‘B’
        //key - key，96 bytes
        //RETURNED：SUCCESS -0
        /***************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_download_key",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_download_key(
            ushort icdev,
            byte mode,
            byte[] key
            );
        /***** FUNCTION：authenticate Mifare One card*****************/
        //  PARAMETERS：icdev- device address
        //        model：key authentication mode
        //        block：absolute block number that need authenticate
        //        key：  key content，6 byte
        //  RETURNED：SUCCESS -0
        //  Note：model=0x60:authenticate A key
        //        model=0x61:authenticate B key
        /**********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_M1_authentication1",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_authentication1(
            ushort icdev,
            byte mode,
            byte secnr
            );
        /*******  FUNCTION：inialize one block of Mifare one as purse *******/
        //  PARAMETERS：icdev：device address
        //        block：M1 block address
        //        value：inialize value
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_M1_initval",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_initval(
            ushort icdev,
            byte adr,
            long value
            );
        /*******  FUNCTION：read Mifare One purse value **********************/
        //  PARAMETERS：icdev： device address
        //        block： M1 block address
        //        pValue：return value
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_M1_readval",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_readval(
            ushort icdev,
            byte adr,
            ref long value
            );
        /*******  FUNCTION：Mifare One decrement fund **************************/
        //  PARAMETERS：icdev：device address
        //        block：M1 block address
        //        value：decrement value
        //  RETURNED：SUCCESS - 0
        //  Note：if run successfully，returned data will be saved as buffer of card,
        //        and donot modify context of block. if we need save result to card , we need
        //        run rf_M1_restore
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_M1_decrement",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_decrement(
            ushort icdev,
            byte adr,
            long value
            );
        /******** FUNCTION：Mifare One increment value **************************/
        //  PARAMETERS：icdev：device address
        //        block：M1 block address
        //        value：increment value
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_M1_increment",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_increment(
            ushort icdev,
            byte adr,
            long value
            );
        /******** FUNCTION：Save as Mifare One value to other block **********************/
        //  PARAMETERS：icdev：device address
        //        block：M1 block address
        //  RETURNED：SUCCESS - 0
        //  Note：It transfer block context to buffer of card ，and use
        //        rf_M1transfer() to transfer buffer of card to other block
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_M1_restore",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_restore(
            ushort icdev,
            byte adr
            );
        /****** FUNCTION：transfer Mifare One value ***********************/
        //  PARAMETERS：icdev：device address
        //        block：M1 block address
        //  RETURNED：SUCCESS - 0
        //  Note：it only is called after calling increment、decrement and restore 
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_M1_transfer",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_transfer(
            ushort icdev,
            byte adr
            );
        /****** FUNCTION：Request ISO14443 TYPE-A card and reset **********************/
        //  PARAMETERS：icdev：device address
        //              model: request model, 0x26, request IDLE, 0x52, request all(WUPA)
        //              pData：output data, 4 bytes snr + reset info.
        //              pMsgLg: output data length
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_typea_rst",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_typea_rst(
            ushort icdev,
            byte mode,
            byte[] pData,
            ref byte[] pMsgLg
            );
        /******** FUNCTION：send cos command to ISO14443-A card  ********/
        //PARAMETERS：icdev：device address
        //command：cos command
        //MsgLg：length of returned data
        //Date：returned data ，include SW1、SW2
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        /*for example：int status
        unsigned short icdev
        unsigned char* cmd;
        unsigned char pData[MAX_RF_BUFFER];
        unsigned char len;

        status = rf_cos_command(icdev,cmd,sizeof(cmd),pData,&len);
        */
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_cos_command",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_cos_command(
            ushort icdev,
            byte[] command,
            byte cmdLen,
            byte[] pData,
            ref byte[] pMsgLg
            );
        /******** FUNCTION：search ISO14443-B card ************************/
        //PARAMETERS：icdev：device address
        //AFI： use AFI, if AFI = 0, all cards respond in induce area
        //slotmax: max channels number between hardware and rf card
        //MsgLg：length of returned data
        //Date：returned data
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        /*for example：int status
        int icdev
        unsigned char msglg
        unsigned char pDate[MAX_RF_BUFFER];
        status = rf_atqb(icdev,0,1,pDate,&msglg);

        */
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_atqb",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_atqb(
            ushort icdev,
            byte model,
            byte[] pData,
            ref byte[] pMsgLg
            );
        /*********  FUNCTION：activate all cards of searched ISO14443-B cards ************/
        //PARAMETERS：icdev：device address
        //PUPI： flag of card
        //CID： logic address , value 0～14 and less than slotmax 
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_attrib",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_attrib(
            ushort icdev,
            ulong PUPI,
            byte CID
            );
        /*********  FUNCTION：send cos command to ISO14443-B card ******/
        //PARAMETERS：icdev：device address
        //CID： logic address
        //command：cos command
        //MsgLg：length of returned data
        //Date： data of returned data ,include SW1、SW2
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        /*
        for example：int status
        int icdev
        unsigned char *command;
        unsigned char msglg;
        unsigned char pData[MAX_RF_BUFFER];
        status = rf_typeb_cos(icdev,0,command,sizeof(command),pData,&msglg);

        */
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_typeb_cos",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_typeb_cos(
            ushort icdev,
            byte CID,
             byte[] command,
             byte cmdLen,
             byte[] pData,
             ref byte[] pMsgLg
            );
        /*********** FUNCTION：validate AT88RF020 password **************************/
        //PARAMETERS：icdev：device address
        //key： password, 8 bytes
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_at020_check",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_at020_check(
            ushort icdev,
            byte[] key
            );
        //********** FUNCTION：read one page data of AT88RF020 card ************************/
        //PARAMETERS：icdev：device address
        //adr：page address，（0～31）
        //Date：returned data，8 bytes
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        /*
        for example：int status
        int icdev
        unsigned char pData[MAX_RF_BUFFER];
        unsigned char len;
        status = rf_at020_read(icdev,0,pData,&len);

        */
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_at020_read",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_at020_read(
            ushort icdev,
            byte adr,
            byte[] pData,
            ref byte[] pMsgLg
            );
        /*********** FUNCTION：write  page address data of AT88RF020 ************************/
        //PARAMETERS：icdev：device address
        //adr：page address，（0～31）
        //Date：data ，8 bytes
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_at020_write",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_at020_write(
            ushort icdev,
            byte adr,
            byte[] data
            );
        /************** FUNCTION：********************************************/
        //PARAMETERS：icdev：device address
        //date： data，4 bytes
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_at020_lock",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_at020_lock(
            ushort icdev,
            byte[] data
            );
        /********** FUNCTION： ********************/
        //PARAMETERS：icdev：device address
        //date： data, 6 bytes
        //RETURNED：SUCCESS - 0
        /**************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_at020_count",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_at020_count(
            ushort icdev,
            byte[] data
            );
        /********** FUNCTION：let AT88RF020 to enter HALT state ******************/
        //PARAMETERS：icdev：device address
        //RETURNED：SUCCESS - 0
        /*****************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_at020_deselect",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_at020_deselect(
            ushort icdev
            );
        /********** FUNCTION：set light's color ******************/
        //PARAMETERS：icdev：device address
        //      color: 0 ,black out
        //			   1 ,red
        //			   2 ,green
        //			   3 ,yellow
        //RETURNED：SUCCESS - 0
        /*************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_light",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_light(
            ushort icdev,
            byte color
            );
        /********** FUNCTION： ******************/

        //RETURNED：SUCCESS - 0
        /*************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_cl_deselect",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_cl_deselect(
            ushort icdev
            );
        //FUNCTION：lock ST card ********************************/
        //  PARAMETERS：icdev： device address
        //        Chip_ID：ID number
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_st_select",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_st_select(
            ushort icdev,
            byte[] pChip_ID
            );
        /******** FUNCTION：let ST card to enter DESACTIVED state ***************/
        //  PARAMETERS：icdev：  device address
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_st_completion",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_st_completion(
            ushort icdev
            );
        /*******  FUNCTION：read one block data of SR176 *************************/
        //  PARAMETERS：icdev： device address
        //        block： block address
        //        pData： returned data
        //        pLen:   length of pData
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_sr176_readblock",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_sr176_readblock(
            ushort icdev,
            byte block,
            byte[] pData,
            byte pLen
            );
        
        /*******  FUNCTION：write one block data of SR176 *************************/
        //  PARAMETERS：icdev：device address
        //        block：block address
        //        data： data ，2 bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
         [DllImport("MasterRD.dll",
        EntryPoint = "rf_sr176_writeblock",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_sr176_writeblock(
            ushort icdev,
            byte block,
            byte[] data
            );
        /*******  FUNCTION：lock SR176 block ****************************/
        //  PARAMETERS：icdev： device address
        //        lockreg:LOCKREG
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_sr176_protectblock",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_sr176_protectblock(
            ushort icdev,
            byte lockreg
            );
        /*******  FUNCTION：read one block data of SRIX4K ************************/
        //  PARAMETERS：icdev： device address
        //        block： block address
        //        pData:  returned data
        //        pLen:	  length of pData
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_srix4k_readblock",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_srix4k_readblock(
            ushort icdev,
            byte block,
            byte[] pData,
            ref byte pLen
            );
        /*******  FUNCTION：write one block dat of SRIX4K ************************/
        //  PARAMETERS：icdev：device address
        //        block：block address
        //        pData: data, 4 bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_srix4k_writeblock",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_srix4k_writeblock(
            ushort icdev,
            byte block,
            byte[] pData
            );
        /******** FUNCTION：authenticate SRIX4K *************************/
        //  PARAMETERS：icdev：device address
        //        pData: RND，6 bytes
        //        pSIG:	 SIG
        //        pLen:	 length of SIG
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_srix4k_authenticate",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_srix4k_authenticate(
            ushort icdev,
            byte[] pRND,
            byte[] pSIG,
            ref byte pLen
            );
        /******** FUNCTION：get UID of SRIX4K **************************/
        //  PARAMETERS：icdev：device address
        //        pUID:	 UID
        //        pLen:	 length of UID
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_srix4k_getuid",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_srix4k_getuid(
            ushort icdev,
            byte[] Puid,
            ref byte pLen
            );
        //FUNCTION：lock SRIX4K ****************************/
        //  PARAMETERS：icdev：device address
        // lockreg:	LOCKREG
        /**********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_srix4k_writelockreg",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_srix4k_writelockreg(
            ushort icdev,
            byte lockreg
            );
        /****** FUNCTION：lock ultra light **********************/
        //  PARAMETERS：icdev：device address
        //        pSnr： serial number
                  //pLene：length of pSnr
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_ul_select",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_ul_select(
            ushort icdev,
            byte[] pSnr,
            ref byte pLen
            );
        
        /******** FUNCTION：write one block data of ultra light ************/
        //  PARAMETERS：icdev： device address
        //        page：  page address of ultra light（0～0x0f）
        //        data：  data ，4bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/

        [DllImport("MasterRD.dll",
        EntryPoint = "rf_ul_write",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_ul_write(
            ushort icdev,
            byte page,
            byte[] data
            );

        /******** FUNCTION：ISO15693_Inventory ***********************/
        //  PARAMETERS：icdev：device address
        //        Pdata: return data，DSFID(1 byte)+ UID(8 bytes)
        //        pLen:	 Pdata's length
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Inventory",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Inventory(
            ushort icdev,
            byte[] pData,
            ref byte pLen
            );


        /******** FUNCTION：ISO15693_Stay_Quiet **********************/
        //  PARAMETERS：icdev：device address
        //        UID:	 UID 8 bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Stay_Quiet",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Stay_Quiet(
            ushort icdev,
            byte[] UID
            );

        /******** FUNCTION：ISO15693_Select **************************/
        //  PARAMETERS：icdev：device address
        //        UID:	 UID 8 bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Select",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Select(
            ushort icdev,
            byte[] UID
            );


        /******** FUNCTION：ISO15693_Reset_To_Ready ******************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Reset_To_Ready",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Reset_To_Ready(
            ushort icdev,
            byte model,
            byte[] UID
            );

        /******** FUNCTION：ISO15693_Read ***************************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //        block: block number
        //        number: read block numbers，< 0x10
        //        Pdata: return data
        //        pLen:  Pdata's length
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Read",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Read(
            ushort icdev,
            byte model,
            byte[] UID,
            byte block,
            byte number,
            byte[] Pdata,
            ref byte pLen
            );

        /******** FUNCTION：ISO15693_Write ***************************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //        block: block number
        //        data:  writed data，4 bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Write",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Write(
            ushort icdev,
            byte model,
            byte[] UID,
            byte block,
            byte[] data
            );			 
        /******** FUNCTION：ISO15693_Lock_Block **********************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //        block: block number
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Lock_Block",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Lock_Block(
            ushort icdev,
            byte model,
            byte[] UID,
            byte block
            );			 

        /******** FUNCTION：ISO15693_Write_AFI ***********************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //        AFI:   writed AFI
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Write_AFI",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Write_AFI(
            ushort icdev,
            byte model,
            byte[] UID,
            byte AFI
            );		

        /******** FUNCTION：ISO15693_Lock_AFI ************************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Lock_AFI",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Lock_AFI(
            ushort icdev,
            byte model,
            byte[] UID
            );	

        /******** FUNCTION：ISO15693_Write_DSFID *********************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //        DSFID: writed DSFID
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Write_DSFID",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Write_DSFID(
            ushort icdev,
            byte model,
            byte[] UID,
            byte DSFID
            );	

        /******** FUNCTION：ISO15693_Lock_DSFID **********************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Lock_DSFID",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Lock_DSFID(
            ushort icdev,
            byte model,
            byte[] UID
            );	

        /******** FUNCTION：ISO15693_Get_System_Information **********/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //        Pdata: return data
        //        pLen:  Pdata's length
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Get_System_Information",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Get_System_Information(
            ushort icdev,
            byte model,
            byte[] UID,
            byte[] Pdata,
            ref byte[] pLen
            );						       

        /******** FUNCTION：ISO15693_Get_Block_Security **************/
        //  PARAMETERS：icdev：device address
        //        model: bit0=Select_flags,bit1=Addres_flags
        //        UID:	 UID 8 bytes
        //        block: block number
        //        number:read block numbers，< 0x40
        //        Pdata: return data
        //        pLen:  Pdata's length
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "ISO15693_Get_Block_Security",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int ISO15693_Get_Block_Security(
            ushort icdev,
            byte model,
            byte[] UID,
            byte block,
            byte number,
            byte[] Pdata,
            ref byte[] pLen
            );						       


        /******** FUNCTION：Display message on MR600 **************/
        //  PARAMETERS：
        //        icdev：device address
        //        cDecodeModel: decode mode
        //        cDpPosition: when cDecodeModel = 1, the point position, could be 0 to 7
        //        cDisplayData: display data
        //  return:
        //        0: success
        //        1: failure
        //  usage:
        //        cDecodeModel=0: the display data are seven segments data, need not to decode with reader
        //        cDecodeModel=1: the display data are 0 to 9 in hex mode, reader will decode the data and display it, it the value is 10 (hex: 0x0A) the display bit will be blank
        //        cDisplayData: 8 bytes display data
        //        when cDecodeModel = 0, the decode table below:
        //        display	code
        //        '0'		0x7E
        //        '1'		0x30
        //        '2'		0x6D
        //        '3'		0x79
        //        '4'		0x33
        //        '5'		0x5B
        //        '6'		0x5F
        //        '7'		0x70
        //        '8'		0x7F
        //        '9'		0x7B
        /********************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_display",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_display(
            ushort icdev,
            byte cDecodeModel,
            byte cDpPosition,
            byte[] DisplayData
            );	
        /******** FUNCTION：write data to FLASH in MR600 **************/
        //  PARAMETERS：
        //        icdev：device address
        //        iBlock: block number, 256 bytes per block, total 2048 blocks, 0x0000 - 0x07FF
        //        cStartAddress: wirte start addr. in block, 0 - 255
        //        cBytes: bytes to write
        //        WriteData: data to write
        //  return:
        //        0: success
        //        1: failure
        //  notice:
        //        max. 224 bytes per command
        //        cStartAddress + cBytes <= 256
        /********************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_flash_write",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_flash_write(
            ushort icdev,
            byte iBlock,
            byte cStartAddress,
            byte cBytes,
            byte[] WriteData
            );	

        /******** FUNCTION：read data from FLASH in MR600 **************/
        //  PARAMETERS：
        //        icdev：device address
        //        iBlock: block number, 256 bytes per block, total 2048 blocks, 0x0000 - 0x07FF
        //        cStartAddress: wirte start addr. in block, 0 - 255
        //        cBytes: bytes to write
        //        pData: data read
        //  return:
        //        0: success
        //        1: failure
        //  notice:
        //        max. 224 bytes per command
        //        cStartAddress + cBytes <= 256
        /********************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_flash_read",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_flash_read(
            ushort icdev,
            byte iBlock,
            byte cStartAddress,
            byte cBytes,
            byte[] pData
            );	

        /****** FUNCTION：find a ISO14443 TYPE A card, support 3 level anticollision *****/
        //  PARAMETERS：icdev: device address
        //        	cModel: request mode 0x52 or 0x26
        //        	pLen: returned data length
        //       	pUidLength: the UID length
        //       	pData: returned data, they are: 2 bytes ATQA + 1 bytes SAK + 4 or 7 or 10 bytes UID
        //  Note：mode=0x26: REQA, search card that not halted
        //        mode=0x52: WUPA, search all card 
        /********************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_search_type_a",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_search_type_a(
            ushort icdev,
            byte cModel,
            byte[] pData,
            byte pLen,
            byte pUidLength
            );	

        /****** FUNCTION：ISO14443-4 TYPE A card reest **********************/
        //  PARAMETERS：icdev: device address
        //        	cModel: request mode 0x52 or 0x26
        //        	pLen: returned data length
        //       	pData: ATS, card reset infomation
        /********************************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_iso14443_4_type_a_reset",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_iso14443_4_type_a_reset(
            ushort icdev,
            byte[] pData,
            ref byte pLen
            );	
        /*******  FUNCTION：set hardware as no-contact mode *************/
        //              ISO14443 TYPE A OR ISO14443 TYPE B
        //  PARAMETERS：icdev - device address
        //        type:  work mode of hardware
        //  RETURNED：SUCCESS - 0
        //  Note：type='A':set TYPE_A mode
        //        type='B':set TYPE_B mode
        //        type='r':set AT88RF020 mode 
        //        type='1':set 15693 mode
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_init_type",
             CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_init_type(
            ushort icDev,
            char type
            );
        /******** FUNCTION：search ISO14443-3 TYPE_A card *******************/
        //  PARAMETERS：icdev  - device address
        //        model - search card mode 
        //  RETURNED：SUCCESS - 0
        //  Note：mode=0x26 -  search card into sleep state 
        //        mode=0x52  - search all card 
        /*********************************************************/
        [DllImport("MasterRD.dll",
              EntryPoint = "rf_request",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_request(
            ushort icDev,
            byte mode,
            ref ushort TagType
            );
        /********* FUNCTION：ISO14443-3 TYPE_A card defend collide **************/
        //  PARAMETERS：icdev - device address
        //        bcnt：   serial number bytes of card ,value :4、7、10，Mifare = 4
        //        pSnr：   return serial number
        //        pRLength:return length of pSnr
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
             EntryPoint = "rf_anticoll",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_anticoll(
            ushort icDev,
            int bcnt,
             byte[] Snr,
              ref byte Len
            );
        /******** FUNCTION：Locked ISO14443-3 TYPE_A card *************/
        //  PARAMETERS：icdev- device address
        //        pSnr： serial number of card
        //        srcLen:length of pSnr，MifareOne 's value = 4
        //        Size： return card's size
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
            EntryPoint = "rf_select",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_select(
            ushort icDev,
            byte[] Snr,
            byte len,
            ref  byte size
            );
        /**********************************************************
        function:  init reader
        parameters: port serialPort, value(1~256)
                    szAttr: attribute for reader
        return:   if SUCCESS return icdev; other <0
         *********************************************************/
        [DllImport("MasterRD.dll",
           EntryPoint = "rf_init_com",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_init_com(
            ushort port,
            string szAttr
            );
        /******** FUNCTION：beep *************************************/
        //  PARAMETERS：icdev - device address
        //        msec： beep time , unit : 10 ms
        //  RETURNED : SUCCESS - 0 
        /*********************************************************/
        [DllImport("MasterRD.dll",
          EntryPoint = "rf_beep",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_beep(
             ushort icDev,
            byte time
            );
        /***** FUNCTION：authenticate Mifare One card*****************/
        //  PARAMETERS：icdev- device address
        //        model：key authentication mode
        //        block：absolute block number that need authenticate
        //        key：  key content，6 byte
        //  RETURNED：SUCCESS -0
        //  Note：model=0x60:authenticate A key
        //        model=0x61:authenticate B key
        /**********************************************************/
        [DllImport("MasterRD.dll",
         EntryPoint = "rf_M1_authentication2",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_authentication2(
             ushort icDev,
            byte mode,
            byte secnr,
            byte[] key
            );
        /*******  FUNCTION：read one block data at Mifare one************/
        //  PARAMETERS：icdev：device address
        //        adr：M1 card absolute block address
        //        pData： read data，16 byte
        //        pLen:length of pData
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
        EntryPoint = "rf_M1_read",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_read(
             ushort icDev,
             byte adr,
             byte[] pData,
             ref byte pLen
            );
        /********** FUNCTION：close port ************************************/
        [DllImport("MasterRD.dll",
       EntryPoint = "rf_ClosePort",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_ClosePort(
             ushort icDev
            );
        /*******  FUNCTION：write one block data at Mifare one ************/
        //  PARAMETERS：icdev：device address
        //        secnr：M1 absolute block address
        //        pData： write data，16 byte
        //  RETURNED：SUCCESS - 0
        /*********************************************************/
        [DllImport("MasterRD.dll",
      EntryPoint = "rf_M1_write",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_M1_write(
              ushort icDev,
            byte secnr,
            byte[] pData
            );
        /******* FUNCTION：let invalidated ISO14443-3 TYPE_A card to sleep state */
        //  PARAMETERS：icdev - device address
        //  RETURNED：SUCCESS - 0
        /**********************************************************/
        [DllImport("MasterRD.dll",
     EntryPoint = "rf_halt",
                    CharSet = CharSet.Ansi,
                    CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_halt(
              ushort icDev
            );
    }
}
