欢迎光临
我们一直在努力

OPC客户程序(VB篇——异步)-.NET教程,VB.Net语言

建站超值云服务器,限时71元/月

建立如下窗体:

引用如下:

代码如下:

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » OPC客户程序(VB篇——异步)-.NET教程,VB.Net语言
分享到: 更多 (0)