首页 > 开发 > 综合 > 正文

OPC客户程序(VB篇——异步)

2024-07-21 02:20:55
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 建立如下窗体:

    引用如下:

    代码如下:


    option explicit
    option base 1


    const writeasync_id = 1
    const readasync_id = 2
    const refreshasync_id = 3

    '----------------------------------------------------------------------------
    ' interface objects
    '----------------------------------------------------------------------------
    public withevents serverobj as opcserver
    public withevents groupobj as opcgroup

    dim itemobj1 as opcitem
    dim itemobj2 as opcitem

    dim serverhandle(2) as long

    private sub chkgroupactive_click()

    if chkgroupactive = 1 then
    groupobj.isactive = 1
    else
    groupobj.isactive = 0
    end if
    end sub

    private sub command_start_click()

    dim outtext as string

    on error goto errorhandler

    command_start.enabled = false
    command_read.enabled = true
    command_write.enabled = true
    command_exit.enabled = true
    chkgroupactive.enabled = true

    outtext = "连接opc服务器"
    set serverobj = new opcserver
    serverobj.connect ("xxxserver")

    outtext = "添加组"
    set groupobj = serverobj.opcgroups.add("group")


    groupobj.issubscribed = true

    chkgroupactive_click

    outtext = "添加item"
    set itemobj1 = groupobj.opcitems.additem("xxxitem1", 1)
    set itemobj2 = groupobj.opcitems.additem("xxxitem2", 2)

    serverhandle(1) = itemobj1.serverhandle
    serverhandle(2) = itemobj2.serverhandle

    exit sub


    errorhandler:
    msgbox err.description + chr(13) + _
    outtext, vbcritical, "error"


    end sub

    private sub command_read_click() '异步读

    dim outtext as string
    dim myvalue as variant
    dim myquality as variant
    dim mytimestamp as variant
    dim clientid as long
    dim serverid as long
    dim errornr() as long
    dim errorstring as string

    on error goto errorhandler
    outtext = "读值"

    clientid = readasync_id
    groupobj.asyncread 1, serverhandle, errornr, clientid, serverid
    if errornr(1) <> 0 then
    errorstring = serverobj.geterrorstring(errornr(1))
    msgbox errorstring, vbcritical, "error asyncread()"
    end if

    erase errornr
    exit sub

    errorhandler:
    msgbox err.description + chr(13) + _
    outtext, vbcritical, "error"

    end sub

    private sub command_write_click() '异步写

    dim outtext as string
    dim serverhandles(1) as long
    dim myvalues(1) as variant
    dim errornr() as long
    dim errorstring as string
    dim cancel_id as long

    outtext = "writing value"
    on error goto errorhandler


    myvalues(1) = edit_writeval

    groupobj.asyncwrite 1, serverhandle, myvalues, errornr, writeasync_id, cancel_id

    if errornr(1) <> 0 then
    errorstring = serverobj.geterrorstring(errornr(1))
    msgbox errorstring, vbcritical, "error asyncread()"
    end if

    erase errornr
    exit sub

    errorhandler:
    msgbox err.description + chr(13) + _
    outtext, vbcritical, "error"

    end sub


    private sub command_exit_click() '停止
    dim outtext as string

    on error goto errorhandler

    command_start.enabled = true
    command_read.enabled = false
    command_write.enabled = false
    command_exit.enabled = false
    chkgroupactive.enabled = false

    outtext = "removing objects"
    set itemobj1 = nothing
    set itemobj2 = nothing
    serverobj.opcgroups.removeall
    set groupobj = nothing
    serverobj.disconnect
    set serverobj = nothing

    exit sub

    errorhandler:
    msgbox err.description + chr(13) + _
    outtext, vbcritical, "error"

    end sub


    '异步读回调
    private sub groupobj_asyncreadcomplete(byval transactionid as long, byval numitems as long, clienthandles() as long, itemvalues() as variant, qualities() as long, timestamps() as date, errors() as long)
    dim errorstring as string

    if (transactionid = readasync_id) then
    if errors(1) = 0 then
    edit_readval = itemvalues(1)
    edit_readqu = getqualitytext(qualities(1))
    edit_readts = timestamps(1)
    else
    errorstring = serverobj.geterrorstring(errors(1))
    msgbox errorstring, vbcritical, "error asyncreadcomplete()"
    end if
    end if
    end sub

    '异步写回调
    private sub groupobj_asyncwritecomplete(byval transactionid as long, byval numitems as long, clienthandles() as long, errors() as long)
    dim errorstring as string

    if (transactionid = writeasync_id) then
    if errors(1) = 0 then
    edit_writeres = serverobj.geterrorstring(errors(1))
    else
    errorstring = serverobj.geterrorstring(errors(1))
    msgbox errorstring, vbcritical, "error asyncwritecomplete()"
    end if
    end if
    end sub
    '回调
    private sub groupobj_datachange(byval transactionid as long, byval numitems as long, clienthandles() as long, itemvalues() as variant, qualities() as long, timestamps() as date)

    dim i as long

    for i = 1 to numitems
    edit_ondataval(i - 1) = itemvalues(i)
    edit_ondataqu(i - 1) = getqualitytext(qualities(i))
    edit_ondatats(i - 1) = timestamps(i)

    next i

    end sub


    private function getqualitytext(quality) as string

    select case quality
    case 0: getqualitytext = "bad"
    case 64: getqualitytext = "uncertain"
    case 192: getqualitytext = "good"
    case 8: getqualitytext = "not_connected"
    case 13: getqualitytext = "device_failure"
    case 16: getqualitytext = "sensor_failure"
    case 20: getqualitytext = "last_known"
    case 24: getqualitytext = "comm_failure"
    case 28: getqualitytext = "out_of_service"
    case 132: getqualitytext = "last_usable"
    case 144: getqualitytext = "sensor_cal"
    case 148: getqualitytext = "egu_exceeded"
    case 152: getqualitytext = "sub_normal"
    case 216: getqualitytext = "local_override"

    case else: getqualitytext = "unknown error"
    end select

    end function





    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表