public override IsoValue ParseBinary(int field, sbyte[] buf, int pos, ICustomField custom) { if (pos < 0) { throw new ParseException($"Invalid bin NUMERIC field {field} pos {pos}"); } if (pos + Length / 2 > buf.Length) { throw new ParseException( $"Insufficient data for bin {IsoType} field {field} of length {Length}, pos {pos}"); } //A long covers up to 18 digits if (Length < 19) { return(new IsoValue(IsoType.NUMERIC, Bcd.DecodeToLong(buf, pos, Length), Length)); } try { return(new IsoValue(IsoType.NUMERIC, Bcd.DecodeToBigInteger(buf, pos, Length), Length)); } catch (Exception) { throw new ParseException( $"Insufficient data for bin {IsoType} field {field} of length {Length}, pos {pos}"); } }
public override IsoValue ParseBinary(int field, sbyte[] buf, int pos, ICustomField custom) { if (pos < 0) { throw new ParseException($"Invalid DATE6 field {field} position {pos}"); } if (pos + 3 > buf.Length) { throw new ParseException($"Insufficient data for DATE6 field {field}, pos {pos}"); } var tens = new int[3]; var start = 0; for (var i = pos; i < pos + tens.Length; i++) { tens[start++] = Bcd.ParseBcdLength(buf[i]); } var year = tens[0] > 50 ? 1900 + tens[0] : 2000 + tens[0]; var month = tens[1]; var day = tens[2]; var dt = new DateTime(year, month, day, 0, 0, 0, 0); if (TimeZoneInfo != null) { dt = TimeZoneInfo.ConvertTime(dt, TimeZoneInfo); } return(new IsoValue(IsoType, AdjustWithFutureTolerance(new DateTimeOffset(dt)).DateTime)); }
public void toNumber_0x1234() { TestContext.WriteLine("BCD: toNumber, 0x1234)"); var result = Bcd.toNumber(new byte[] { 0x12, 0x34 }); Assert.That(result, Is.EqualTo(1234)); }
public object DecodeBinaryField(sbyte[] bytes, int offset, int length) { return(Bcd.DecodeToLong(bytes, offset, length * 2)); }
public void toBCD_12345() { TestContext.WriteLine("BCD: toBCD, encode 12345 to BCD"); var result = Bcd.toBCD(12345); Assert.That(result, Is.EqualTo(new byte[] { 0x01, 0x23, 0x45 })); }
public void toNumber_Empty() { TestContext.WriteLine("BCD: toNumber, empty"); var result = Bcd.toNumber(new byte[] { }); Assert.That(result, Is.EqualTo(0)); }
public void toBCD_1000020() { TestContext.WriteLine("BCD: toBCD, encode 1000020 to BCD"); var result = Bcd.toBCD(1000020); Assert.That(result, Is.EqualTo(new byte[] { 0x01, 0x00, 0x00, 0x20 })); }
public sbyte[] EncodeBinaryField(object obj) { var s = Convert.ToString(obj); var buf = new sbyte[s.Length / 2 + s.Length % 2]; Bcd.Encode(s, buf); return(buf); }
public sbyte[] EncodeBinaryField(object val) { var value = (BigInteger)val; var s = value.ToString(NumberFormatInfo.InvariantInfo); var buf = new sbyte[s.Length / 2 + s.Length % 2]; Bcd.Encode(s, buf); return(buf); }
MemoryPositionData[] parseMemoryPosition(byte[] ram) { if (memoryPosition == null) { return(null); } return(memoryPosition.Select(entry => { switch (entry.type) { case ENCODING_UINT8: var length = (entry.length != null) ? entry.length : 1; if (length == 1) { entry.value = ram[(ushort)entry.offset]; } else { UInt32 value = 0; for (var n = 0; n < length; n++) { if (entry.offset + n < ram.Length) { byte shl = (byte)((length - n - 1) * 8); value += (UInt32)(ram[(ushort)entry.offset + n] << shl); } } entry.value = value; } break; case ENCODING_STRING: var offset = (ushort)entry.offset; var dump = ""; while (ram[offset] > 31 && ram[offset] < 128 && dump.Length < MAXIMAL_STRING_LENGTH) { dump += Encoding.UTF8.GetString(new byte[] { ram[offset++] }); } entry.value = dump; break; case ENCODING_BCD: var bcdLength = entry.length != null ? (int)entry.length : 2; var number = Bcd.toNumber(ram.Skip((ushort)entry.offset).Take(bcdLength).ToArray()); entry.value = number; break; default: entry.value = "TYPE_INVALID"; break; } return entry; }).ToArray()); }
public void Test2() { double freq = 128.775; // Act var bcd = Bcd.Dec2Bcd(freq); // Assert var freqOutUint = Bcd.Bcd2Dec(bcd); var freqOutDouble = (double)freqOutUint / 100; Assert.That(freqOutDouble, Is.EqualTo(freq)); }
private byte[] ExpandSector(int minute, int second, int frame, ReadOnlySpan <byte> sector) { var data = new byte[OutputSectorLength]; sector.Slice(0, InputSectorLength).CopyTo(data.AsSpan(0x0010)); new byte[] { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, Bcd.Encode(minute), Bcd.Encode(second), Bcd.Encode(frame), 0x01 }.AsSpan().CopyTo(data); return(data); }
public bool SetDateTime(DateTime value) { byte temp; ReadRegister(RegisterSeconds, out temp); var transaction = CreateWriteTransaction(RegisterSeconds, (byte)(BitMask.GetValue(temp, ConfigClockHaltMask) | Bcd.Pack(value.Second)), Bcd.Pack(value.Minute), Bcd.Pack(value.Hour), Bcd.Pack((int)(value.DayOfWeek + 1)), Bcd.Pack(value.Day), Bcd.Pack(value.Month), Bcd.Pack(value.Year - 2000)); return(Execute(transaction) >= 8); }
/// <summary> /// Sets the Date and Time on the DS1307. /// </summary> /// <param name="value">The date and time as a System.DateTime object /// that will be saved on the DS1307.</param> public async Task SetAsync(DateTimeOffset value) { byte[] writeBuffer = new byte[] { RTC_ADDRESS, Bcd.FromInt(value.Second), Bcd.FromInt(value.Minute), Bcd.FromInt(value.Hour), Bcd.FromInt((int)value.DayOfWeek), Bcd.FromInt(value.Day), Bcd.FromInt(value.Month), Bcd.FromInt(value.Year >= 2000 ? value.Year - 2000: 0), }; await this.WriteAsync(writeBuffer); }
public async Task Resume() { byte[] readBuffer = new byte[1]; byte[] writeBuffer = new byte[] { RTC_ADDRESS }; // *** // *** Read the seconds // *** await this.WriteReadAsync(writeBuffer, readBuffer); int seconds = Bcd.ToInt((byte)(readBuffer[0] & 0x7f)); // *** // *** Reset bit 7 // *** writeBuffer = new byte[] { RTC_ADDRESS, (byte)(seconds & 0x80) }; await this.WriteAsync(writeBuffer); }
public DateTime GetDateTime() { var buffer = new byte[7]; ReadRegister(RegisterSeconds, buffer); // ignore clock halt bit buffer[RegisterSeconds] = BitMask.TurnOffBits(buffer[RegisterSeconds], ConfigClockHaltMask); int seconds = Bcd.Unpack(buffer[RegisterSeconds]); int minutes = Bcd.Unpack(buffer[RegisterMinutes]); // avoid 12/24 hour bit (forced 24 hour) buffer[RegisterHours] = (byte)(buffer[RegisterHours] & ~ConfigTwelveHourClockMask); int hours = Bcd.Unpack(buffer[RegisterHours]); int days = Bcd.Unpack(buffer[RegisterDays]); int months = Bcd.Unpack(buffer[RegisterMonths]); int years = Bcd.Unpack(buffer[RegisterYears]) + 2000; return(new DateTime(years, months, days, hours, minutes, seconds)); }
public void Test() { /* * def to_radio_bcd16(val): * encodable = int(val * 100) * remainder = ((val * 100) - encodable) / 100.0 * return int(str(encodable), 16), round(remainder,3), val */ double freq = 128.775; uint freq1 = (uint)(freq * 100); double remainder = ((freq * 100) - freq1) / 100.0; var bcd = Bcd.Dec2Bcd(freq1); var freq2 = Bcd.Bcd2Dec(bcd); Assert.That(freq2, Is.EqualTo(freq1)); //Assert.That(ToRadioBcd(128.775), Is.EqualTo(75895)); }
public void TestEncoding() { sbyte[] buf = new sbyte[2]; buf[0] = 1; buf[1] = 1; Bcd.Encode("00", buf); Assert.Equal(new byte[] { 0, 1 }.ToSignedBytes(), buf); Bcd.Encode("79", buf); Assert.Equal(new byte[] { 0x79, 1 }.ToSignedBytes(), buf); Bcd.Encode("80", buf); Assert.Equal(new byte[] { (byte)0x80, 1 }.ToSignedBytes(), buf); Bcd.Encode("99", buf); Assert.Equal(new byte[] { (byte)0x99, 1 }.ToSignedBytes(), buf); Bcd.Encode("100", buf); Assert.Equal(new byte[] { 1, 0 }.ToSignedBytes(), buf); Bcd.Encode("779", buf); Assert.Equal(new byte[] { 7, 0x79 }.ToSignedBytes(), buf); Bcd.Encode("999", buf); Assert.Equal(new byte[] { 9, (byte)0x99 }.ToSignedBytes(), buf); }
/// <summary> /// Get the current Date and Time from the DS1307. /// </summary> /// <returns>The current date and time as a System.DateTime object.</returns> public Task <DateTimeOffset> GetAsync() { DateTimeOffset returnValue = DateTime.MinValue; byte[] readBuffer = new byte[7]; byte[] writeBuffer = new byte[] { RTC_ADDRESS }; this.WriteReadAsync(writeBuffer, readBuffer); returnValue = new DateTime ( Bcd.ToInt(readBuffer[6]) + 2000, // Year Bcd.ToInt(readBuffer[5]), // Month Bcd.ToInt(readBuffer[4]), // Day Bcd.ToInt((byte)(readBuffer[2] & 0x3f)), // Hours over 24 hours (bit 6 is 24/12 hour format; 1 = 12, 0 = 24) Bcd.ToInt(readBuffer[1]), // Minutes Bcd.ToInt((byte)(readBuffer[0] & 0x7f)) // Seconds (bit 7 is the clock halt bit; 0 = enabled, 1 = halted) ); return(Task <DateTimeOffset> .FromResult(returnValue)); }
public void TestEncoding() { var buf = new sbyte[2]; buf[0] = 1; buf[1] = 1; Bcd.Encode("00", buf); Assert.Equal(new byte[] { 0, 1 }.ToInt8(), buf); Bcd.Encode("79", buf); Assert.Equal(new byte[] { 0x79, 1 }.ToInt8(), buf); Bcd.Encode("80", buf); Assert.Equal(new byte[] { 0x80, 1 }.ToInt8(), buf); Bcd.Encode("99", buf); Assert.Equal(new byte[] { 0x99, 1 }.ToInt8(), buf); Bcd.Encode("100", buf); Assert.Equal(new byte[] { 1, 0 }.ToInt8(), buf); Bcd.Encode("779", buf); Assert.Equal(new byte[] { 7, 0x79 }.ToInt8(), buf); Bcd.Encode("999", buf); Assert.Equal(new byte[] { 9, 0x99 }.ToInt8(), buf); }
public void TestDecoding() { sbyte[] buf = new sbyte[2]; Assert.Equal(0, Bcd.DecodeToLong(buf, 0, 1)); Assert.Equal(0, Bcd.DecodeToLong(buf, 0, 2)); Assert.Equal(0, Bcd.DecodeToLong(buf, 0, 3)); Assert.Equal(0, Bcd.DecodeToLong(buf, 0, 4)); buf[0] = 0x79; Assert.Equal(79, Bcd.DecodeToLong(buf, 0, 2)); buf[0] = unchecked ((sbyte)0x80); Assert.Equal(80, Bcd.DecodeToLong(buf, 0, 2)); buf[0] = unchecked ((sbyte)0x99); Assert.Equal(99, Bcd.DecodeToLong(buf, 0, 2)); buf[0] = 1; Assert.Equal(100, Bcd.DecodeToLong(buf, 0, 4)); buf[1] = 0x79; Assert.Equal(179, Bcd.DecodeToLong(buf, 0, 4)); buf[1] = unchecked ((sbyte)0x99); Assert.Equal(199, Bcd.DecodeToLong(buf, 0, 4)); buf[0] = 9; Assert.Equal(999, Bcd.DecodeToLong(buf, 0, 4)); }
public IsoSectorInfo Decode(ICdSector sector) { var result = new IsoSectorInfo { Number = sector.Number, Data = sector.Data, UserDataOffset = 0, UserDataLength = 2352 }; var data = sector.Data; if (!(data[0x0000] == 0x00 && data[0x0001] == 0xFF && data[0x0002] == 0xFF && data[0x0003] == 0xFF && data[0x0004] == 0xFF && data[0x0005] == 0xFF && data[0x0006] == 0xFF && data[0x0007] == 0xFF && data[0x0008] == 0xFF && data[0x0009] == 0xFF && data[0x000A] == 0xFF && data[0x000B] == 0x00)) { return(result); } result.Minutes = Bcd.Decode(data[0x000C]); result.Seconds = Bcd.Decode(data[0x000D]); result.Frames = Bcd.Decode(data[0x000E]); result.Mode = data[0x000F]; switch (result.Mode) { case 0x01: result.UserDataOffset = 16; result.UserDataLength = 2048; result.EdcOffset = 2064; result.EccOffset = 2072; break; case 0x02: result.UserDataOffset = 24; result.Id = data[0x0010]; result.Channel = data[0x0011]; result.EndOfRecord = (data[0x0012] & 0x01) != 0; result.IsVideo = (data[0x0012] & 0x02) != 0; result.IsAudio = (data[0x0012] & 0x04) != 0; result.IsData = (data[0x0012] & 0x08) != 0; result.Trigger = (data[0x0012] & 0x10) != 0; result.Form = (data[0x0012] & 0x020) != 0 ? 2 : 1; result.IsTimeDependent = (data[0x0012] & 0x40) != 0; result.EndOfFile = (data[0x0012] & 0x80) != 0; if (result.IsAudio ?? false) { switch (data[0x0013] & 0x03) { case 0x0: result.AudioChannels = 1; break; case 0x1: result.AudioChannels = 2; break; } switch (data[0x0013] & 0x0C) { case 0x0: result.AudioRate = 37800; break; case 0x4: result.AudioRate = 18900; break; } switch (data[0x0013] & 0x30) { case 0x00: result.AudioBitsPerSample = 4; break; case 0x10: result.AudioBitsPerSample = 8; break; } result.AudioEmphasis = (data[0x0013] & 0x40) != 0; } switch (result.Form ?? 1) { case 1: result.UserDataLength = 2048; result.EdcOffset = 2072; result.EccOffset = 2076; break; case 2: result.UserDataLength = (result.IsAudio ?? false) ? 2304 : 2324; result.EdcOffset = 2348; break; } break; } return(result); }
/// <inheritdoc /> public void SetTransponderCode(uint code) { uint bcdCode = Bcd.Dec2Bcd(code); _fsConnect.TransmitClientEvent(_setTransponderCodeEventId, bcdCode, _groupId); }
public object DecodeBinaryField(sbyte[] bytes, int offset, int length) => Bcd.DecodeToBigInteger(bytes, offset, length * 2);
public void Write(Stream outs, bool binary, bool forceStringEncoding) { switch (Type) { case IsoType.LLLVAR: case IsoType.LLVAR: case IsoType.LLLLVAR: WriteLengthHeader(Length, outs, Type, binary, forceStringEncoding); break; case IsoType.LLBIN: case IsoType.LLLBIN: case IsoType.LLLLBIN: WriteLengthHeader(binary ? Length : Length * 2, outs, Type, binary, forceStringEncoding); break; default: if (binary) { //numeric types in binary are coded like this sbyte[] buf = null; switch (Type) { case IsoType.NUMERIC: buf = new sbyte[Length / 2 + Length % 2]; break; case IsoType.AMOUNT: buf = new sbyte[6]; break; case IsoType.DATE10: case IsoType.DATE4: case IsoType.DATE_EXP: case IsoType.TIME: case IsoType.DATE12: case IsoType.DATE14: buf = new sbyte[Length / 2]; break; } //Encode in BCD if it's one of these types if (buf != null) { Bcd.Encode(ToString(), buf); outs.Write(buf.ToUnsignedBytes(), 0, buf.Length); return; } } break; } if (binary && (Type == IsoType.BINARY || Type == IsoType.LLBIN || Type == IsoType.LLLBIN || Type == IsoType.LLLLBIN)) { var missing = 0; if (Value is sbyte[]) { var bytes = (sbyte[])Value; outs.Write(bytes.ToUnsignedBytes(), 0, bytes.Length); missing = Length - bytes.Length; } else if (Encoder is ICustomBinaryField) { var binval = ((ICustomBinaryField)Encoder).EncodeBinaryField(Value); outs.Write(binval.ToUnsignedBytes(), 0, binval.Length); missing = Length - binval.Length; } else { var binval = HexCodec.HexDecode(Value.ToString()); outs.Write(binval.ToUnsignedBytes(), 0, binval.Length); missing = Length - binval.Length; } if (Type != IsoType.BINARY || missing <= 0) { return; } for (var i = 0; i < missing; i++) { outs.WriteByte(0); } } else { var bytes = ToString().GetSignedbytes(Encoding); outs.Write(bytes.ToUnsignedBytes(), 0, bytes.Length); } }
public void Write(Stream outs, bool binary, bool forceStringEncoding) { switch (Type) { case IsoType.LLLVAR: case IsoType.LLVAR: case IsoType.LLLLVAR: WriteLengthHeader(Length, outs, Type, binary, forceStringEncoding); break; case IsoType.LLBIN: case IsoType.LLLBIN: case IsoType.LLLLBIN: WriteLengthHeader(binary ? Length : Length * 2, outs, Type, binary, forceStringEncoding); break; default: if (binary) { //numeric types in binary are coded like this var buf = Type switch { IsoType.NUMERIC => new sbyte[Length / 2 + Length % 2], IsoType.AMOUNT => new sbyte[6], IsoType.DATE10 => new sbyte[Length / 2], IsoType.DATE4 => new sbyte[Length / 2], IsoType.DATE_EXP => new sbyte[Length / 2], IsoType.TIME => new sbyte[Length / 2], IsoType.DATE12 => new sbyte[Length / 2], IsoType.DATE14 => new sbyte[Length / 2], IsoType.DATE6 => new sbyte[Length / 2], _ => null }; //Encode in BCD if it's one of these types if (buf != null) { Bcd.Encode(ToString(), buf); outs.Write(buf.ToUint8(), 0, buf.Length); return; } } break; } if (binary && (Type == IsoType.BINARY || Type == IsoType.LLBIN || Type == IsoType.LLLBIN || Type == IsoType.LLLLBIN)) { var missing = 0; if (Value is sbyte[] bytes) { outs.Write(bytes.ToUint8(), 0, bytes.Length); missing = Length - bytes.Length; } else { switch (Encoder) { case ICustomBinaryField customBinaryField: { var binval = customBinaryField.EncodeBinaryField(Value); outs.Write(binval.ToUint8(), 0, binval.Length); missing = Length - binval.Length; break; } default: { var binval = HexCodec.HexDecode(Value.ToString()); outs.Write(binval.ToUint8(), 0, binval.Length); missing = Length - binval.Length; break; } } } if (Type != IsoType.BINARY || missing <= 0) { return; } for (var i = 0; i < missing; i++) { outs.WriteByte(0); } } else { var bytes = ToString().GetSignedBytes(Encoding); outs.Write(bytes.ToUint8(), 0, bytes.Length); } }
private void _parse() { _datatype = ((DataType)m_io.ReadU1()); switch (Datatype) { case DataType.Integer: { _dataValue = new Integer(m_io, this, m_root); break; } case DataType.Unsigned: { _dataValue = new Unsigned(m_io, this, m_root); break; } case DataType.Long: { _dataValue = new Long(m_io, this, m_root); break; } case DataType.Boolean: { _dataValue = new Boolean(m_io, this, m_root); break; } case DataType.Structure: { _dataValue = new Structure(m_io, this, m_root); break; } case DataType.Array: { _dataValue = new Array(m_io, this, m_root); break; } case DataType.Float64: { _dataValue = new Float64(m_io, this, m_root); break; } case DataType.DoNotCare: { _dataValue = new DoNotCare(m_io, this, m_root); break; } case DataType.LongUnsigned: { _dataValue = new LongUnsigned(m_io, this, m_root); break; } case DataType.Time: { _dataValue = new Time(m_io, this, m_root); break; } case DataType.OctetString: { _dataValue = new OctetString(m_io, this, m_root); break; } case DataType.NullData: { _dataValue = new NullData(m_io, this, m_root); break; } case DataType.CompactArray: { _dataValue = new CompactArray(m_io, this, m_root); break; } case DataType.DateTime: { _dataValue = new DateTime(m_io, this, m_root); break; } case DataType.DoubleLongUnsigned: { _dataValue = new DoubleLongUnsigned(m_io, this, m_root); break; } case DataType.Float32: { _dataValue = new Float32(m_io, this, m_root); break; } case DataType.Long64Unsigned: { _dataValue = new Long64Unsigned(m_io, this, m_root); break; } case DataType.DoubleLong: { _dataValue = new DoubleLong(m_io, this, m_root); break; } case DataType.Long64: { _dataValue = new Long64(m_io, this, m_root); break; } case DataType.Date: { _dataValue = new Date(m_io, this, m_root); break; } case DataType.Enum: { _dataValue = new Enum(m_io, this, m_root); break; } case DataType.Bcd: { _dataValue = new Bcd(m_io, this, m_root); break; } case DataType.VisibleString: { _dataValue = new VisibleString(m_io, this, m_root); break; } case DataType.BitString: { _dataValue = new BitString(m_io, this, m_root); break; } } }
public Bcd(int value) { _value = Bcd.FromInt(value); }