public void sendAPDU(Address recipient, OctetString link, IAPDU apdu, bool broadcast) { ByteStream queue = new ByteStream(); // BACnet virtual link layer detail // BACnet/IP queue.WriteByte(0x81); // Original-Unicast-NPDU, or Original-Broadcast-NPDU queue.WriteByte((byte)(broadcast ? 0xb : 0xa)); // NPCI ByteStream apduStream = new ByteStream(); writeNpci(apduStream, recipient, link, apdu); // APDU apdu.write(apduStream); // Length queue.WriteShort((ushort)(queue.Position + apduStream.Length + 2)); // Combine the queues queue.Write(apduStream); IPEndPoint isa; if (recipient.IsGlobal) { isa = LocalBroadcastAddress.MacAddress.InetSocketAddress; } else if (link != null) { isa = link.InetSocketAddress; } else { isa = recipient.MacAddress.InetSocketAddress; } this.link.SendPacket(isa, queue.ReadToEnd()); }
protected void writeNpci(ByteStream queue, Address recipient, OctetString link, IAPDU apdu) { NPDU.NPDU npci; if (recipient.IsGlobal) { npci = new NPDU.NPDU((Address)null); } else if (isLocal(recipient)) { if (link != null) { throw new System.Exception("Invalid arguments: link service address provided for a local recipient"); } npci = new NPDU.NPDU(null, null, apdu.expectsReply); } else { if (link == null) { throw new System.Exception( "Invalid arguments: link service address not provided for a remote recipient"); } npci = new NPDU.NPDU(recipient, null, apdu.expectsReply); } npci.write(queue); }