Exemplo n.º 1
0
        public string ReadCard(out string strCardID, out double dCurCharge, out int iCurIg)
        {
            strCardID  = "";
            dCurCharge = 0;
            iCurIg     = 0;
            //初始化
            icdev = RFDef.rf_init(port, baud);
            if (icdev < 0)
            {
                quit();
                return(ConstMsg.RFINITERR);
            }

            //寻卡
            UInt16 tagtype = 0;
            byte   size    = 0;
            uint   snr     = 0;

            RFDef.rf_reset(icdev, 10);
            st = RFDef.rf_request(icdev, 1, out tagtype);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFREQUESTERR);
            }

            st = RFDef.rf_anticoll(icdev, 0, out snr);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFANTICOLLERR);
            }

            st = RFDef.rf_select(icdev, snr, out size);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFSELECTERR);
            }

            //验证B
            byte[] bkey1 = new byte[16];
            byte[] bkey2 = new byte[6];

            string bkey = "123456789012";

            bkey1 = Encoding.ASCII.GetBytes(bkey);
            RFDef.a_hex(bkey1, bkey2, 12);
            st = RFDef.rf_load_key(icdev, 4, sec, bkey2);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFLOADKEY_B_ERR);
            }
            st = RFDef.rf_authentication(icdev, 4, sec);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFAUTHENTICATION_B_ERR);
            }

            //读卡号
            int i = 0;

            byte[] databuff = new byte[16];
            byte[] buff     = new byte[32];

            for (i = 0; i < 16; i++)
            {
                databuff[i] = 0;
            }
            for (i = 0; i < 32; i++)
            {
                buff[i] = 0;
            }
            //卡号
            st = RFDef.rf_read(icdev, sec * 4, databuff);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFREADERR);
            }
            else
            {
                RFDef.hex_a(databuff, buff, 16);
                strCardID = System.Text.Encoding.ASCII.GetString(buff);
                strCardID = strCardID.Substring(0, 5);
            }

            //蜂鸣
            st = RFDef.rf_beep(icdev, 2);

            quit();
            return(ConstMsg.RFOK);
        }
Exemplo n.º 2
0
        public string ConsWriteCard(double dCurCharge, double dCurChargeBak, int iCurIg)
        {
            //初始化
            icdev = RFDef.rf_init(port, baud);
            if (icdev < 0)
            {
                quit();
                return(ConstMsg.RFINITERR);
            }

            //寻卡
            UInt16 tagtype = 0;
            byte   size    = 0;
            uint   snr     = 0;

            RFDef.rf_reset(icdev, 10);
            st = RFDef.rf_request(icdev, 0, out tagtype);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFREQUESTERR);
            }

            st = RFDef.rf_anticoll(icdev, 0, out snr);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFANTICOLLERR);
            }

            st = RFDef.rf_select(icdev, snr, out size);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFSELECTERR);
            }

            //验证B
            byte[] bkey1 = new byte[16];
            byte[] bkey2 = new byte[6];

            string bkey = "123456789012";

            bkey1 = Encoding.ASCII.GetBytes(bkey);
            RFDef.a_hex(bkey1, bkey2, 12);
            st = RFDef.rf_load_key(icdev, 4, sec, bkey2);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFLOADKEY_B_ERR);
            }
            st = RFDef.rf_authentication(icdev, 4, sec);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFAUTHENTICATION_A_ERR);
            }

            //读余额,为返写时存下数据
            int i = 0;

            byte[] databuffcharge = new byte[16];
            for (i = 0; i < 16; i++)
            {
                databuffcharge[i] = 0;
            }
            st = RFDef.rf_read(icdev, sec * 5, databuffcharge);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFREADERR);
            }

            //准备余额至databuff
            byte[] databuff = new byte[16];
            byte[] buff     = new byte[32];
            int    lendata  = 0;

            string strzero30 = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
            string data      = dCurCharge.ToString("0.00");
            int    dotindex  = data.IndexOf(".", 0);

            if (dotindex == -1)
            {
                lendata = data.Length;
                data    = data + strzero30.Substring(0, 30 - lendata) + "00";
            }
            else
            {
                string datalar  = data.Substring(0, dotindex);
                string datatiny = data.Substring(dotindex + 1, 2);
                lendata = datalar.Length;
                data    = datalar + strzero30.Substring(0, 30 - lendata) + datatiny;
            }
            buff = Encoding.ASCII.GetBytes(data);
            RFDef.a_hex(buff, databuff, 32);

            //写余额
            st = RFDef.rf_write(icdev, sec * 5, databuff);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFWRITEERR);
            }
            else
            {
                //反读余额
                for (i = 0; i < 16; i++)
                {
                    databuff[i] = 0;
                }
                st = RFDef.rf_read(icdev, sec * 5, databuff);
                if (st != 0)
                {
                    //反写余额
                    st = RFDef.rf_write(icdev, sec * 5, databuffcharge);
                    if (st != 0)
                    {
                        //蜂鸣
                        st = RFDef.rf_beep(icdev, 2);
                        quit();
                        return("CMT|反读反写出错,trans提交。correct data: C:" + dCurCharge.ToString() + "I:" + iCurIg.ToString());
                    }
                    else
                    {
                        quit();
                        return("ROL|反读反写成功,trans回滚。");
                    }
                }
                else
                {
                    double dChargeInv = 0;
                    RFDef.hex_a(databuff, buff, 16);
                    string strCharge = System.Text.Encoding.ASCII.GetString(buff);
                    int    fcIndex   = strCharge.IndexOf("F", 0);
                    if (fcIndex == -1)
                    {
                        dChargeInv = -1;
                    }
                    else
                    {
                        dChargeInv = double.Parse(strCharge.Substring(0, fcIndex) + "." + strCharge.Substring(30, 2));
                    }
                    bool invflag = true;
                    if (dChargeInv == -1)
                    {
                        invflag = false;
                    }
                    else
                    {
                        if (dCurCharge != dChargeInv || dCurCharge != dCurChargeBak || dCurChargeBak != dChargeInv)
                        {
                            invflag = false;
                        }
                    }
                    if (!invflag)
                    {
                        //反写余额
                        st = RFDef.rf_write(icdev, sec * 5, databuffcharge);
                        if (st != 0)
                        {
                            //蜂鸣
                            st = RFDef.rf_beep(icdev, 2);
                            quit();
                            return("CMT|校验错误,反写出错,trans提交。correct data: C:" + dCurCharge.ToString() + "I:" + iCurIg.ToString());
                        }
                        else
                        {
                            quit();
                            return("ROL|校验错误,反写成功,trans回滚。");
                        }
                    }
                    else
                    {
                        quit();
                        return(ConstMsg.RFOK);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public string ReadCard(out string strCardID)        //,out double dCurCharge,out int iCurIg)
        {
#if DEBUG
            strCardID = "000174";
            return(ConstMsg.RFOK);
#endif
            strCardID = "";
            string ret      = "";
            int    i        = 0;
            byte[] databuff = new byte[16];
            byte[] buff     = new byte[32];

            for (i = 0; i < 16; i++)
            {
                databuff[i] = 0;
            }
            for (i = 0; i < 32; i++)
            {
                buff[i] = 0;
            }
            if (!FindCard(out ret))
            {
                return(ret);
            }
            if (!VerifyBKey(out ret))
            {
                if (!FindCard(out ret))
                {
                    return(ret);
                }
                if (!VerifyBKey2(out ret)) //return ret;
                {
                    if (!FindCard(out ret))
                    {
                        return(ret);
                    }
                    if (!VerifyBKey3(out ret))
                    {
                        return(ret);
                    }
                }
            }
            //读卡号
            st = RFDef.rf_read(icdev, sec * 4, databuff);
            if (st != 0)
            {
                quit();
                return(ConstMsg.RFREADERR);
            }
            else
            {
                RFDef.hex_a(databuff, buff, 16);
                string strtemp = System.Text.Encoding.ASCII.GetString(buff);
                strtemp = strtemp.ToLower();
                if (strtemp.StartsWith("aaa"))
                {
                    strCardID = strtemp.Substring(0, 8);
                }
                else if (strtemp.StartsWith("bbb"))
                {
                    strCardID = strtemp.Substring(3, 8);
                }
                else
                {
                    strCardID = strtemp.Substring(0, 6);
                }
            }

            //蜂鸣
            st = RFDef.rf_beep(icdev, 3);
            quit();
            return(ConstMsg.RFOK);
        }