private Message LoadMessage(Conversation conv, MessageSettings settings) { MessageType type = settings.Type; if (type == MessageType.ValueCount) return null; Message message = new Message(conv, type, settings.Name); foreach (MessageSettings.ElementListValue element in settings.ElementList) { if (element.Type == "BYTE") { if (element.ValueString != null) { message.Elements.Add(new Literal(element.ValueString, conv, element.ExcludeFromChecksum)); } else { UseVariable useVar = new UseVariable(conv, element.Name, element.Type, element.Size, element.ExcludeFromChecksum); message.Elements.Add(useVar); if (element.SizeName != null) { try { ByteVar sizeVar = (ByteVar)conv.GetVariable(element.SizeName); sizeVar.AddResizeVariable((ByteVar)useVar.Variable); } catch (Exception) // exceptions are caused by incorrect element types - both must be ByteVar { } } } } else if (element.Type == "DYNAMICBYTE") { UseVariable useVar = new UseVariable(conv, element.Name, element.Type, element.Size, element.ExcludeFromChecksum); message.Elements.Add(useVar); if (element.SizeName != null) { try { ByteVar sizeVar = (ByteVar)conv.GetVariable(element.SizeName); sizeVar.AddResizeVariable((ByteVar)useVar.Variable); } catch (Exception) // exceptions are caused by incorrect element types - both must be ByteVar { } } } else if (element.Type == "STRING") { UseVariable useVar = new UseVariable(conv, element.Name, element.Type, element.Size, element.ExcludeFromChecksum); message.Elements.Add(useVar); } } return message; }
public UseVariable(Conversation conversation, String varName, String type = null, int? size = null, bool excludeFromChecksum = false, int messageId = 0) : base(conversation, excludeFromChecksum) { Converse session = conversation.Converse; int? variableSize; Variable = conversation.GetVariable(varName); VariableType = type; variableSize = size; Calculations = session.Calculations; IsChecksum16 = (varName.ToUpper() == "CHECKSUM16"); IsChecksum8 = (varName.ToUpper() == "CHECKSUM8"); IsChecksum = IsChecksum16 || IsChecksum8; if (Variable == null) // variable not defined if (type == null) { Variable = new StringVar(varName, conversation); session.Variables.Add(Variable); } else if (type == "BYTE") { Variable = new ByteVar(varName, variableSize, conversation); session.Variables.Add(Variable); } else if (type == "DYNAMICBYTE") { if (variableSize == null || variableSize < 1) variableSize = 10000; Variable = new DynamicByteVar(varName, variableSize.Value, conversation); session.Variables.Add(Variable); } else if (type == "STRING") { Variable = new StringVar(varName, conversation); session.Variables.Add(Variable); } else throw new Exception("Variable " + varName + " type not supported: " + type); }