国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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





    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 襄城县| 成都市| 玉田县| 定襄县| 汝南县| 巴楚县| 吐鲁番市| 耒阳市| 丹阳市| 当阳市| 济宁市| 长顺县| 崇文区| 津南区| 太谷县| 贡觉县| 尼木县| 孝义市| 绍兴县| 夏津县| 当雄县| 商丘市| 新野县| 拜泉县| 疏附县| 钦州市| 虞城县| 本溪市| 伽师县| 永春县| 台湾省| 岚皋县| 响水县| 勐海县| 临猗县| 庆云县| 富川| 澄城县| 文山县| 库尔勒市| 松溪县|