Esempio n. 1
0
        /// <summary>
        /// 外接读卡函数
        /// </summary>
        /// <param name="cardinfo"></param>
        /// <returns></returns>
        public static int ReadGasCard(ref cardInfo cardinfo)
        {
            //if (!IsUse) return 0;
            if (!IsUse)
            {
                int resDebug = 0;
                cardinfo.cardNo   = "2063838";
                cardinfo.cardType = "1";
                cardinfo.gasCount = "1";
                cardinfo.iccSpare = "0";
                cardinfo.icErroy  = "";
                cardinfo.icNum    = "1";

                return(0);
            }

            cardInfo cardinfoTemp = new cardInfo();
            string   enCrypt      = "";
            int      res          = 0;

            try
            {
                //苍南表读卡
                res = GasCardReader1.ReadCard(Port, Bps.ToString(), ref cardinfoTemp.cardNo, ref cardinfoTemp.gasCount, ref cardinfoTemp.iccSpare, ref cardinfoTemp.icNum, ref cardinfoTemp.icMark, ref cardinfoTemp.strEnCrypt, ref cardinfoTemp.icErroy);
                if (res == 6)
                {
                    //天信表读卡
                    int port = -1;
                    if (Port.StartsWith("COM"))
                    {
                        port = int.Parse(Port.Substring(3)) - 1;
                    }
                    byte[] sCardId   = new byte[200];
                    int    iGasCount = 0;
                    int    iICCSpare = 0;
                    int    iIcNum    = 0;
                    int    iIcMark   = 0;
                    byte[] sPassWord = new byte[200];
                    int    iIcErroy  = -1;
                    string sErrMsg   = null;

                    res = IcSmartCard.ReadCard("Tancy_IC.dll", port, Bps, sCardId, ref iGasCount, ref iICCSpare, ref iIcNum, ref iIcMark, sPassWord, ref iIcErroy, ref sErrMsg);
                    res = iIcErroy;
                    //cardinfoTemp.icErroy = sErrMsg;

                    string cardID   = System.Text.Encoding.Default.GetString(sCardId).Replace("\0", "");
                    string passWord = System.Text.Encoding.Default.GetString(sPassWord).Replace("\0", "");
                    if (sErrMsg != null)
                    {
                        sErrMsg = (sErrMsg).Replace("\0", "");
                    }
                    cardinfoTemp.icErroy = sErrMsg;
                    if (iIcErroy != 0)
                    {
                        return(res);
                    }
                    cardinfoTemp.cardNo   = string.IsNullOrEmpty(cardID) ? "" : cardID;
                    cardinfoTemp.gasCount = string.IsNullOrEmpty(iGasCount.ToString()) ? "" : iGasCount.ToString();
                    cardinfoTemp.iccSpare = string.IsNullOrEmpty(iICCSpare.ToString()) ? "" : iICCSpare.ToString();
                    cardinfoTemp.icNum    = string.IsNullOrEmpty(iIcNum.ToString()) ? "" : iIcNum.ToString();
                    cardinfoTemp.icMark   = string.IsNullOrEmpty(iIcMark.ToString()) ? "" : iIcMark.ToString();
                    if (!string.IsNullOrEmpty(passWord.ToString()))
                    {
                        cardinfoTemp.strEnCrypt = passWord.ToString();
                        StringBuilder temp = new StringBuilder(65);
                        StringBuilder pass = new StringBuilder(cardinfoTemp.strEnCrypt);
                        IcSmartCard.XinaoDeCrypt(pass, temp);
                        cardinfoTemp.strDeCrypt = temp.ToString();
                    }
                    else
                    {
                        cardinfoTemp.strEnCrypt = "";
                        cardinfoTemp.strDeCrypt = "";
                    }
                    //res = GasCardReader0.ReadCard(Port, Bps.ToString(), ref cardinfoTemp.cardNo, ref cardinfoTemp.gasCount, ref cardinfoTemp.iccSpare, ref cardinfoTemp.icNum, ref cardinfoTemp.icMark, ref cardinfoTemp.strEnCrypt, ref cardinfoTemp.icErroy);

                    cardinfoTemp.cardType = "0";
                    //res = GasCardReader1.DeCrypt(cardinfoTemp.strEnCrypt, ref cardinfoTemp.strDeCrypt);
                    //if (res != 64)
                    //    throw new Exception("GasCardReader0.DeCrypt err");
                    cardinfo = cardinfoTemp;
                    //res = 0;
                    return(res);
                }
                else if (res == 0)
                {
                    cardinfoTemp.cardType = "1";
                    res = GasCardReader1.DeCrypt(cardinfoTemp.strEnCrypt, ref cardinfoTemp.strDeCrypt);
                    if (res != 64)
                    {
                        throw new Exception("GasCardReader1.DeCrypt err");
                    }
                    //Log.Info("read De before:" + cardinfoTemp.strDeCrypt);

                    //Log.Info("read En before:" + cardinfoTemp.strEnCrypt);

                    cardinfoTemp.strDeCrypt = cardinfoTemp.strDeCrypt.Substring(0, 64);
                    cardinfoTemp.strEnCrypt = cardinfoTemp.strEnCrypt.Substring(0, 128);
                    Log.Info("read De:" + cardinfoTemp.strDeCrypt);
                    Log.Info("read En:" + cardinfoTemp.strEnCrypt);

                    cardinfo = cardinfoTemp;
                    res      = 0;
                    return(res);
                }
                else
                {
                    return(res);
                }
            }
            catch (Exception ex)
            {
                Log.Error("[GasCardReaderHelper][ReadGasCard]Error!\n" + ex.ToString());
                return(-1);
            }
        }