BACnet通讯1、概述.系统实现基于BACnet/IP(又称B/IP)网络进行通讯。BACnet虚拟链路层(BVLL)提供了BACnet网络层和某指定的通讯子系统的接口,本文指定了BACnet虚拟链路控制(BVLC)要求支持的定向和广播信息。本实现关于BACnet协议定义主要可分为三层:B/IP网络虚拟层,BACnet网络层和BACnet应用层。B/IP提供了TCP/IP一样的通讯结构,采用UDP的通讯方式。本文接下来将对此三层结构进行详细的讲解。其文本协议的大致格式如下:BACnet/IPBACnet网络层BACnet应用层BVLCTypeBVLCFunctionLengthNPDUAPDUNPDU:BACnet网络层数据结构,下文进行详细的讲解APDU:BACnet应用层数据结构,下文进行详细的讲解2、BACnet/IP.BVLCType:0x81,代表BACnet/IP网络BVLCFunction:指定报文的类型,何种作用;本应用使用的报文类型分为两种:0A:点对点通讯0B:广播通信Length:指定报文的长度,包括BVLCType、BVLCFunction以及本身在内NPDU:根据不同情况,报文长短不同,见下文的解析。APDU:根据NPDU不同以及不同的请求,报文不同,见下文的解析。3、BACnetNPDU结构.版本信息1ByteVersion控制字1ByteControl目标网络号2BytesDNET目标网络长度1ByteDLEN目标地址可变长度DADR原网络号2BytesSNET原网络长度1ByteSLEN原地址可变SNET路由数目1ByteHopCount消息类型1ByteMessageType卖方ID2BytesVendorID应用层信息可变APDU版本信息:默认0x01控制字:BIT7:1表示MessageType存在,为0时MessageType不存在BIT6:保留,为0BIT5:1表示DNETDLENHopCount存在,当DLEN为0时表明广播网络DADR不存在;0表示DNETDLENDADRHopCount不存在BIT4:保留,为0BIT3:1表示SNETSLEN存在,当SLEN为0时表明无效SADR不存在;0表示SNETSLENSADRHopCount不存在BIT2:1表示为一个需确认的请求数据单元,一复杂ACK数据单元或一网络层信息非reply的;0表示除去上述情形的数据单元BIT1、0:网络优先级11=LifeSafetymessage10=CriticalEquipmentmessage01=Urgentmessage00=Normalmessage目标网络号:FFFF的时候表示广播目标长度:表示DADR的长度目标地址:目标网络MAC地址原网络号:同目标网络号原网络长度:表示SADR的长度原地址:原网络的MAC地址路由数目:数据需要经过的路由个数,当不存在DNET的时候,需设置为FF消息类型:X'00':Who-Is-Router-To-NetworkX'01':I-Am-Router-To-NetworkX'02':I-Could-Be-Router-To-NetworkX'03':Reject-Message-To-NetworkX'04':Router-Busy-To-NetworkX'05':Router-Available-To-NetworkX'06':Initialize-Routing-TableX'07':Initialize-Routing-Table-AckX'08':Establish-Connection-To-NetworkX'09':Disconnect-Connection-To-NetworkX'0A'toX'7F':ReservedforusebyASHRAEX'80'toX'FF':Availableforvendorproprietarymessages卖方ID:当控制字的BIT7为1并且消息类型为X'80'toX'FF':的时候,才会存在本次开发没有用到,不再解释本次应用中控制字只用到0x20和0x04两种,前者为广播查询设备,后者微点对对取设备数据或属性。4、BACnetAPDU结构:(应用层协议数据单元)BACnet网络层数据主要分为一下几种:BACnetPDU::=CHOICE{confirmed-request-PDU[0]BACnet-Confirmed-Request-PDU,unconfirmed-request-PDU[1]BACnet-Unconfirmed-Request-PDU,simpleACK-PDU[2]BACnet-SimpleACK-PDU,complexACK-PDU[3]BACnet-ComplexACK-PDU,segmentAck-PDU[4]BACnet-SegmentACK-PDU,error-PDU[5]BACnet-Error-PDU,reject-PDU[6]BACnet-Reject-PDU,abort-PDU[7]BACnet-Abort-PDU}对于本次应用,主要用到了BACnet-Confirmed-Request-PDU(需确认的请求协议数据单元)BACnet-Unconfirmed-Request-PDU(无需确认的请求协议数据单元)BACnet-ComplexACK-PDU(复杂的ACK命令)BACnet-Error-PDU(报错的协议数据单元)对于上述几种数据的分类主要根据pdutype(下文进行讲解)进行判定。4.1.BACnet-Confirmed-Request-PDU的结构:BACnet-Confirmed-Request-PDU::=SEQUENCE{pdu-type[0]Unsigned(0..15),--0forthisPDUtypesegmented-message[1]BOOLEAN,more-follows[2]BOOLEAN,segmented-response-accepted[3]BOOLEAN,reserved[4]Unsigned(0..3),--mustbesettozeromax-segments-accepted[5]Unsigned(0..7),--asper20.1.2.4max-APDU-length-accepted[6]Unsigned(0..15),--asper20.1.2.5invokeID[7]Unsigned(0..255),sequence-number[8]Unsigned(0..255)OPTIONAL,--onlyifsegmentedmsgproposed-window-size[9]Unsigned(1..127)OPTIONAL,--onlyifsegmentedmsgservice-choice[10]BACnetConfirmedServiceChoice,service-request[11]BACnet-Confirmed-Service-RequestOPTIONAL}BIT7BIT6BIT5BIT4BIT3BIT2BIT1BIT0PDUTYPESEGMORSA00MaxSegsMaxRespInvokeIDSequenceNumber(onlypresentbySEG==1)ProposedWindowSize(onlypresentbySEG==1)ServiceChioceServiceRequest……PDUType0(BACnet-Confirmed-Service-Request-PDU)SEG0(UnsegmentedRequest)1(SegmentedRequest)MOR0(NoMoreSegmentsFollow)1(MoreSegmentsFollow)SA0(SegmentedResponsenotaccepted)1(SegmentedResponseaccepted)MaxSegs(0..7)(Numberofresponsesegmentsacceptedper20.1.2.4)MaxResp(0..15)(SizeofMaximumAPDUacceptedper20.1.2.5)InvokeID(0..255)SequenceNumber(0..255)OnlypresentifSEG=1ProposedWindowSize(1..127)OnlypresentifSEG=1ServiceChoiceBACnetConfirmedServiceChoiceServiceRequestVariableEncodingSEG:指出当前的数据单元是否为完整的还是为一部分分段信息MOR:指出是否还有更多的分段信息SA:为1时指出将收到一个复杂的回应(complexack)MaxSegs:指出设备将要接收到多少分段信息B'000'Unspecifiednumberofsegmentsaccepted.B'001'2segmentsaccepted.B'010'4segmentsaccepted.B'011'8segmentsaccepted.B'100'16segmentsaccepted.B'101'32segmentsaccepted.B'110'64segmentsaccepted.B'111'Greaterthan64segmentsaccepted.MaxResp:指出将收到的(APDU)最大长度B'0000'UptoMinimumMessageSize(50octets)B'0001'Upto128octetsB'0010'Upto206octets(fitsinaLonTalkframe)B'0011'Upto480octets(fitsinanARCNETframe)B'0100'Upto1024octetsB'0101'Upto1476octets(fitsinanISO8802-3frame)InvokeID:调用者IDServiceChioce:此处表明次报文的作用,详见BACnetConfirmedServiceChoiceServiceRequest:根据BACnetConfirmedServiceChoice不同而结构不同,详见BACnet-Confirmed-Service-RequestBACnetConfirmedServiceChoice::=枚举类型{--AlarmandEventServicesacknowledgeAlarm(0),confirmedCOVNotification(1),confirmedEventNotification(2),getAlarmSummary(3),getEnrollmentSummary(4),getEventInformation(29),subscribeCOV(5),subscribeCOVProperty(28),lifeSafetyOperation(27),--FileAccessServicesatomicReadFile(6),atomicWriteFile(7),--ObjectAccessServicesaddListElement(8),removeListElement(9),createObject(10),deleteObject(11),readProperty(12),readPropertyConditional(13),readPropertyMultiple(14),readRange(26),writeProperty(15),writePropertyMultiple(16),。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。}上述标记为红色的表示本次应用中所用到的,所以只对这些进行讲解。可以根据BACnetConfirmedServiceChoice找到相对应的Request或者Ack的数据的结构BACnet-Confirmed-Se