/// <summary> /// 预消费 /// </summary> /// <param name="paid">消费金额(分为单位)</param> /// <param name="walletType">钱包类型 1表示M1 2表示CPU</param> /// <param name="maxOfflineMonth">离线过期月数</param> /// <returns></returns> public YCTPaymentInfo Prepaid(int paid, int walletType, int maxOfflineMonth = 0) { DateTime dt = DateTime.Now; List <byte> data = new List <byte>(); data.AddRange(BEBinaryConverter.IntToBytes(paid)); data.AddRange(BEBinaryConverter.IntToBytes(paid)); data.Add(BCDConverter.IntToBCD(dt.Year / 100)); data.Add(BCDConverter.IntToBCD(dt.Year % 100)); data.Add(BCDConverter.IntToBCD(dt.Month)); data.Add(BCDConverter.IntToBCD(dt.Day)); data.Add(BCDConverter.IntToBCD(dt.Hour)); data.Add(BCDConverter.IntToBCD(dt.Minute)); data.Add(BCDConverter.IntToBCD(dt.Second)); data.Add((byte)(maxOfflineMonth > 0 ? 0x01 : 0x00)); data.Add(BCDConverter.IntToBCD(maxOfflineMonth)); var response = Request(YCTCommandType.Prepaid, data.ToArray()); if (response != null && response.IsCommandExcuteOk) { if (walletType == 1) { return(GetPaymentInfoFromM1(response)); } if (walletType == 2) { return(GetPaymentInfoFromCPU(response)); } } return(null); }
protected override List <byte> BuildRequestData() { List <byte> data = new List <byte>(); // Year (Last 2 Digits) data.Add(BCDConverter.GetBCDByte((byte)(_dateTime.Year % 100))); // Month data.Add(BCDConverter.GetBCDByte((byte)_dateTime.Month)); // Day data.Add(BCDConverter.GetBCDByte((byte)_dateTime.Day)); // Hour data.Add(BCDConverter.GetBCDByte((byte)_dateTime.Hour)); // Minute data.Add(BCDConverter.GetBCDByte((byte)_dateTime.Minute)); // Second data.Add(BCDConverter.GetBCDByte((byte)_dateTime.Second)); // Day of Week data.Add(BCDConverter.GetBCDByte(_dayOfWeek)); return(data); }
/// <summary> /// 设置商家编号 /// </summary> /// <param name="code"></param> /// <returns></returns> public bool SetServiceCode(int code) { code = code % 10000; //取数值的低四位 byte[] bcd = new byte[] { BCDConverter.IntToBCD(code / 100), BCDConverter.IntToBCD(code % 100) }; var response = Request(YCTCommandType.SetServiceCode, bcd); return(response != null && response.IsCommandExcuteOk); }
public void bcdconvert_bcd4byteconvert_byte_count_property() { BCDConverter c = new BCDConverter(); BCD4ByteConverter c4 = new BCD4ByteConverter(); BCDConverter pt = c; Assert.AreEqual(1, pt.ByteCount); pt = c4; //pt.ByteCount = 2; Assert.AreEqual(4, pt.ByteCount); Assert.AreEqual(4, c4.ByteCount); }
internal static ClockResult ExtractClock(ReadClockRequest request, FINSResponse response) { if (response.Data.Length < DATE_LENGTH + DAY_OF_WEEK_LENGTH) { throw new FINSException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '" + (DATE_LENGTH + DAY_OF_WEEK_LENGTH).ToString() + "'"); } ReadOnlyMemory <byte> data = response.Data; return(new ClockResult { ClockDateTime = getClockDateTime(data.Slice(0, DATE_LENGTH).ToArray()), DayOfWeek = BCDConverter.ToByte(data.ToArray()[DATE_LENGTH]), }); }
byte[] GetCoord(double f) { bool sign = f < 0; f = Math.Abs(f); int res = (int)Math.Truncate(f); res = res * 100000 + (int)Math.Round((f - res) * 60000); byte[] bytes = BCDConverter.GetBytes(res, 4); if (sign) { bytes[0] |= 0x80; } return(bytes); }
private static double getCycleTime(byte[] bytes) { if (bytes.Length != 4) { throw new ArgumentOutOfRangeException(nameof(bytes), "The Cycle Time Bytes Array Length must be 4"); } uint cycleTimeValue = BCDConverter.ToUInt32(bytes.Reverse().ToArray()); if (cycleTimeValue > 0) { return(cycleTimeValue / (double)10); } return(0); }
public void TestBCDConverter() { BCDConverter c = new BCDConverter(); byte[] bs = new byte[] { 0x10 }; int n = 10; object obj = c.ConvertToObject(bs); Assert.AreEqual(n, (int)obj); byte[] bs2 = c.ConvertToBytes(n); Assert.AreEqual(1, bs2.Length); Assert.AreEqual(0x10, bs2[0]); bs2 = c.ConvertToBytes(99 + 0); Console.WriteLine(bs2[0]); }
private static DateTime getClockDateTime(byte[] bytes) { byte year = BCDConverter.ToByte(bytes[0]); byte month = BCDConverter.ToByte(bytes[1]); byte day = BCDConverter.ToByte(bytes[2]); byte hour = BCDConverter.ToByte(bytes[3]); byte minute = BCDConverter.ToByte(bytes[4]); byte second = BCDConverter.ToByte(bytes[5]); if (year < 70) { return(new DateTime(2000 + year, month, day, hour, minute, second)); } else if (year < 100) { return(new DateTime(1900 + year, month, day, hour, minute, second)); } throw new FINSException("Invalid DateTime Values received from the PLC Clock"); }
private DateTime ReadDate(byte[] data) { if (data.Length == 3) { if (data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x00) { return(FromDate); } else if (data[0] == 0xFF && data[1] == 0xFF && data[2] == 0xFF) { return(FromDate); } else { return(new DateTime(2000 + BCDConverter.BCDtoInt(data[0]), BCDConverter.BCDtoInt(data[1]), BCDConverter.BCDtoInt(data[2]))); } } else { throw new InvalidCastException("指定的转换无效,字节数组必需为三个字节"); } }
byte[] GetPacket(TrackerPacket point) { int i = 0; byte[] buf = new byte[45 + 20]; for (int j = 0; j < buf.Length; j++) { buf[j] = 0; } buf[i++] = 0x29; buf[i++] = 0x29; buf[i++] = 0x80; i += 2; // length uint imei = (uint)(m_Tracker.m_IMEI - 14100000000); for (int k = 0; k < 4; k++) { buf[i + 3 - k] = (byte)(imei % 100); if (k != 2) { buf[i + 3 - k] |= 0x80; } imei = imei / 100; } i += 4; DateTime dt = CTime.GetTime(point.m_Time + 3600 * 2); buf[i++] = BCDConverter.GetBytes(dt.Year - 2000, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Month, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Day, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Hour, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Minute, 1)[0]; buf[i++] = BCDConverter.GetBytes(dt.Second, 1)[0]; GetCoord(point.m_fLat).CopyTo(buf, i); i += 4; GetCoord(point.m_fLng).CopyTo(buf, i); i += 4; BCDConverter.GetBytes(point.m_Speed, 2).CopyTo(buf, i); i += 2; BCDConverter.GetBytes(point.m_Direction, 2).CopyTo(buf, i); i += 2; decimal odo = 0, ignition = 0, oilcut = 0, pwr = 12000; decimal din1 = 0, din2 = 0, din3 = 0, din4 = 0; point.GetInput("DIN6", out ignition); point.GetInput("DIN7", out oilcut); point.GetInput("PWR", out pwr); point.GetInput("ODO", out odo); point.GetInput("DIN1", out din1); point.GetInput("DIN2", out din2); point.GetInput("DIN3", out din3); point.GetInput("DIN4", out din4); BIConverter.GetBytes((int)odo).CopyTo(buf, i); buf[i++] = (byte)(((pwr == 0) ? 16 : (pwr > 3000) ? 24 : 8) + ((point.IsFixed(false) ? 224 : 0))); i += 3; buf[i++] = (byte)(1 + ((oilcut > 0) ? 0 : 4) + ((din1 > 0) ? 0 : 8) + ((din2 > 0) ? 0 : 16) + ((din3 > 0) ? 0 : 32) + ((din4 > 0) ? 0 : 64) + ((ignition > 0) ? 0 : 128)); //ign, pwr, oil cut buf[i++] = 0xFC; //alarms buf[i++] = 0x36; // 32;//tcp buf[i++] = 0x00; buf[i++] = 0x00; buf[i++] = 0x78; buf[i++] = 0x00; buf[i++] = 0x00; buf[i++] = 0x00; buf[i++] = 0x00; buf[i++] = 0x3C; buf[i++] = 0x00; decimal fuel = 0; if (point.GetInput("FUEL", out fuel)) { string data = fuel.ToString("000.00").Replace(",", "."); BIConverter.GetBytes((ushort)(data.Length + 2)).CopyTo(buf, i); i += 2; BIConverter.GetBytes((ushort)0x0023).CopyTo(buf, i); i += 2; Encoding.ASCII.GetBytes(data).CopyTo(buf, i); i += data.Length; } if (point.GetInput("AIN5", out fuel)) { BIConverter.GetBytes((ushort)4).CopyTo(buf, i); i += 2; BIConverter.GetBytes((ushort)0x002D).CopyTo(buf, i); i += 2; BIConverter.GetBytes((ushort)fuel).CopyTo(buf, i); i += 2; } BIConverter.GetBytes((ushort)(i - 3)).CopyTo(buf, 3); buf[i++] = GetCRC(buf, 0, i); buf[i++] = 0x0D; byte[] res = new byte[i]; string str = "<<"; for (int j = 0; j < i; j++) { res[j] = buf[j]; str += " " + buf[j].ToString("X2"); } m_Logger.Push(LogLevel.INFO, 0, str); return(res); }
/// <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); }
/// <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 }