Beispiel #1
0
        private void WriteDog()
        {
            int ret = -1;

            byte[] rdDate = new byte[100];
            _myRandom.NextBytes(rdDate);
            ret = _Writer.WriteData(0, rdDate, _Key);

            byte[] data = null;
            if (ret == 0)
            {
                data = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(MydsEncrypt.Encrypt(CHECKSTRING));
                ret  = _Writer.WriteData(CHECKPOSITON, data, _Key);
            }

            if (ret == 0)
            {
                data = SEBinaryConverter.IntToBytes(txtProjectID.IntergerValue);
                ret  = _Writer.WriteData(61, data, _Key);
            }

            if (ret == 0)
            {
                SoftwareType sl = SoftwareType.None;
                if (this.chk汇海中考软件.Checked)
                {
                    sl |= SoftwareType.TYPE_汇海中考软件;
                }
                if (this.chk学生体测.Checked)
                {
                    sl |= SoftwareType.TYPE_汇海学生体测软件;
                }
                if (this.chk国民体测.Checked)
                {
                    sl |= SoftwareType.TYPE_汇海国民体测软件;
                }
                if (this.chk运动会.Checked)
                {
                    sl |= SoftwareType.TYPE_汇海运动会软件;
                }
                data = SEBinaryConverter.IntToBytes((int)sl);
                ret  = _Writer.WriteData(31, data, _Key);
            }

            if (ret == 0)
            {
                data = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(MydsEncrypt.Encrypt(dtStart.Value.Date.ToString("yyMMdd")));
                ret  = _Writer.WriteData(17, data, _Key);
            }

            if (ret == 0)
            {
                data = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(MydsEncrypt.Encrypt(dtEnd.Value.Date.ToString("yyMMdd")));
                ret  = _Writer.WriteData(5, data, _Key);
            }
            if (ret != 0)
            {
                throw new InvalidOperationException("写狗失败 errorcode=" + ret.ToString());
            }
        }
Beispiel #2
0
        public Packet CreateSetVacantPacket(byte address, short vacant)
        {
            //设置车位余数
            Packet packet = new Packet();

            packet.Order = OrderCode.Comm_SetVacantNotify;
            //分区位余
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes(vacant));
            return(packet);
        }
Beispiel #3
0
        /// <summary>
        /// 读取卡片的序列号
        /// </summary>
        /// <returns></returns>
        public override string ReadCardID()
        {
            string cardID = string.Empty;

            byte[] data = new byte[256];
            byte   ret  = HPC_RFID_DLL.ISO14443A_ReadCardSn(data);

            if (ret == 0)
            {
                cardID = SEBinaryConverter.BytesToLong(new byte[] { data[0], data[1], data[2], data[3] }).ToString();
            }
            return(cardID);
        }
Beispiel #4
0
        private ReportBase DeformatCardPermitedPacket(Packet packet)
        {
            //已经确认的事件
            //格式:事件序列号(4byte)+口地址(1byte)+上次地址(1byte)+事件状态(1byte)+停车状态(1byte)+事件参数(1byte)+发生卡内码(4byte)+
            //发生日期时间秒分时日月年(6byte)+操作员编号(1byte)+ 交易金额(2byte)+储值卡余额(4byte)+CRC
            OfflineCardReadReport report = new OfflineCardReadReport();

            report.Address       = packet.ReadByteFromParameter(4);
            report.CardID        = ((uint)SEBinaryConverter.BytesToInt(packet.ReadDataFromParameter(9, 4))).ToString();
            report.EventStatus   = CardEventStatus.Valid;
            report.EventDateTime = BytesToDateTime(packet.ReadDataFromParameter(13, 6));
            return(report);
        }
 /// <summary>
 /// 将整形IP转化成字符串型的IP地址,semode表示整形是否是小端模式的整数
 /// </summary>
 /// <param name="ip"></param>
 /// <param name="semode"></param>
 /// <returns></returns>
 public static string IPtoStr(int ip, bool semode)
 {
     if (semode)
     {
         return(IPtoStr(SEBinaryConverter.IntToBytes(ip)));
     }
     else
     {
         byte[] b = SEBinaryConverter.IntToBytes(ip);
         byte[] d = new byte[] { b[3], b[2], b[1], b[0] };
         return(IPtoStr(d));
     }
 }
Beispiel #6
0
        /// <summary>
        /// 写主变量传感器序列号
        /// </summary>
        public bool WritePVSensorSN(long longAddress, int sn)
        {
            RequestPacket request = new RequestPacket()
            {
                LongOrShort = 1,
                Address     = longAddress,
                Command     = 47,
                DataContent = SEBinaryConverter.IntToBytes(sn, 3).Reverse().ToArray(),
            };
            ResponsePacket response = Request(request);

            return(response != null);
        }
Beispiel #7
0
        /// <summary>
        /// 写设备的最终装配号
        /// </summary>
        public bool WriteFinalAssemblyNumber(long longAddress, int assemblyNumber)
        {
            RequestPacket request = new RequestPacket()
            {
                LongOrShort = 1,
                Address     = longAddress,
                Command     = 19,
                DataContent = SEBinaryConverter.IntToBytes(assemblyNumber, 3).Reverse().ToArray(),
            };
            ResponsePacket response = Request(request);

            return(response != null);
        }
 public int ReadInteger(int addr, int len)
 {
     if (len > 4)
     {
         throw new InvalidCastException("超过最大的整形数");
     }
     byte[] data = ReadData(addr, len);
     if (data != null)
     {
         return(SEBinaryConverter.BytesToInt(data));
     }
     throw new InvalidOperationException("访问加密狗错误,请插入正确的加密狗重试!");
 }
Beispiel #9
0
        private ReportBase DeformatCardWaitingPacket(Packet packet)
        {
            //正在等待处理的事件
            //口地址[1]+上次地址[1]+事件状态[1]+停车状态[1]+事件参数[1]+发生卡内码[4]+上次动作日期时间秒分时日月年[6]+发生日期时间秒分时日月年[6]+
            //操作员编号[1]+小数点位数[1]+交易金额[2]+储值卡余额[4]
            OfflineCardReadReport report = new OfflineCardReadReport();

            report.Address       = packet.ReadByteFromParameter(0);
            report.CardID        = ((uint)SEBinaryConverter.BytesToInt(packet.ReadDataFromParameter(5, 4))).ToString();
            report.EventStatus   = CardEventStatus.Pending;
            report.LastDateTime  = BytesToDateTime(packet.ReadDataFromParameter(9, 6));
            report.EventDateTime = BytesToDateTime(packet.ReadDataFromParameter(15, 6));
            return(report);
        }
Beispiel #10
0
        /// <summary>
        /// ¶ÁÈ¡¿¨Æ¬µÄÐòÁкÅ
        /// </summary>
        /// <returns></returns>
        public override string ReadCardID()
        {
            string         readdata = "";
            RFDEV_CARDINFO card     = new RFDEV_CARDINFO();

            card.CardNum = new byte[4];
            int ret = NLRF_QueryCardInfo(_Hdev, ref card);

            if (ret == 0)
            {
                readdata = SEBinaryConverter.BytesToLong(card.CardNum).ToString();
            }
            return(readdata);
        }
Beispiel #11
0
        private ReportBase DeformatParkVacantPacket(Packet packet)
        {
            ParkVacantReport report = new ParkVacantReport();

            report.Address = packet.Address;
            if (packet.Parameters.Count % 2 == 0)
            {
                for (int i = 0; i < packet.Parameters.Count; i += 2)
                {
                    byte[] data = packet.ReadDataFromParameter(i, 2);
                    report.ParkVacant += SEBinaryConverter.BytesToShort(data);
                }
            }
            return(report);
        }
Beispiel #12
0
        public List <Packet> CreateInsertCardPackets(byte address, CardInfo card)
        {
            List <Packet> packets = new List <Packet>();
            Packet        packet  = new Packet();

            packet.Address = address;
            packet.Order   = OrderCode.Comm_DownLoadCardNotify;
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes((short)card.Index));
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes((short)(card.Index + 1)));

            packets.Add(packet);
            packet = FormatCard(address, card, ActionType.Add);
            packets.Add(packet);
            return(packets);
        }
Beispiel #13
0
        //转换车牌号码
        private string PlateLicense(short sh0, short sh1, short sh2, short sh3, short sh4)
        {
            List <byte> bytes = new List <byte>();

            bytes.AddRange(SEBinaryConverter.ShortToBytes(sh0));
            bytes.AddRange(SEBinaryConverter.ShortToBytes(sh1));
            bytes.AddRange(SEBinaryConverter.ShortToBytes(sh2));
            bytes.AddRange(SEBinaryConverter.ShortToBytes(sh3));
            bytes.AddRange(SEBinaryConverter.ShortToBytes(sh4));

            string ret = Encoding.GetEncoding("GB2312").GetString(bytes.ToArray()).Trim();

            ret = ret.Trim('\0');
            return(ret);
        }
        public void TestLngConvert()
        {
            long a, b;

            byte[] data = null;

            a    = 969419446969496;
            data = SEBinaryConverter.LongToBytes(a);
            b    = SEBinaryConverter.BytesToLong(data);
            Assert.IsTrue(a == b);

            a    = -494641949416;
            data = SEBinaryConverter.LongToBytes(a);
            b    = SEBinaryConverter.BytesToLong(data);
            Assert.IsTrue(a == b);
        }
Beispiel #15
0
        //设置车位上下限
        public Packet CreateSetCarPortLimitationPacket(byte address, short upLimit, short downLimit)
        {
            Packet packet = new Packet();

            packet.Order = OrderCode.Comm_WriteParameterTable;
            packet.Parameters.Add((byte)9); //页号

            packet.Parameters.Add(1);       //分车场数量
            packet.Parameters.Add(0);       //备用字节
            //总车位上下限
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes(upLimit));
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes(downLimit));
            //分车场车位上下限
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes(upLimit));
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes(downLimit));
            return(packet);
        }
Beispiel #16
0
        private ReportBase DeformatCardReadReportPacket(Packet packet)
        {
            //上传卡号+CardKind+4bytesID
            CardReadReport report = new CardReadReport();

            report.Address = packet.Address;
            if (packet.Source == 0xb1)
            {
                report.Reader = EntranceReader.Reader1;
            }
            if (packet.Source == 0xb2)
            {
                report.Reader = EntranceReader.Reader2;
            }
            report.CardType = packet.ReadByteFromParameter(0);
            report.CardID   = ((uint)(SEBinaryConverter.BytesToInt(packet.ReadDataFromParameter(1, 4)))).ToString();
            return(report);
        }
Beispiel #17
0
        private ReportBase DeformatCardInvalidPacket(Packet packet)
        {
            CardInvalidEventReport report = new CardInvalidEventReport();

            report.Address     = packet.Address;
            report.InvalidType = GetInvalidType(packet.Parameters[0]);
            try
            {
                if (report.InvalidType == EventInvalidType.INV_UnRegister) //卡片未注册
                {
                    report.CardID = ((uint)SEBinaryConverter.BytesToInt(packet.ReadDataFromParameter(1, 4))).ToString();
                }
            }
            catch
            {
            }
            return(report);
        }
        public void TestIntConvert()
        {
            int i = 2000;

            byte[] data = SEBinaryConverter.IntToBytes(i);
            int    j    = SEBinaryConverter.BytesToInt(data);

            Assert.IsTrue(i == j);

            i    = -330;
            data = SEBinaryConverter.IntToBytes(i);
            j    = SEBinaryConverter.BytesToInt(data);
            Assert.IsTrue(i == j);

            i    = 94949491;
            data = SEBinaryConverter.IntToBytes(i);
            j    = SEBinaryConverter.BytesToInt(data);
            Assert.IsTrue(i == j);
            Console.Write(i.ToString());
        }
Beispiel #19
0
        /// <summary>
        /// 读取主变量传感器信息
        /// </summary>
        public SensorInfo ReadPVSensor(long longAddress)
        {
            SensorInfo    ret     = null;
            RequestPacket request = new RequestPacket()
            {
                LongOrShort = 1, Address = longAddress, Command = 14
            };
            ResponsePacket response = Request(request);

            if (response != null && response.DataContent != null && response.DataContent.Length >= 16)
            {
                byte[] d = response.DataContent;
                ret             = new SensorInfo();
                ret.SensorSN    = SEBinaryConverter.BytesToInt(new byte[] { d[2], d[1], d[0] });
                ret.UnitCode    = (UnitCode)d[3];
                ret.UpperLimit  = BitConverter.ToSingle(new byte[] { d[7], d[6], d[5], d[4] }, 0);
                ret.LowerLimit  = BitConverter.ToSingle(new byte[] { d[11], d[10], d[9], d[8] }, 0);
                ret.MinimumSpan = BitConverter.ToSingle(new byte[] { d[15], d[14], d[13], d[12] }, 0);
            }
            return(ret);
        }
Beispiel #20
0
 private static byte[] mainKey = new byte[] { 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0 }; //默认通讯密钥
 /// <summary>
 /// 通过项目编号生成项目密钥
 /// </summary>
 /// <param name="projectCode"></param>
 /// <returns></returns>
 public static byte[] CreateKey(int projectCode)
 {
     byte[] inputByteArray = SEBinaryConverter.LongToBytes(projectCode);
     using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
     {
         des.Key     = mainKey;
         des.IV      = mainKey;
         des.Mode    = CipherMode.ECB;
         des.Padding = PaddingMode.None;
         System.IO.MemoryStream ms = new System.IO.MemoryStream();
         using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
         {
             cs.Write(inputByteArray, 0, inputByteArray.Length);
             cs.FlushFinalBlock();
             cs.Close();
         }
         byte[] ret = ms.ToArray();
         ms.Close();
         return(ret);
     }
 }
Beispiel #21
0
        /// <summary>
        /// 获取UID
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="UIDLen"></param>
        /// <returns></returns>
        private string GetUID(byte[] UID, byte UIDLen)
        {
            ulong value = 0;

            byte[] data = new byte[UIDLen];
            Array.Copy(UID, data, UIDLen);
            value = (ulong)SEBinaryConverter.BytesToLong(data);
            if (WegenType == WegenType.Wengen26)
            {
                value &= 0xffffff;
            }
            else if (WegenType == WegenType.Wengen34)
            {
                value &= 0xffffffff;
            }
            if (value > 0)
            {
                return(value.ToString());
            }
            return(string.Empty);
        }
Beispiel #22
0
        /// <summary>
        /// 获取卡片序列号,wgType=0表示WEGEN 34, wgType=1表示WEGEN26协议
        /// </summary>
        /// <returns></returns>
        public string ReadSN(int wgType = 0)
        {
            string ret      = null;
            var    response = Request(YCTCommandType.ReadSerialNumber, null);

            if (response != null && response.IsCommandExcuteOk)
            {
                byte[] data = response.Data;
                if (data != null && data.Length >= 4)
                {
                    if (wgType == 1)
                    {
                        ret = SEBinaryConverter.BytesToLong(Slice(data, 0, 3)).ToString(); //取前三字节
                    }
                    else
                    {
                        ret = SEBinaryConverter.BytesToLong(Slice(data, 0, 4)).ToString();
                    }
                }
            }
            return(ret);
        }
Beispiel #23
0
        private Packet FormatCard(byte address, CardInfo card, ActionType action)
        {
            Packet packet = new Packet();

            packet.Address = address;
            packet.Order   = (byte)(action == ActionType.Upate ? OrderCode.Comm_UpdateCard : OrderCode.Comm_DownLoadCard);
            //卡片位置序号
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes(card.Index));
            //卡号
            packet.Parameters.AddRange(SEBinaryConverter.IntToBytes((int)Convert.ToUInt32(card.CardID)));
            //注册标记
            packet.Parameters.Add(GetHardwareCardStatus(card.Status));
            //卡片类型字段为一复合字段,高4位为车型,低四位为卡片类型
            byte b = (byte)((byte)card.CarType * 16 + ((byte)card.CardType & 0x0F));

            packet.Parameters.Add(b);
            //有效期
            packet.Parameters.AddRange(DateToBytes(card.ValidDate));
            //入场权限
            packet.Parameters.AddRange(SEBinaryConverter.IntToBytes(0));
            //卡片编号
            packet.Parameters.AddRange(SEBinaryConverter.ShortToBytes(card.CardNum));
            //车牌问候
            packet.AddBytes(new byte[10]);
            //余额,如果侠额小于零时只下发0,因为硬件不支持负数金额,会变成很大的正数
            packet.Parameters.AddRange(SEBinaryConverter.IntToBytes(TariffSetting.Current.TariffOption.FromYuan(card.Balance > 0 ? card.Balance : 0)));
            //入场标记(入场bit7=1)
            packet.Parameters.Add((byte)card.ParkingStatus);
            //进场时间
            packet.Parameters.AddRange(DateTimeToBytes(card.LastDateTime));
            //上次地址
            packet.Parameters.Add((byte)card.LastEntrance);
            //权限组
            packet.Parameters.Add(card.AccessID);
            packet.Parameters.Add(0);
            packet.Parameters.Add(0);
            return(packet);
        }
        public void TestShortConvert()
        {
            short s1, s2;

            byte[] data = null;

            s1   = 3000;
            data = SEBinaryConverter.ShortToBytes(s1);
            s2   = SEBinaryConverter.BytesToShort(data);
            Assert.IsTrue(s1 == s2);

            s1   = -32768;
            data = SEBinaryConverter.ShortToBytes(s1);
            s2   = SEBinaryConverter.BytesToShort(data);
            Assert.IsTrue(s1 == s2);

            int i = 65535;

            data = SEBinaryConverter.ShortToBytes((short)i);
            s2   = SEBinaryConverter.BytesToShort(data);
            Assert.IsTrue(s2 == (short)i);
            Console.WriteLine(s2.ToString());
        }
Beispiel #25
0
        private byte[] TimeEntityToBytes(TimeEntity time)
        {
            short s = (short)(time.Hour * 60 + time.Minute);

            return(SEBinaryConverter.ShortToBytes(s));
        }
Beispiel #26
0
        /// <summary>
        /// 设置卡片信息
        /// </summary>
        /// <param name="card"></param>
        /// <param name="data"></param>
        /// <param name="keepStaus"></param>
        /// <returns></returns>
        private void SetCardInfo(CardInfo info, byte[] data, bool keepStaus)
        {
            int block0 = 0 * 16;
            int block1 = 1 * 16;
            int block2 = 2 * 16;

            byte[] tempbytes = new byte[4];

            #region 块0
            info.CardVersion = data[block0 + 0];

            if (!keepStaus)
            {
                info.Status = (data[block0 + 1] & 0x80) == 0x80 ? CardStatus.Enabled : CardStatus.Disabled;
            }

            //List<CardType> cardTypes = null;
            switch (data[block0 + 1] & 0x0F)
            {
            case 0x00:
                info.CardType = CardType.VipCard;
                break;

            case 0x01:
                info.CardType = CardType.OwnerCard;
                break;

            case 0x02:
                info.CardType = CardType.MonthRentCard;
                break;

            case 0x03:
                info.CardType = CardType.PrePayCard;
                break;

            case 0x04:
                info.CardType = CardType.TempCard;
                break;

            case 0x05:
                info.CardType = CardType.UserDefinedCard1.GetFirstCardTypeFromBase;
                break;

            case 0x06:
                info.CardType = CardType.UserDefinedCard2.GetFirstCardTypeFromBase;
                break;

            case 0x0E:
                info.CardType = CardType.OperatorCard;
                break;

            default:
                info.CardType = CardType.TempCard;
                break;
            }
            info.CarType = (byte)((data[block0 + 1] >> 4) & 0x03);

            info.OnlineHandleWhenOfflineMode = (data[block0 + 2] & 0x01) != 0x01;
            info.CanRepeatIn       = (data[block0 + 2] & 0x04) != 0x04;
            info.CanRepeatOut      = (data[block0 + 2] & 0x08) != 0x08;
            info.WithCount         = (data[block0 + 2] & 0x10) == 0x10;
            info.CanEnterWhenFull  = (data[block0 + 2] & 0x20) != 0x20;
            info.HolidayEnabled    = (data[block0 + 2] & 0x40) == 0x40;
            info.EnableWhenExpired = (data[block0 + 2] & 0x80) != 0x80;

            info.AccessID = data[block0 + 3];

            if (data[block0 + 4] == 0x00 && data[block0 + 5] == 0x00 && data[block0 + 6] == 0x00)
            {
                info.ActivationDate = DateTime.MaxValue;
            }
            else if (data[block0 + 4] == 0xFF && data[block0 + 5] == 0xFF && data[block0 + 6] == 0xFF)
            {
                info.ActivationDate = DateTime.MinValue;
            }
            else
            {
                info.ActivationDate = new DateTime(2000 + BCDConverter.BCDtoInt(data[block0 + 4]), BCDConverter.BCDtoInt(data[block0 + 5]), BCDConverter.BCDtoInt(data[block0 + 6]));
            }

            if (data[block0 + 6] == 0x00 && data[block0 + 8] == 0x00 && data[block0 + 9] == 0x00)
            {
                info.ValidDate = DateTime.MinValue;
            }
            else if (data[block0 + 7] == 0xFF && data[block0 + 8] == 0xFF && data[block0 + 9] == 0xFF)
            {
                info.ValidDate = DateTime.MaxValue;
            }
            else
            {
                info.ValidDate = new DateTime(2000 + BCDConverter.BCDtoInt(data[block0 + 7]), BCDConverter.BCDtoInt(data[block0 + 8]), BCDConverter.BCDtoInt(data[block0 + 9]));
            }

            #endregion

            #region 块1
            info.ParkingStatus  = (data[block1 + 0] & 0x01) != 0x01 ? ParkingStatus.In : 0;
            info.ParkingStatus |= (data[block1 + 0] & 0x02) != 0x02 ? ParkingStatus.IndoorIn : 0;
            info.ParkingStatus |= (data[block1 + 0] & 0x08) != 0x08 ? ParkingStatus.PaidBill : 0;
            info.ParkingStatus |= (data[block1 + 0] & 0x40) != 0x40 ? ParkingStatus.NestedParkMarked : 0;

            if (data[block1 + 1] == 0x00 && data[block1 + 2] == 0x00 && data[block1 + 3] == 0x00 && data[block1 + 4] == 0x00)
            {
                //第1层车场入场时间无效
            }
            else
            {
                tempbytes[0]      = data[block1 + 1];
                tempbytes[1]      = data[block1 + 2];
                tempbytes[2]      = data[block1 + 3];
                tempbytes[3]      = data[block1 + 4];
                info.LastDateTime = FromDate.AddSeconds(SEBinaryConverter.BytesToLong(tempbytes));
            }

            if (data[block1 + 5] == 0x00 && data[block1 + 6] == 0x00 && data[block1 + 7] == 0x00 && data[block1 + 8] == 0x00)
            {
                //缴费时间无效
            }
            else
            {
                tempbytes[0]      = data[block1 + 5];
                tempbytes[1]      = data[block1 + 6];
                tempbytes[2]      = data[block1 + 7];
                tempbytes[3]      = data[block1 + 8];
                info.PaidDateTime = FromDate.AddSeconds(SEBinaryConverter.BytesToLong(tempbytes));
            }

            tempbytes[0] = data[block1 + 9];
            tempbytes[1] = data[block1 + 10];
            tempbytes[2] = data[block1 + 11];
            tempbytes[3] = 0x00;
            info.ParkFee = SEBinaryConverter.BytesToInt(tempbytes) / 100.00M;

            tempbytes[0]      = data[block1 + 12];
            tempbytes[1]      = data[block1 + 13];
            tempbytes[2]      = data[block1 + 14];
            tempbytes[3]      = 0x00;
            info.TotalPaidFee = SEBinaryConverter.BytesToInt(tempbytes) / 100.00M;
            //info.TotalFee = SEBinaryConverter.BytesToInt(tempbytes) / 100.00M;


            #endregion

            #region 块2
            tempbytes[0] = data[block2 + 0];
            tempbytes[1] = data[block2 + 1];
            tempbytes[2] = data[block2 + 2];
            tempbytes[3] = 0x00;
            info.Balance = SEBinaryConverter.BytesToInt(tempbytes) / 100.00M;

            Array.Resize(ref tempbytes, 9);
            Array.Copy(data, block2 + 6, tempbytes, 0, 9);
            if (BytesIsAllZero(tempbytes))
            {
                info.CarPlate = string.Empty;
            }
            else
            {
                info.CarPlate     = Encoding.GetEncoding("gb2312").GetString(tempbytes);
                info.LastCarPlate = info.CarPlate;////脱机模式时,卡片的最后一次识别车牌保存在卡片里的车牌号码中
            }
            #endregion
        }
 public static string IPtoStr(int ip)
 {
     return(IPtoStr(SEBinaryConverter.IntToBytes(ip)));
 }
        private void WriteDog(SoftDogInfo dog)
        {
            int ret = -1;

            byte[] rdDate = new byte[100];
            _myRandom.NextBytes(rdDate);
            ret = DogWrite_32(rdDate.Length, 0, rdDate);

            byte[] data = null;
            if (ret == 0)
            {
                data = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(MydsEncrypt.Encrypt(CHECKSTRING));
                ret  = DogWrite_32(data.Length, CHECKPOSITON, data);
            }

            if (ret == 0)
            {
                data = SEBinaryConverter.IntToBytes(dog.ProjectNo);
                ret  = DogWrite_32(data.Length, 61, data);
            }

            if (ret == 0)
            {
                data = SEBinaryConverter.IntToBytes((int)dog.SoftwareList);
                ret  = DogWrite_32(data.Length, 31, data);
            }

            if (ret == 0)
            {
                data = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(MydsEncrypt.Encrypt(dog.StartDate.ToString("yyMMdd")));
                ret  = DogWrite_32(data.Length, 17, data);
            }

            if (ret == 0)
            {
                data = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(MydsEncrypt.Encrypt(dog.ExpiredDate.ToString("yyMMdd")));
                ret  = DogWrite_32(data.Length, 5, data);
            }
            if (ret == 0)
            {
                ret = DogWrite_32(1, 37, new byte[] { (byte)(dog.IsHost ? 1 : 0) });
            }
            if (ret == 0 && !string.IsNullOrEmpty(txtUser.Text.Trim()))
            {
                data = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(MydsEncrypt.Encrypt(dog.DBUser));
                byte[] temp = new byte[10];
                for (int i = 0; i < temp.Length; i++)
                {
                    if (i < data.Length)
                    {
                        temp[i] = data[i];
                    }
                    else
                    {
                        temp[i] = 0x20;
                    }
                }
                ret = DogWrite_32(temp.Length, 40, temp);
            }
            if (ret == 0 && !string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                data = System.Text.ASCIIEncoding.GetEncoding("GB2312").GetBytes(MydsEncrypt.Encrypt(dog.DBPassword));
                byte[] temp = new byte[10];
                for (int i = 0; i < temp.Length; i++)
                {
                    if (i < data.Length)
                    {
                        temp[i] = data[i];
                    }
                    else
                    {
                        temp[i] = 0x20;
                    }
                }
                ret = DogWrite_32(temp.Length, 50, temp);
            }
            if (ret != 0)
            {
                throw new InvalidOperationException("写狗失败 errorcode=" + ret.ToString());
            }
        }
Beispiel #29
0
        /// <summary>
        /// 从卡片扇区数据中获取并设置卡片信息
        /// </summary>
        /// <param name="card">卡片实体类</param>
        /// <param name="data">扇区信息</param>
        /// <param name="keepStatus">是否保持卡片实体类卡片状态,不从卡片扇区数据中获取卡片状态</param>
        /// <returns></returns>
        public CardInfo GetCardInfoFromData(byte[] data)
        {
            CardInfo card = null;

            try
            {
                if (IsValidData(data))
                {
                    card = new CardInfo();
                    #region 块0
                    byte[] block0 = new byte[16];
                    Array.Copy(data, 0, block0, 0, block0.Length);
                    card.CardVersion = block0[0];

                    card.Status   = (block0[1] & 0x80) == 0x80 ? CardStatus.Enabled : CardStatus.Disabled;
                    card.CardType = GetCardType((byte)(block0[1] & 0x0F));
                    card.CarType  = (byte)((block0[1] >> 4) & 0x03);

                    card.OnlineHandleWhenOfflineMode = (block0[2] & 0x01) != 0x01;
                    card.CanRepeatIn       = (block0[2] & 0x04) != 0x04;
                    card.CanRepeatOut      = (block0[2] & 0x08) != 0x08;
                    card.WithCount         = (block0[2] & 0x10) == 0x10;
                    card.CanEnterWhenFull  = (block0[2] & 0x20) != 0x20;
                    card.HolidayEnabled    = (block0[2] & 0x40) == 0x40;
                    card.EnableWhenExpired = (block0[2] & 0x80) != 0x80;

                    card.AccessID       = block0[3];
                    card.ActivationDate = ReadDate(new byte[] { block0[4], block0[5], block0[6] });
                    card.ValidDate      = ReadDate(new byte[] { block0[7], block0[8], block0[9] });
                    #endregion

                    #region 块1
                    byte[] block1 = new byte[16];
                    Array.Copy(data, 16, block1, 0, block1.Length);

                    card.ParkingStatus |= (block1[0] & 0x01) != 0x01 ? ParkingStatus.In : 0;
                    card.ParkingStatus |= (block1[0] & 0x02) != 0x02 ? ParkingStatus.IndoorIn : 0;
                    card.ParkingStatus |= (block1[0] & 0x08) != 0x08 ? ParkingStatus.PaidBill : 0;
                    card.ParkingStatus |= (block1[0] & 0x40) != 0x40 ? ParkingStatus.NestedParkMarked : 0;

                    long seconds = SEBinaryConverter.BytesToLong(new byte[] { block1[1], block1[2], block1[3], block1[4] });
                    if (seconds > 0)
                    {
                        card.LastDateTime = FromDate.AddSeconds(seconds);
                    }

                    seconds = SEBinaryConverter.BytesToLong(new byte[] { block1[5], block1[6], block1[7], block1[8] });
                    if (seconds > 0)
                    {
                        card.PaidDateTime = FromDate.AddSeconds(seconds);
                    }

                    card.ParkFee      = SEBinaryConverter.BytesToInt(new byte[] { block1[9], block1[10], block1[11] }) / 100.00M;
                    card.TotalPaidFee = SEBinaryConverter.BytesToInt(new byte[] { block1[12], block1[13], block1[14] }) / 100.00M;
                    #endregion

                    #region 块2
                    byte[] block2 = new byte[16];
                    Array.Copy(data, 32, block2, 0, block2.Length);
                    card.Balance = SEBinaryConverter.BytesToInt(new byte[] { block2[0], block2[1], block2[2] }) / 100.00M;

                    if (block2[3] == 0x00 && block2[4] == 0x00 && block2[5] == 0x00)
                    {
                        //免费时间点无效
                    }
                    else
                    {
                        card.FreeDateTime = FromDate.AddMinutes(SEBinaryConverter.BytesToInt(new byte[] { block2[3], block2[4], block2[5], 0x0 }));
                    }

                    byte[] tempbytes = new byte[9];
                    Array.Copy(block2, 6, tempbytes, 0, 9);
                    if (BytesIsAllZero(tempbytes))
                    {
                        card.CarPlate = string.Empty;
                    }
                    else
                    {
                        card.CarPlate = Encoding.GetEncoding("gb2312").GetString(tempbytes, 0, tempbytes.Length);
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex);
            }
            return(card);
        }
Beispiel #30
0
        /// <summary>
        /// 根据卡片实体类生成卡片数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public byte[] CreateDateBytes(CardInfo info)
        {
            try
            {
                int block0 = 0 * 16;
                int block1 = 1 * 16;
                int block2 = 2 * 16;

                byte[] tempbytes = new byte[4];
                byte[] data      = new byte[48];

                #region 块0
                data[block0 + 0] = info.CardVersion;


                //这里需要使用CardType来判断,如使用IsMonthCard,业主卡也属于月租卡
                data[block0 + 1] |= GetByte(info.CardType);
                data[block0 + 1] |= (byte)((info.CarType & 0x03) << 4);
                data[block0 + 1] |= 0x40;//预留,置1
                data[block0 + 1] |= info.Status == CardStatus.Enabled ? (byte)0x80 : (byte)0x00;

                data[block0 + 2] |= info.OnlineHandleWhenOfflineMode ? (byte)0x00 : (byte)0x01;
                data[block0 + 2] |= 0x02;//预留,置1
                data[block0 + 2] |= info.CanRepeatIn ? (byte)0x00 : (byte)0x04;
                data[block0 + 2] |= info.CanRepeatOut ? (byte)0x00 : (byte)0x08;
                data[block0 + 2] |= info.WithCount ? (byte)0x10 : (byte)0x00;
                data[block0 + 2] |= info.CanEnterWhenFull ? (byte)0x00 : (byte)0x20;
                data[block0 + 2] |= info.HolidayEnabled ? (byte)0x40 : (byte)0x00;
                data[block0 + 2] |= info.EnableWhenExpired ? (byte)0x00 : (byte)0x80;

                data[block0 + 3] = info.AccessID;

                if (info.ActivationDate != null && info.ActivationDate != DateTime.MaxValue)
                {
                    if (info.ActivationDate == DateTime.MinValue)
                    {
                        data[block0 + 4] = 0xFF;
                        data[block0 + 5] = 0xFF;
                        data[block0 + 6] = 0xFF;
                    }
                    else if (info.ActivationDate.Year - 2000 >= 0)
                    {
                        data[block0 + 4] = BCDConverter.IntToBCD(info.ActivationDate.Year % 100);
                        data[block0 + 5] = BCDConverter.IntToBCD(info.ActivationDate.Month);
                        data[block0 + 6] = BCDConverter.IntToBCD(info.ActivationDate.Day);
                    }
                }

                if (info.ValidDate != null && info.ValidDate != DateTime.MinValue)
                {
                    if (info.ValidDate == DateTime.MaxValue)
                    {
                        data[block0 + 7] = 0xFF;
                        data[block0 + 8] = 0xFF;
                        data[block0 + 9] = 0xFF;
                    }
                    else if (info.ValidDate.Year - 2000 >= 0)
                    {
                        data[block0 + 7] = BCDConverter.IntToBCD(info.ValidDate.Year % 100);
                        data[block0 + 8] = BCDConverter.IntToBCD(info.ValidDate.Month);
                        data[block0 + 9] = BCDConverter.IntToBCD(info.ValidDate.Day);
                    }
                }

                data[block0 + 10] = 0xFF; //预留(留作卡号等使用),默认0xFF

                data[block0 + 11] = 0xFF; //预留(留作卡号等使用),默认0xFF

                data[block0 + 12] = 0xFF; //预留(留作卡号等使用),默认0xFF

                data[block0 + 13] = 0xFF; //预留(留作卡号等使用),默认0xFF

                data[block0 + 14] = 0xFF; //预留(留作卡号等使用),默认0xFF

                data[block0 + 15] = GetDataLRC(data, 0);
                #endregion

                #region 块1
                data[block1 + 0] |= (byte)((info.ParkingStatus & ParkingStatus.In) == ParkingStatus.In ? 0x00 : 0x01);
                data[block1 + 0] |= (byte)((info.ParkingStatus & ParkingStatus.IndoorIn) == ParkingStatus.IndoorIn ? 0x00 : 0x02);
                data[block1 + 0] |= (byte)((info.ParkingStatus & ParkingStatus.PaidBill) == ParkingStatus.PaidBill ? 0x00 : 0x08);
                data[block1 + 0] |= (byte)((info.ParkingStatus & ParkingStatus.NestedParkMarked) == ParkingStatus.NestedParkMarked ? 0x00 : 0x40);
                data[block1 + 0] |= 0xB4;//其余置1

                if (info.LastDateTime < FromDate)
                {
                    return(null);                             //如果刷卡时间小于起始时间,返回空值
                }
                tempbytes        = SEBinaryConverter.UintToBytes((uint)(info.LastDateTime - FromDate).TotalSeconds);
                data[block1 + 1] = tempbytes[0];
                data[block1 + 2] = tempbytes[1];
                data[block1 + 3] = tempbytes[2];
                data[block1 + 4] = tempbytes[3];

                if (info.PaidDateTime.HasValue)
                {
                    if (info.PaidDateTime.Value < FromDate)
                    {
                        return(null);                                   //如果缴费时间小于起始时间,返回空值
                    }
                    tempbytes        = SEBinaryConverter.UintToBytes((uint)(info.PaidDateTime.Value - FromDate).TotalSeconds);
                    data[block1 + 5] = tempbytes[0];
                    data[block1 + 6] = tempbytes[1];
                    data[block1 + 7] = tempbytes[2];
                    data[block1 + 8] = tempbytes[3];
                }

                if (info.ParkFee > 167772.15M)
                {
                    return(null);                          //如果余额大于最大金额167772.15元,返回空值
                }
                tempbytes         = SEBinaryConverter.IntToBytes((int)(info.ParkFee * 100));
                data[block1 + 9]  = tempbytes[0];
                data[block1 + 10] = tempbytes[1];
                data[block1 + 11] = tempbytes[2];

                //if (info.TotalFee > 167772.15M) return null;//如果余额大于最大金额167772.15元,返回空值
                //tempbytes = SEBinaryConverter.IntToBytes((int)info.TotalFee * 100);
                if (info.TotalPaidFee > 167772.15M)
                {
                    return(null);                               //如果已缴费用大于最大金额167772.15元,返回空值
                }
                tempbytes         = SEBinaryConverter.IntToBytes((int)(info.TotalPaidFee * 100));
                data[block1 + 12] = tempbytes[0];
                data[block1 + 13] = tempbytes[1];
                data[block1 + 14] = tempbytes[2];

                data[block1 + 15] = GetDataLRC(data, 1);
                #endregion

                #region 块2
                if (info.Balance > 167772.15M)
                {
                    return(null);                          //如果余额大于最大金额167772.15元,返回空值
                }
                tempbytes        = SEBinaryConverter.IntToBytes((int)(info.Balance * 100));
                data[block2 + 0] = tempbytes[0];
                data[block2 + 1] = tempbytes[1];
                data[block2 + 2] = tempbytes[2];

                if (info.FreeDateTime.HasValue)
                {
                    if (info.FreeDateTime.Value < FromDate)
                    {
                        return(null);                                   //如果免费时间点小于起始时间,返回空值
                    }
                    TimeSpan ts = info.FreeDateTime.Value - FromDate;
                    if (ts.TotalMinutes < 0xFFFFFF)
                    {
                        tempbytes        = SEBinaryConverter.IntToBytes((int)ts.TotalMinutes);
                        data[block2 + 3] = tempbytes[0];
                        data[block2 + 4] = tempbytes[1];
                        data[block2 + 5] = tempbytes[2];
                    }
                    else
                    {
                        //超过最大值时设为最大值
                        data[block2 + 3] = 0xFF;
                        data[block2 + 4] = 0xFF;
                        data[block2 + 5] = 0xFF;
                    }
                }

                if (!string.IsNullOrEmpty(info.CarPlate))
                {
                    tempbytes = Encoding.GetEncoding("gb2312").GetBytes(info.CarPlate);
                    if (tempbytes.Length > 9)//最多9字节
                    {
                        Array.Resize(ref tempbytes, 9);
                    }
                    Array.Copy(tempbytes, 0, data, block2 + 6, tempbytes.Length);//9字节,不够9字节用0x00填充
                }

                data[block2 + 15] = GetDataLRC(data, 2);
                #endregion

                return(data);
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex);
            }
            return(null);
        }