private Item GetItem(XElement element) { Item item = null; string itemType = GetAttribute(element, "Type"); string name = GetAttribute(element, "Name"); short index = short.Parse(GetAttribute(element, "SortIndex")); switch (itemType) { case "ByteArrayByteCrcItem": int crcFromIndex = int.Parse(GetAttribute(element, "CrcFromIndex")); int crcToIndex = int.Parse(GetAttribute(element, "CrcToIndex")); item = new ByteArrayByteCrcItem(name, index, crcFromIndex, crcToIndex); break; case "ByteArrayByteItem": item = new ByteArrayByteItem(name, index); foreach (var subItemElement in element.Elements("BitItem")) { string bitItemName = GetAttribute(subItemElement, "Name"); int bitItemLength = int.Parse(GetAttribute(subItemElement, "Length")); ((ByteArrayByteItem)item).AddBitItem(bitItemName, bitItemLength); } break; case "ByteArrayCompositeValueItem": item = new ByteArrayCompositeValueItem(name, index); foreach (var subItemElement in element.Elements("Item")) { Item subItem = GetItem(subItemElement); ((ByteArrayCompositeValueItem)item).AddItem((ValueItem<byte[], DataItem>)subItem); } break; case "ByteArrayDoubleItem": item = new ByteArrayDoubleItem(name, index); break; case "ByteArrayInt16Item": item = new ByteArrayInt16Item(name, index); break; case "ByteArrayInt32Item": item = new ByteArrayInt32Item(name, index); break; case "ByteArrayInt64Item": item = new ByteArrayInt64Item(name, index); break; case "ByteArrayLoopItem": ByteArrayCompositeValueItem itemByteArrayCompositeValue = null; foreach (var subItemElement in element.Elements("Item")) { if (GetAttribute(subItemElement, "Type") == "ByteArrayCompositeValueItem") { itemByteArrayCompositeValue = (ByteArrayCompositeValueItem)GetItem(subItemElement); break; } } if (itemByteArrayCompositeValue == null) { throw new ElementNotFoundException("Element ByteArrayCompositeValueItem Item not found"); } item = new ByteArrayLoopItem(name, index, itemByteArrayCompositeValue); break; case "ByteArrayStringItem": int byteCount = int.Parse(GetAttribute(element, "ByteCount")); string encoding = GetAttribute(element, "Encoding"); item = new ByteArrayStringItem(name, index, byteCount, Encoding.GetEncoding(encoding)); break; case "SimpleStringValueItem": item = new SimpleStringValueItem(name, index); break; case "CustomItem": { string customTypeName = GetAttribute(element, "CustomTypeName"); Type type = Type.GetType(customTypeName); object[] args = null; if (element.Elements("Parameter") != null) { args = new object[element.Elements("Parameter").Count() + 2]; int i = 0; args[0] = name; args[1] = index; foreach (var paraElement in element.Elements("Parameter")) { string pType = GetAttribute(paraElement, "Type"); string pValue = GetAttribute(paraElement, "Value"); object p = Convert.ChangeType(pValue, Type.GetType(pType)); args[i + 2] = p; i++; } } item = (Item)System.Activator.CreateInstance(type, args); break; } default: throw new UnknownElementException("Unknown item type:" + itemType); } _adapterObjects[_currentAdapterName].Add(item.Name, item); return item; }
public void StartServer() { TcpServerChannel tcpServerChannel = new TcpServerChannel("TcpServerChannel", 9988); SingleFormatterByteArrayInterpreter interpreter = new SingleFormatterByteArrayInterpreter("Interpreter"); interpreter.SetHeaders(new byte[] { 0x55, 0xAA }); interpreter.SetTailers(new byte[] { 0xAA, 0x55 }); ByteArrayFormatter formatter = new ByteArrayFormatter("Formatter1"); formatter.AddItem(new ByteArrayStringItem("NodeName", 1, 6, Encoding.ASCII)); formatter.AddItem(new ByteArrayInt32Item("NodeId", 0)); formatter.AddItem(new ByteArrayInt16Item("Temperature", 2)); formatter.AddItem(new ByteArrayDoubleItem("Longitude", 3)); ByteArrayByteItem babi = new ByteArrayByteItem("Flags", 4); babi.AddBitItem("Flag1", 1); babi.AddBitItem("Flag2", 3); babi.AddBitItem("Flag3", 4); formatter.AddItem(babi); interpreter.AddFormatter(formatter); ByteArrayAdapter baa = new ByteArrayAdapter("ByteArrayAdapter", tcpServerChannel, interpreter, Program.ShowEnvelop); baa.Setup(); }