public void StartServer() { TcpServerChannel channelTcpServer = new TcpServerChannel("TcpServerChannel", 9988); SingleFormatterByteArrayInterpreter interpreter = new SingleFormatterByteArrayInterpreter("Interpreter"); interpreter.SetTailers(new byte[] { 0xAA, 0x55 }); interpreter.SetHeaders(new byte[] { 0x55, 0xAA }); ByteArrayFormatter formatter = new ByteArrayFormatter("Formatter1"); ByteArrayCompositeValueItem byteArrayCompositeValueItem = new ByteArrayCompositeValueItem("ByteArrayCompositeValueItem1"); byteArrayCompositeValueItem.AddItem(new ByteArrayStringItem("NodeName", 1, 6, Encoding.ASCII)); byteArrayCompositeValueItem.AddItem(new ByteArrayInt32Item("NodeId", 0)); byteArrayCompositeValueItem.AddItem(new ByteArrayInt16Item("Temperature", 2)); byteArrayCompositeValueItem.AddItem(new ByteArrayDoubleItem("Longitude", 3)); byteArrayCompositeValueItem.AddItem(new ByteArrayByteItem("Reserved", 4)); ByteArrayLoopItem byteArrayLoopItem = new ByteArrayLoopItem("byteArrayLoopItem1", 1, byteArrayCompositeValueItem); formatter.AddItem(new ByteArrayInt32Item("Type", 0)); formatter.AddItem(byteArrayLoopItem); interpreter.AddFormatter(formatter); ByteArrayAdapter baa = new ByteArrayAdapter("ByteArrayAdapter", channelTcpServer, interpreter, Program.ShowEnvelop); baa.Setup(); }
public void StartServer() { TcpServerChannel channelTcpServer = new TcpServerChannel("TcpServerChannel", 9988); MultipleFormatterByteArrayInterpreter interpreter = new MultipleFormatterByteArrayInterpreter("Interpreter"); interpreter.SetTailers(new byte[] { 0xAA, 0x55 }); interpreter.SetHeaders(new byte[] { 0x55, 0xAA }); ///////////////////////////////////////////////////////////////////////////////////////// ByteArrayFormatter formatter_F1 = new ByteArrayFormatter("Formatter1", 1); ByteArrayCompositeValueItem byteArrayCompositeValueItem_F1 = new ByteArrayCompositeValueItem("F1_ByteArrayCompositeValueItem1"); byteArrayCompositeValueItem_F1.AddItem(new ByteArrayStringItem("F1_NodeName", 1, 6, Encoding.ASCII)); byteArrayCompositeValueItem_F1.AddItem(new ByteArrayInt32Item("F1_NodeId", 0)); byteArrayCompositeValueItem_F1.AddItem(new ByteArrayInt16Item("F1_Temperature", 2)); byteArrayCompositeValueItem_F1.AddItem(new ByteArrayDoubleItem("F1_Longitude", 3)); byteArrayCompositeValueItem_F1.AddItem(new ByteArrayByteItem("F1_Reserved", 4)); ByteArrayLoopItem byteArrayLoopItem_F1 = new ByteArrayLoopItem("F1_byteArrayLoopItem1", 1, byteArrayCompositeValueItem_F1); formatter_F1.AddItem(new ByteArrayInt32Item("F1_Type", 0)); formatter_F1.AddItem(byteArrayLoopItem_F1); ///////////////////////////////////////////////////////////////////////////////////////// ByteArrayFormatter formatter_F2 = new ByteArrayFormatter("Formatter2", 2); formatter_F2.AddItem(new ByteArrayInt32Item("F2_Type", 0)); formatter_F2.AddItem(new ByteArrayStringItem("F2_NodeName", 2, 6, Encoding.ASCII)); formatter_F2.AddItem(new ByteArrayInt32Item("F2_NodeId", 1)); formatter_F2.AddItem(new ByteArrayInt16Item("F2_Temperature", 3)); formatter_F2.AddItem(new ByteArrayDoubleItem("F2_Longitude", 4)); formatter_F2.AddItem(new ByteArrayByteItem("F2_Reserved", 5)); /////////////////////////////////////////////////////////////////////////////////////////////// ByteArrayTypedFormatterFilter byteArrayTypedFormatterFilter = new ByteArrayTypedFormatterFilter("ByteArrayTypedFormatterFilter", 0); //the index we use 0 meaning the Type Id is the index[0] in the input buffer without header. interpreter.AddFormatter(formatter_F1); interpreter.AddFormatter(formatter_F2); interpreter.AddFormatterFilter(byteArrayTypedFormatterFilter); ByteArrayAdapter baa = new ByteArrayAdapter("ByteArrayAdapter", channelTcpServer, interpreter, Program.ShowEnvelop); baa.Setup(); }
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); }
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; }