Visual Basic开发BACnet IP主站程序方法介绍
这里要说的方法就是调用BACnet IP主站快速开发工具OMBACnetIPMaster.dll。
步骤1、创建通道,而通道管理所有的Bacnet属性对象。参数分别IP地址,端口号和网络超时时间,默认是100毫秒。
Private Sub OpenChannelButton_Click()
StringToByte txtIPAddress, IPAddress()
ChannelHandle(0) = CreateBacnetChannel(IPAddress(0), CLng(txtPort), 100)
End Sub
通道编辑操作的相关API函数声明如下
‘ 通道管理用于创建、修改、删除通道。创建通道以后,才能创建Bacnet属性对象
‘ 创建一个通道对象只要是为了设置不同设备的IP地址和端口号。网络超时等待时间默认是100毫秒
Function CreateBacnetChannel Lib 'OMBacnetIPMaster.dll' Alias '_CreateChannel@12' (ByRef IPAddress As Byte, ByVal nPort As Long, ByVal nTimeOut As Long) As Long
Function ConfigureChannel Lib 'OMBacnetIPMaster.dll' Alias '_ConfigureChannel@16' (ByVal ChannelHandle As Long, ByRef IPAddress As Byte, ByVal nPort As Long, ByVal nTimeOut As Long) As Long
Function RemoveChannel Lib 'OMBacnetIPMaster.dll' Alias '_RemoveChannel@4' (ByVal ChannelHandle As Long) As Long
步骤2、创建Bacnet属性对象。
Private Sub CreateBacnetObjButton_Click()
TagHandle(0) = CreateBacnetObj(ChannelHandle(0), 1, 1, 85)
End Sub
BACnet属性对象编辑相关的API函数申明如下
‘ BACnet对象管理,创建、修改、删除
‘参数 ChannelHandle 为通道对象的句柄
‘参数 nBacnetObjType 为Bacent属性对象的类型ID,譬如0表示 Analog Input,1表示Analog Output
‘参数 nInstanceNum 为指明哪一个对象,譬如要访问Analog Input 1。则 nInstanceNum为1
‘参数 nProperityID 为要访问属性的ID,譬如当前值的属性ID是85
‘参数 TagHandle 为Bacnet对象的句柄
Function CreateBacnetObj Lib 'OMBacnetIPMaster.dll' Alias '_CreateBacnetObject@16' (ByVal ChannelHandle As Long, ByVal nBacnetObjType As Long, ByVal nInstanceNum As Long, ByVal nProperityID As Long) As Long
Function ConfigureObject Lib 'OMBacnetIPMaster.dll' Alias '_ConfigureObject@16' (ByVal ChannelHandle As Long, ByVal nBacnetObjType As Long, ByVal nInstanceNum As Long, ByVal nProperityID As Long) As Long
Function RemoveBacnetObject Lib 'OMBacnetIPMaster.dll' Alias '_RemoveBacnetObject@4' (ByVal TagHandle As Long) As Long
步骤3、BACnet属性对象的读写
Private Sub WriteBacnetObjectButtton_Click()
error = WriteBacnetObj(TagHandle(0), CSng(Tag2.Text), 0)
End Sub
Private Sub ReadBacnetObjButton_Click()
Dim a As Variant
error = ReadBacnetObj(TagHandle(0), a)
Tag1.Text = a
End Sub
‘ BACnet对象属性值读写
‘参数 nTagNum 为Bacnet对象的Tag Number。也就是要写的对象属性的数据类型。当操作过ReadBacnet以后,这个参数可以为空。
‘当只写操作的时候,则需要用户自己指明是什么数据类型。譬如Analog Output为4.也就是单精度浮点型。
Function ReadBacnetObj Lib 'OMBacnetIPMaster.dll' Alias '_ReadBacnetObject@8' (ByVal TagHandle As Long, ByRef Value As Variant) As Long
Function WriteBacnetObj Lib 'OMBacnetIPMaster.dll' Alias '_WriteBacnetObject@12' (ByVal TagHandle As Long, ByRef Value As Variant, ByVal nTagNum As Long) As Long
步骤4、关闭程序时,释放对象资源。
Private Sub RemoveBacnetObjButton_Click()
error = RemoveBacnetObject(ByVal TagHandle(0))
End Sub
Private Sub RemoveChannelButton_Click()
error = RemoveChannel(ByVal ChannelHandle(0))
End Sub
最后讨论一下辅助函数的作用
‘以下是辅助函数,可以选择性使用
‘设置写操作的优先等级
Function SetBacnetPriority Lib 'OMBacnetIPMaster.dll' Alias '_SetBacnetPriority@8' (ByVal TagHandle As Long, ByVal nPriority As Long) As Long
‘返回Bacnet对象属性的数据类型,一般用于写操作
Function GetBacnetObjType Lib 'OMBacnetIPMaster.dll' Alias '_GetBacnetObjType@4' (ByVal TagHandle As Long) As Long
‘通信故障状况回调函数
Function EnableErrorMsgNofity Lib 'OMBacnetIPMaster.dll' Alias '_EnableErrorMsgNofity@4' (ByVal Callback As Long) As Long
Function EnableAbortMsgNofity Lib 'OMBacnetIPMaster.dll' Alias '_EnableAbortMsgNofity@4' (ByVal Callback As Long) As Long
Function EnableRejectMsgNofity Lib 'OMBacnetIPMaster.dll' Alias '_EnableRejectMsgNofity@4' (ByVal Callback As Long) As Long