public void CreateProperty_Set_ByteArray <T> (MemoryRegister <T> memoryRegister) { base.AddMethod(METHODS_SET_BYTE_PREFIX + memoryRegister.id, new Action <byte[]>((_value) => { this.SetByteArrayToMem(_value, memoryRegister.address, memoryRegister.size); })); }
// Use with <CustomSet>method:ULongToBcd</CustomSet> public byte[] ULongToBcd(MemoryRegister <ulong> MemoryRegister, dynamic inputValue) { if (inputValue is string) { return(this.ULongToBcd_Logic(inputValue, MemoryRegister.size)); } return(this.ULongToBcd_Logic(inputValue.ToString(), MemoryRegister.size)); }
public void CreateProperty_Get_ByteArray <T> (MemoryRegister <T> memoryRegister) { base.AddMethod(METHODS_GET_BYTE_PREFIX + memoryRegister.id, new Func <byte[]>(() => { return(this.GetByteArrayFromMem(memoryRegister.address, memoryRegister.size)); })); }
public async Task <bool> Equals(MemoryRegister <T> other) { if (other == null) { return(false); } bool ok_id = string.Equals(this.id, other.id); bool ok_description = string.Equals(this.description, other.description); bool ok_address = (this.address == other.address); bool ok_size = (this.size == other.size); bool ok_write = (this.write == other.write); byte[] valLocal = new byte[] { }; byte[] valOther = new byte[] { }; bool ok_value = true; try { if (this.valueType != RegType.BOOL) { valLocal = this.ValueByteArrayRaw; valOther = await other.GetValueByteArray(); ok_value = valLocal.SequenceEqual(valOther); Utils.Print("Equals: " + this.id + " -> " + Utils.ByteArrayToString(valLocal) + " == " + Utils.ByteArrayToString(valOther) + " = " + ok_value); } else { T bitLocal = this.ValueRaw; T bitOther = await other.GetValueFromMtu(); ok_value = (bool.Equals(bitLocal, bitOther)); Utils.Print("Equals: " + this.id + " -> " + bitLocal + " == " + bitOther + " = " + ok_value); } } catch (Exception e) { return(false); } return(ok_id && ok_description && ok_address && ok_size && ok_write && ok_value); }
// Convert hexadecimal number to integer value // Use with <CustomSet>method:HexToInt</CustomSet> public int HexToInt(MemoryRegister <int> MemoryRegister, dynamic inputValue) { if (inputValue is string) // Removes 0x prefix { return(int.Parse(inputValue.Substring(2), NumberStyles.HexNumber)); } else { return(( int )inputValue); } }
public void CreateProperty_Set_String <T> (MemoryRegister <T> memoryRegister) { base.AddMethod(METHODS_SET_STRING_PREFIX + memoryRegister.id, new Action <string>((_value) => { // String field with format to apply if (memoryRegister.HasCustomFormat_Set) { _value = this.ApplyFormat(_value, memoryRegister.format_Set); } this.SetStringToMem <T> (_value, memoryRegister.address, memoryRegister.size); })); }
public int ReadIntervalMinutes_Set(MemoryRegister <int> MemoryRegister, dynamic inputValue) { string[] readIntervalArray = ((string)inputValue).Split(' '); string readIntervalStr = readIntervalArray[0].ToLower(); string timeUnit = readIntervalArray[1].ToLower(); int timeIntervalMins = Int32.Parse(readIntervalStr); if (timeUnit.StartsWith("hour") || timeUnit.StartsWith("hr")) { timeIntervalMins = timeIntervalMins * 60; } return(timeIntervalMins); }
public void AddElement <T> (MemoryRegister <T> register) { switch (Type.GetTypeCode(typeof(T))) { case TypeCode.Int32: this.dictionary[RegType.INT].Add(register); break; case TypeCode.UInt32: this.dictionary[RegType.UINT].Add(register); break; case TypeCode.UInt64: this.dictionary[RegType.ULONG].Add(register); break; case TypeCode.Boolean: this.dictionary[RegType.BOOL].Add(register); break; case TypeCode.Char: this.dictionary[RegType.CHAR].Add(register); break; case TypeCode.String: this.dictionary[RegType.STRING].Add(register); break; } }
public bool Equals(MemoryRegister <T> other) { if (other == null) { return(false); } bool ok_id = string.Equals(this.id, other.id); bool ok_description = string.Equals(this.description, other.description); bool ok_address = (this.address == other.address); bool ok_size = (this.size == other.size); bool ok_write = (this.write == other.write); bool ok_value = object.Equals(this.ValueRaw, other.ValueRaw); return(ok_id && ok_description && ok_address && ok_size && ok_write && ok_value); }
// Use with <CustomGet>method:ULongToBcd</CustomGet> public async Task <ulong> BcdToULong(MemoryRegister <ulong> MemoryRegister) { byte[] bytes = await MemoryRegister.GetValueByteArray(); string outNum = string.Empty; foreach (byte b in bytes) { outNum += b.ToString("X"); } outNum = outNum.TrimEnd(new char[] { 'F' }); outNum = outNum .Replace("A", "10") .Replace("B", "11") .Replace("C", "12") .Replace("D", "13") .Replace("E", "14") .Replace("F", "15"); return(ulong.Parse(outNum)); }
// Use with <CustomGet>method:ULongToBcd</CustomGet> public ulong BcdToULong(MemoryRegister <ulong> MemoryRegister) { byte[] bytes = MemoryRegister.ValueByteArray; string outNum = string.Empty; foreach (byte b in bytes) { outNum += b.ToString("X"); } outNum = outNum.TrimEnd(new char[] { 'F' }); outNum = outNum .Replace("A", "10") .Replace("B", "11") .Replace("C", "12") .Replace("D", "13") .Replace("E", "14") .Replace("F", "15"); ulong a = ulong.Parse(outNum); return(a); }
// Using custom method we can't be assured of the type used for parameter, but yes about return value // Custom header format: public T RegisterId_Get|CustomId ( MemoryRegister<T> MemoryRegister, dynamic inputValue ) public void CreateProperty_Set <T>(MemoryRegister <T> memoryRegister) { #region Normal Set base.AddMethod(METHODS_SET_PREFIX + memoryRegister.id, new Action <T>((_value) => { // Numeric field with operation to evaluate if (memoryRegister.HasCustomOperation_Set) { _value = this.ExecuteOperation <T> (memoryRegister.mathExpression_Set, _value); } switch (Type.GetTypeCode(typeof(T))) { case TypeCode.Int32: this.SetIntToMem((int )(object)_value, memoryRegister.address, memoryRegister.size); break; case TypeCode.UInt32: this.SetUIntToMem((uint )(object)_value, memoryRegister.address, memoryRegister.size); break; case TypeCode.UInt64: this.SetULongToMem((ulong)(object)_value, memoryRegister.address, memoryRegister.size); break; case TypeCode.Boolean: this.SetBoolToMem((bool )(object)_value, memoryRegister.address, memoryRegister.size); break; case TypeCode.Char: this.SetCharToMem((char )(object)_value, memoryRegister.address); break; //case TypeCode.String : this.SetStringToMem(TypeCode.String, (string)(object)_value, regObj.address); break; } })); #endregion #region Custom Set previous to Normal Set // But only someone have special set block/method defined on MTU family classes if (memoryRegister.HasCustomMethod_Set) { MethodInfo customMethod = this.GetType().GetMethod( memoryRegister.methodId_Set, BindingFlags.Instance | BindingFlags.IgnoreReturn | BindingFlags.NonPublic | BindingFlags.Public); //new Type[] { typeof( MemoryRegister<T> ), typeof ( dynamic } ); // Method is not present in MTU family class if (customMethod == null) { string strError = EXCEP_REGI_METHOD.Replace("#", memoryRegister.methodId_Set); Console.WriteLine("Create Custom Set " + memoryRegister.id + ": Error - " + strError); throw new CustomMethodNotExistException(strError); } base.AddMethod(METHODS_SET_CUSTOM_PREFIX + memoryRegister.id, new Func <dynamic, dynamic>((_value) => { return(customMethod.Invoke(this, new object[] { memoryRegister, _value })); })); } #endregion }
// Custom header format: public T RegisterId_Get|CustomId ( MemoryRegister<T> MemoryRegister ) private void CreateProperty_Get <T> (MemoryRegister <T> memoryRegister) { #region Normal Get // All register have normal get block base.AddMethod(METHODS_GET_PREFIX + memoryRegister.id, new Func <T>(() => { object result = default(T); switch (Type.GetTypeCode(typeof(T))) { case TypeCode.Int32: result = ( object )this.GetIntFromMem(memoryRegister.address, memoryRegister.size); break; case TypeCode.UInt32: result = ( object )this.GetUIntFromMem(memoryRegister.address, memoryRegister.size); break; case TypeCode.UInt64: result = ( object )this.GetULongFromMem(memoryRegister.address, memoryRegister.size); break; case TypeCode.Boolean: result = ( object )this.GetBoolFromMem(memoryRegister.address, memoryRegister.bit); break; case TypeCode.Char: result = ( object )this.GetCharFromMem(memoryRegister.address); break; case TypeCode.String: result = ( object )this.GetStringFromMem(memoryRegister.address, memoryRegister.size); break; } // Numeric field with operation to evaluate if (memoryRegister.HasCustomOperation_Get) { return(this.ExecuteOperation <T> (memoryRegister.mathExpression_Get, result)); } // String field with format to apply else if (memoryRegister.HasCustomFormat_Get) { return(( T )( object )this.ApplyFormat(( string )result, memoryRegister.format_Get)); } // Only return readed value return(( T )result); })); #endregion #region Custom Get that internally uses Normal Get // But only someone have special get block/method defined on MTU family classes if (memoryRegister.HasCustomMethod_Get) { MethodInfo customMethod = this.GetType().GetMethod( memoryRegister.methodId_Get, new Type[] { typeof(MemoryRegister <T>) }); // Method is not present in MTU family class if (customMethod == null) { string strError = EXCEP_REGI_METHOD.Replace("#", memoryRegister.methodId_Get); Console.WriteLine("Create Custom Get " + memoryRegister.id + ": Error - " + strError); throw new CustomMethodNotExistException(strError); } base.AddMethod(METHODS_GET_CUSTOM_PREFIX + memoryRegister.id, new Func <T>(() => { return(( T )customMethod.Invoke(this, new object[] { memoryRegister })); })); } #endregion }
// Use with <CustomSet>method:ULongToBcd</CustomSet> public async Task <byte[]> ULongToBcd(MemoryRegister <ulong> MemoryRegister, dynamic inputValue) { return(this.ULongToBcd_Logic(inputValue.ToString(), MemoryRegister.size)); }