public int Encode(ref byte[] buf, ref BACNET_ADDRESS dest, ref BACNET_NPDU_DATA npdu, int pdu_len) { UInt16 BVLC_length = 0; byte[] data = new byte[1024]; Array.Copy(buf, 0, data, 4, pdu_len); buf = data; buf[0] = 0x81; //BVLL_TYPE_BACNET_IP if (dest.net == 0xFFFF) //全局广播 { buf[1] = (byte)BACNET_BVLC_FUNCTION.BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK; } else if (dest.mac_len == 0) { buf[1] = (byte)BACNET_BVLC_FUNCTION.BVLC_ORIGINAL_BROADCAST_NPDU; } else if (dest.mac_len == 6) { buf[1] = (byte)BACNET_BVLC_FUNCTION.BVLC_ORIGINAL_UNICAST_NPDU; } BVLC_length = (UInt16)(pdu_len + 4); BasicalProcessor.Encode_Unsigned16(ref buf, BVLC_length, 2); return(BVLC_length); }
public static void Get_Broadcast_Address(ref BACNET_ADDRESS dest_address, Boolean local, UInt16 port) { dest_address.net = 0; dest_address.len = 6; for (int u = 0; u < 7; u++) { dest_address.adr[u] = 0; } dest_address.mac_len = 6; for (int i = 0; i < 4; i++) { dest_address.mac[i] = 255; } BasicalProcessor.Encode_Unsigned16(ref dest_address.mac, port, 4); if (!local) { dest_address.net = 0xFFFF; //远程广播 } }
public static void Get_My_Address(ref BACNET_ADDRESS my_address) { my_address.net = 0; my_address.len = 6; for (int i = 0; i < 7; i++) { } my_address.mac_len = 6; my_address.mac[0] = 192; my_address.mac[1] = 168; my_address.mac[2] = 110; my_address.mac[3] = 1; BasicalProcessor.Encode_Unsigned16(ref my_address.mac, 50, 4); my_address.adr[0] = 192; my_address.adr[1] = 168; my_address.adr[2] = 110; my_address.adr[3] = 1; BasicalProcessor.Encode_Unsigned16(ref my_address.adr, 50, 4); //测试用 }
public int Encode(ref byte[] npdu, ref BACNET_ADDRESS dest, ref BACNET_ADDRESS src, ref BACNET_NPDU_DATA npdu_data, int pos = 0) { int len = 0; /* return value - number of octets loaded in this function */ byte i = 0; /* counter */ if (npdu != null) { npdu[pos + 0] = npdu_data.protocol_version; //控制号 npdu[pos + 1] = 0; //控制信息 if (npdu_data.network_layer_message) { npdu[pos + 1] |= BacnetConst.BIT7; } //是否是网络型报文 if (dest.net != null) { npdu[pos + 1] |= BacnetConst.BIT5; } //0 表示DNET DLEN DADR HPO COUNT不存在 // 1 表示都存在, if (src.len != 0) { npdu[pos + 1] |= BacnetConst.BIT3; } //0代表SNET SLEN 和SADR都不存在 // 1表示都存在 if (npdu_data.data_expecting_reply) { npdu[pos + 1] |= BacnetConst.BIT2; } //是否需要回复 npdu[pos + 1] |= (byte)((byte)npdu_data.priority & 0x03); //优先级 len = 2; if (dest.len != 0) { len += BasicalProcessor.Encode_Unsigned16(ref npdu, dest.net, pos + len); npdu[pos + len] = dest.len; len++; //当DNET=FFFF 表示全局广播 //缺省表示本地广播 if (dest.len != 0) { for (i = 0; i < dest.len; i++) { npdu[pos + len++] = dest.adr[i]; } } } if (src.len != 0) { len += BasicalProcessor.Encode_Unsigned16(ref npdu, src.net, pos + len); npdu[pos + len] = src.len; len++; if (src.len != 0) { for (i = 0; i < src.len; i++) { npdu[pos + len++] = src.adr[i]; } } } if (dest.net != 0) { npdu[pos + len] = (byte)npdu_data.hop_count; len++; //转发计数 } if (npdu_data.network_layer_message) { npdu[pos + len] = (byte)npdu_data.network_message_type; len++; if ((int)npdu_data.network_message_type >= 0x80) { len += BasicalProcessor.Encode_Unsigned16(ref npdu, npdu_data.vendor_id, pos + len); } //报文类型 //0x80以后为用户扩展类型 } } return(len); }