private float ReadScaledUInt32(Memory <byte> memory, uint baseAddress, RegisterMappingAttribute propertyRegisterMapping) { var value = (UInt32)ReadUInt16(memory, baseAddress, propertyRegisterMapping.Address); var scale = (Int16)ReadInt16(memory, baseAddress, propertyRegisterMapping.ScalingFactorAddress); return(Convert.ToSingle(value * Math.Pow(10, scale))); }
private string ReadString(Memory <byte> memory, uint baseAddress, RegisterMappingAttribute propertyRegisterMapping) { var span = GetMemory(memory, baseAddress, propertyRegisterMapping.Address, (int)propertyRegisterMapping.Size).Span; return(Encoding.UTF8.GetString(span).TrimEnd((char)0x00)); }
private T ReadRegister <T>(Memory <byte> memory, uint baseAddress, T instance, PropertyInfo property, RegisterMappingAttribute registerMapping) { var value = ReadValue(memory, baseAddress, property, registerMapping); var cast = GenericCastMethod.MakeGenericMethod(property.PropertyType).Invoke(null, new object[] { value }); property.SetValue(instance, cast); return(instance); }
private object ReadObject(Memory <byte> memory, uint baseAddress, PropertyInfo property, RegisterMappingAttribute propertyRegisterMapping) { var span = GetMemory(memory, baseAddress, propertyRegisterMapping.Address); var result = GenericReadMethod.MakeGenericMethod(property.PropertyType).Invoke(this, new object[] { span }); return(result); }
private object ReadValue(Memory <byte> memory, uint baseAddress, PropertyInfo property, RegisterMappingAttribute registerMapping) { switch (registerMapping.Type) { case RegisterType.Acc32: return(ReadAcc32(memory, baseAddress, registerMapping.Address)); case RegisterType.Int16: return(ReadInt16(memory, baseAddress, registerMapping.Address)); case RegisterType.Int32: return(ReadInt32(memory, baseAddress, registerMapping.Address)); case RegisterType.Object: return(ReadObject(memory, baseAddress, property, registerMapping)); case RegisterType.ScaledInt16: return(ReadScaledInt16(memory, baseAddress, registerMapping)); case RegisterType.ScaledInt32: return(ReadScaledInt32(memory, baseAddress, registerMapping)); case RegisterType.ScaledUInt16: return(ReadScaledUInt16(memory, baseAddress, registerMapping)); case RegisterType.ScaledUInt32: return(ReadScaledUInt32(memory, baseAddress, registerMapping)); case RegisterType.String: return(ReadString(memory, baseAddress, registerMapping)); case RegisterType.UInt16: return(ReadUInt16(memory, baseAddress, registerMapping.Address)); case RegisterType.UInt32: return(ReadUInt32(memory, baseAddress, registerMapping.Address)); default: throw new ArgumentException("Unknown register type"); } }