天天看点

IFIX VBA的几个小窍门

 1、从后台调度中访问或修改全局变量的代码

描述:

在iFIX中使用调度要后台运行的时候需要启动FixBackgroundServer程序,而该程序实际上是启动了独立于Workspace的进程,所以在后台调度中不能直接访问全局变量。下面的例子代码描述了怎样访问的方法:

" First get a pointer to the Application object in the Workspace

Dim WrkSpcApp As Object

Set WrkSpcApp = GetObject("", "Workspace.Application")

" Get a pointer to the Variable object called Variable1 in the User globals

Dim TargetVar As Object

Set TargetVar = WrkSpcApp.Documents("User").Page.FindObject("Variable1")

" Set the current value in the variable object to a 1

TargetVar.CurrentValue = 1

2、脉冲输出示例

Dim PauseTime, Start

CloseDigitalPoint "Fix32.FIX.DDD.F_CV"

PauseTime = 0.8  " 设置暂停时间。

  Start = Timer  " 设置开始暂停的时刻。

  Do While Timer < Start + PauseTime

    DoEvents  " 将控制让给其他程序。

  Loop

OpenDigitalPoint "Fix32.FIX.DDD.F_CV"

3、显示通讯状态

Is there a way to view communication status of the driver without using Mission Control?

Resolution

Using a digital input block reading a hardware register, the A_cualm field will be utilized. Chain the DI block to an event action block to a digital output tag using the SIM driver. In the event action block use the following script:

IF the A_cualm =com

Then set DO =1

Else set the DO=0

A data link can be made to the Sim block. The value of this block will determine the status of the communcation.

4、用VBA脚本打开和关闭调度

为了打开和关闭调度,必须用下列方法:

打开一个名字为“ddd”的调度:

Application.Documents.Open "C:/Dynamics/pdb/ddd.evs"

关闭一个名字为“ddd”的调度:

Application.Documents("ddd").Close

注意:关闭调度时不用扩展名 .evs 。

5、在后台调度中访问画面中的对象

在调度中中的脚本不能直接访问画面中的对象。

办法:

使用FindObject方法:

Dim AppObj as Object

Dim PicObj as Object

set AppObj = GetObject ("","Workspace.Application")

set PicObj = AppObj.System.FindObject ("Pic.Obj")

PicObj.Value = 5

Pic为画面的名字,picObj 为所用的对象的属性或方法。

6、怎样用程序控制7.X版本的驱动程序的datablocks, devices, or channels扫描的启停

在应用过程中可能希望灵活的控制驱动程序中的某一个Channel或Device或Datablock,以下介绍两种方法:

6.1.通过编写脚本:

本例子是用脚本来控制第一个Channel的第一个Device的第一个Datablock的,如果你想控制Channel或Device,只需将channel handle或device handle象本例中的Datablock赋给SetPropertyData function即可:

Dim objDriver As Object

Dim strDRVAcronym As String

Dim strProgId As String

"Variables for GetChannel

Dim lngNumChannels As Long

Dim lngChanHandles

Dim strChanNames

"Variables for GetDevices

Dim lngNumDevices As Long

Dim lngDeviceHandles

Dim strDeviceNames

"Variables for GetDataBlocks

Dim lngNumDataBlocks As Long

Dim lngDataBlockHandles

Dim strDataBlockNames

Dim lngErrors

Dim myvar As Long

strDRVAcronym = InputBox("Please enter the driver""s three letter acronym?" & vbCrLf & "(For Example: Enter ABR for the ABR Driver)", "What Driver are you using?")

strProgId = "Intellution." & strDRVAcronym & "drv"

Set objDriver = CreateObject(strProgId)

lngNumChannels = objDriver.GetChannels(lngChanHandles, strChanNames)

lngNumDevices = objDriver.GetDevices(lngChanHandles(0), lngDeviceHandles, strDeviceNames)

lngNumDataBlocks = objDriver.GetDataBlocks(lngDeviceHandles(0), lngDataBlockHandles, strDataBlockNames)

""Build Variables for SetPropertyData function.

lngHandle = lngDataBlockHandles(0)

varProperties = "Enabled"

varProperyData = "1"

lngErrors = objDriver.SetPropertyData(lngHandle, varProperties, varProperyData)

Set objDriver = Nothing

6.2.大多7.X的驱动程序都有一些特殊的控制地址可以用来控制驱动程序中的Server,group,item的扫描状态,可以查看相应驱动程序的帮助中以"惊叹号"开头的一些特殊地址:

!MODE - Enables polling to the server, group, or item specified when a value of 1 is written to a Digital Output block (Analog Output blocks are not supported). Disabling a specified object occurs when a value of 0 is written to a Digital Output block.

You can create a database block with the address !MODE:Datablock1 and trigger it manually, through VBA code, or through a Program Block.

7、用VBA在一个基于时间的后台调度中计算偏差的例子

你可能想计算在一个指定的时间段内开始时间和结束时间的偏差,在后台调度中用一个定时器来实现

方法:

本例为怎么样取在一个特定时间的开始和结束时的值并计算差值。它用了两个全局变量和一个基于时间的后台的调度:

1. 创建两个全局变量: "StartSample" 和 "Initialized"".

2. 设置变量的属性: "StartSample" -- "Long" ;"Initialized" -- "Boolean"。 其中 "Initialized" 初始值为 "False"。

3. 创建一个基于时间的调度,时间间隔为 00:00:10。

4. 用自定义脚本并写入以下脚本。

5. 确认改变tag reference到相应的node 和 tag。

6. 脚本写好后保存调度并设置为用后台运行调度。

7. 例子启动后会弹出对话框表面例子初始化了。

8. 之后该对话框会显示起始值,终止值和该段时间两者的差值。

注意:不要忘了将FixBackgroundServer.exe 加到启动任务配置中去。

脚本:

-----------------------------------------------------------------------------------------------------------

Private Sub FixTimer3_OnTimeOut(ByVal lTimerId As Long)

Dim objWrkSpc As Object

Dim objStartVar As Object

Dim objInit As Object

Dim lngEndVar As Long

Dim lngDelta As Long

’get Workspace object and global variables contained in workspace

Set objWrkSpc = GetObject("", "Workspace.Application")

Set objStartVar = objWrkSpc.Documents("User").Page.FindObject("StartSample")

Set objInit = objWrkSpc.Documents("User").Page.FindObject("Initialized")

"Check to see if this is the first time the script is being fired.

"If so, initialize the StartSample variable with a value from the desired tag

"and set the Initialized flag to true. If the script has been fired once then

"take the end sample, calculate the difference and then store a new start sample.

If objInit.CurrentValue = True Then

lngEndVar = readvalue("Fix32.NODE.TAG.F_CV")

lngDelta = lngEndVar - objStartVar.CurrentValue

MsgBox "START: " & objStartVar.CurrentValue & " END: " & lngEndVar & _

" DIFFERENCE: " & lngDelta

objStartVar.CurrentValue = readvalue("Fix32.NODE.TAG.F_CV")

Else

objStartVar.CurrentValue = readvalue("Fix32.NODE.TAG.F_CV")

MsgBox "StartSample initialized to " & objStartVar.CurrentValue

objInit.CurrentValue = True

End If

"Destroy objects

Set objWrkSpc = Nothing

Set objStartVar = Nothing

Set objInit = Nothing

End Sub

 8、VB中读取iFIX点

This sample code will read data from an iFIX tag.

"declare a few objects

Dim objWorkspace As Object

Dim objDataItem As Object

"get reference to the workspace

Set objWorkspace = CreateObject("Workspace.Application")

"use the workspace.system object to obtain the OODB dataitem

Set objDataItem = objWorkspace.System.FindObject("Fix32.FIX.AI1.F_CV")

"display the value in a msgbox

MsgBox objDataItem.Value

objDataItem.Value = "75"

9、有没有办法使客户端浏览节点上的画面加载速度加快?

回答:通过调整 FixUserPreferences.ini 文件中的参数设置可以让你精确调整 VBA 脚本语言的运行性能,并加快画面在浏览节点上的打开速度。

FixUserPreferences.ini 文件在目录/Dynamics/Local 下,在此文件的 [Scripting] 部分参数ScriptIdleTimeProcessingDelayCount 缺省值设置在 –1。

在这一缺省设置下,你的 VBA 脚本语言的执行将优先于画面的打开,若将此设定改为 0 或 1,你可以提高画面的打开速度。你可以使用的一个办法是在你的 SCADA节点上将参数设为 –1,使 VBA脚本运行速度较快,而在客户端将参数设为 0 或 1,加快画面的打开速度。

注意:不要将参数设为大于 1,这并不能提高你的系统性能。

10、语音报警

    大多数时候大家都是在调度里面做,另一个方法是:

    在ALARMSYUMMAYR控件中,启动报警。

    在ALARMSYUMMAYR控件启用newalarm事件,可以在这个时间里实现语音报警。

    在大量语音报警的情况下,比用调度省事多了。

    选择异步函数,可以在多个报警同时来临时,把所有的语音都报出来。   

11、调用API

    很多时候调用API函数,比自己编方便多了,如Sleep。

    在某些情况下,你或许要使用共享内存,这样的话,使用API是最好的选择了。