public async Task SendKnxMessage() { var target = new KnxNetIpRoutingClient(); try { await target.Connect(); var message = new KnxMessage { MessageType = MessageType.Write, MessageCode = MessageCode.Request, Priority = MessagePriority.Auto, SourceAddress = new KnxDeviceAddress(1, 1, 2), DestinationAddress = new KnxLogicalAddress(1, 1, 28), TransportLayerControlInfo = TransportLayerControlInfo.UnnumberedDataPacket, DataPacketCount = 0, Payload = new DptBoolean(false).Payload }; await target.SendMessage(message); } finally { target.Dispose(); } }
public async Task SendKnxMessage() { var target = new KnxNetIpTunnelingClient(new IPEndPoint(IPAddress.Parse("10.10.10.11"), 3671), KnxAddress.Device(1, 1, 2)); try { await target.Connect(); var message = new KnxMessage { MessageType = MessageType.Write, MessageCode = MessageCode.Request, Priority = MessagePriority.Auto, SourceAddress = new KnxDeviceAddress(1, 1, 2), DestinationAddress = new KnxLogicalAddress(1, 1, 28), TransportLayerControlInfo = TransportLayerControlInfo.UnnumberedDataPacket, DataPacketCount = 0, Payload = new DptBoolean(false).Payload }; await target.SendMessage(message); // test for simpler SendMessage calls //target.Write(KnxAddress.Logical(9, 3, 0), (new DptTime(new TimeSpan(13, 36, 00), DayOfWeek.Monday))); } finally { target.Dispose(); } }
/// <summary> /// Deserializes the specified bytes. /// </summary> /// <param name="bytes">The bytes.</param> public override void Deserialize(byte[] bytes) { if (bytes[0] != 4) { throw new KnxException("Could not parse ConnectionRequest: " + bytes); } this.CommunicationChannel = bytes[1]; this.SequenceCounter = bytes[2]; // bytes[3] is reserved (should be always 0x00) // starting at byte index 4 this.Cemi = KnxMessage.Deserialize(bytes.ExtractBytes(4)); }
public static void Write(this KnxNetIpClient client, KnxLogicalAddress destination, DatapointType data, MessagePriority priority = MessagePriority.Auto) { var message = new KnxMessage { MessageCode = MessageCode.Request, MessageType = MessageType.Write, SourceAddress = client.DeviceAddress, DestinationAddress = destination, Payload = data.Payload, Priority = priority, }; client.SendMessage(message); }
public static void Reply(this KnxNetIpClient client, IKnxMessage replyTo, DatapointType data, MessagePriority priority = MessagePriority.Auto) { var request = replyTo; var message = new KnxMessage { MessageCode = MessageCode.Confirmation, MessageType = replyTo.MessageType, SourceAddress = client.DeviceAddress, DestinationAddress = request.SourceAddress, Payload = data.Payload, Priority = priority, }; client.SendMessage(message); }
public override void Deserialize(byte[] bytes) { Cemi = KnxMessage.Deserialize(bytes.ExtractBytes(0)); }
public static DatapointType Read(this KnxNetIpClient client, Type datapointTypeResultType, KnxLogicalAddress destination, MessagePriority priority = MessagePriority.Auto, TimeSpan timeOut = default) { var replyEvent = new AutoResetEvent(false); var indicationPayload = new byte[] { }; var confirmationPayload = new byte[] { }; var indicationMessage = default(IKnxMessage); var confirmationMessage = default(IKnxMessage); if (timeOut.TotalMilliseconds <= 0) { timeOut = client.Configuration.ReadTimeout; } try { // specify the reply condition void KnxMessageReceived(object sender, IKnxMessage knxMessage) { if (knxMessage.DestinationAddress.ToString() != destination.ToString()) { return; } var indicationCondition = (knxMessage.MessageCode == MessageCode.Indication) && (knxMessage.MessageType == MessageType.Reply); var confirmationCondition = knxMessage.MessageCode == MessageCode.Confirmation; // If we receive a confirmation, we cannot be sure that the payload correspond to the current actor value if (confirmationCondition) { confirmationMessage = knxMessage; confirmationPayload = new byte[knxMessage.PayloadLength]; knxMessage.Payload.CopyTo(confirmationPayload, 0); } // but, if we receive an indication, we can be sure, that the payload is the current actor value if (indicationCondition) { indicationMessage = knxMessage; indicationPayload = new byte[knxMessage.PayloadLength]; knxMessage.Payload.CopyTo(indicationPayload, 0); replyEvent.Set(); } } client.KnxMessageReceived += KnxMessageReceived; try { // create and send the read request var message = new KnxMessage { MessageCode = MessageCode.Request, MessageType = MessageType.Read, SourceAddress = client.DeviceAddress, DestinationAddress = destination, Priority = priority }; Debug.WriteLine("{0} START READING from {1}", DateTime.Now.ToLongTimeString(), destination); client.SendMessage(message); if (replyEvent.WaitOne(timeOut) && indicationMessage != null) { return((DatapointType)Activator.CreateInstance(datapointTypeResultType, indicationPayload)); } else { // Fallback, if we retrieved a confirmation, but no indication. if (confirmationMessage != null) { return((DatapointType)Activator.CreateInstance(datapointTypeResultType, confirmationPayload)); } throw new KnxTimeoutException(string.Format("Did not retrieve an answer within configured timeout of {0} seconds: {1}", timeOut.TotalSeconds, message)); } } finally { client.KnxMessageReceived -= KnxMessageReceived; Debug.WriteLine("{0} STOPPED READING of {1}", DateTime.Now.ToLongTimeString(), destination); } } finally { replyEvent.Dispose(); } }