VB6連線PI實時資料庫

2021-06-08 12:00:13 字數 4496 閱讀 3380

option explicit

dim tagname as string   '儲存單個工位號

dim tagpoint as long    '儲存單個點源

dim apistate as long    '儲存返回狀態

dim loginstate as long '儲存登入狀態

private sub cmdautoget_click()

if me.cmdautoget.caption = "自動採集" then

me.timerget.enabled = true

me.cmdautoget.caption = "停止採集"

elseif me.cmdautoget.caption = "停止採集" then

me.timerget.enabled = false

me.cmdautoget.caption = "自動採集"

end if

end sub

private sub cmdconnect_click()

on error goto errhandle

'連線伺服器

if me.cmdconnect.caption = "connect" then

connect trim(me.txtpiserver.text) '用封裝的connect函式連線資料庫,實際本質是呼叫函式piut_setservernode(servername)

dim quanxian as long       '儲存返回的許可權資訊

loginstate = piut_login("piadmin", "", quanxian)    '登入資料庫

if loginstate <> 0 then     '返回0則登入成功,否則登入失敗

msgbox "function piut_login() has errors!"

end if

me.lblconnstate.caption = "connected..."

me.cmdconnect.caption = "disconnect"

elseif me.cmdconnect.caption = "disconnect" then

dim connstate as long

connstate = piut_disconnect()       '斷開資料庫連線

me.txtgetvalue.text = connstate

me.lblconnstate.caption = "disconnected!"

me.cmdconnect.caption = "connect"

end if

exit sub

errhandle:

me.lblconnstate.caption = "disconnected!"

end sub

private sub cmdfindpoint_click()

if loginstate <> 0 then exit sub

tagname = me.txtgwh.text        '輸入工位號

apistate = pipt_findpoint(tagname, tagpoint)    '根據工位號查詢點源

if apistate = -5 then       '返回-5表示沒找到點源

msgbox "tag is not found!"

elseif apistate = 0 then    '返回0表示找到點源

msgbox "tag is found!"

end if

end sub

'讀取單個點源

private sub cmdget_click()

if apistate <> 0 then exit sub

dim getstate as long    '儲存返回狀態

dim tagvalue as single '儲存讀取的點源對應的值

dim tagvaluestate, tagvaluetime as long     '儲存返回的資料狀態,資料儲存時間

getstate = pisn_getsnapshot(tagpoint, tagvalue, tagvaluestate, tagvaluetime)    '讀取單個點源的資料

if getstate = 0 then    '0表示讀取成功

me.txtgetvalue.text = me.txtgetvalue.text & tagvalue & ","

else

msgbox "can't get value!"

end if

end sub

'批量寫入資料

private sub command1_click()

if apistate <> 0 then exit sub

dim tagname(3) as string    '儲存工位號的陣列

'pt-點源;istat-資料狀態,初始化為0即可;timedate-設定時間,為0則自動為當前時間

'piapierror-寫入時返回的錯誤資訊;icount-寫入資料的個數

'特別注意,不能這樣定義: dim pt(3),istat(3),timedate(3),piapierror(3) as long

'這樣定義只會將最後乙個認為是long型別,前面的都為variant型別

'或者可以分別單個定義

dim pt(3) as long, istat(3) as long, timedate(3) as long, piapierror(3) as long, icount as long

'dim istat(3) as long

dim rval(3) as single

'給工位號陣列賦值

tagname(0) = "ti1602a"

tagname(1) = "ti1602b"

tagname(2) = "ti1602c"

tagname(3) = "ti1602d"

'初始化資料

dim i as integer

for i = 0 to 3

pt(i) = 0

rval(i) = rnd()     '需儲存的值

istat(i) = 0

timedate(i) = 0

piapierror(i) = 0

next i

icount = 4

me.text2.text = ""

for i = 0 to 3

apistate = pipt_findpoint(tagname(i), pt(i))    '迴圈查詢點源,不能批量查詢

me.text2.text = me.text2.text & rval(i) & vbcrlf

next i

dim setstate as long    '儲存函式返回值

setstate = pisn_putsnapshots(pt(0), rval(0), istat(0), timedate(0), piapierror(0), icount) '批量上傳資料,引數為陣列,由於是傳址,故引數為陣列第乙個元素的位址

'pisn_putsnapshotsx(byval count&, ptnum&, drval#, ival&, bval as any, bsize&, istat&, flags%, time0 as any, errors&) as long

'setstate = pisn_putsnapshotsx(count, pt(0), rval(0), ival(0), bval(0), bsize, istat(0), flags(0), timedate(0), piapierror(0))

if setstate = 0 then

msgbox "set sucess!"

else

msgbox "error is: " & setstate

end if

end sub

'定時讀取單個資料

private sub timerget_timer()

if apistate <> 0 then exit sub

dim getstate as long

dim tagvalue as single

dim tagvaluestate, tagvaluetime as long

getstate = pisn_getsnapshot(tagpoint, tagvalue, tagvaluestate, tagvaluetime)

dim tagvaluexltime as date

tagvaluexltime = cdate(pitime2xl(tagvaluetime))

if getstate = 0 then

me.txtgetvalue.text = me.txtgetvalue.text & tagvalue & "," & tagvaluestate & "," & tagvaluexltime & vbcrlf

end if

end sub

VB6 連線MySql資料庫

目測 vb6 的資料比較少了,花了好久時間寫了個查詢語句 dim cn as newadodb.connection dim rs as newadodb.recordset 定義資料庫連線字串變數 dim strcn as string 定義資料庫連線引數變數 dim db host as str...

再看資料庫 (6)連線

連線是多表查詢的基礎。因為關聯式資料庫不包括指標或其它關聯記錄與記錄的機制,連線成為了生成跨表資料關係的唯一機制。這也就是要用連線的原因。1 內連線 使用比較運算子比較被連線列的列值。等值 不等 以外運算子 自然 但它使用選擇列表指出查詢結果集合中所包含的列。並刪除連線表中的反覆列。演示樣例 資料庫...

實時資料庫簡介和比較 PI

實時資料庫是資料庫系統發展的乙個分支,它適用於處理不斷更新的快速變化的資料及具有時間限制的事務處理。實時資料庫技術是實時系統和資料庫技術相結合的產物,實時資料庫系統的主要內容包括 實時資料庫模型 實時事務排程 包括併發控制 衝突解決 死鎖等內容 容錯性與錯誤恢復 記憶體組織與管理 訪問准入控制 i ...