用VB.NET编写了一个OPC连接西门子S7400 PLC的程序,这两天刚刚能够在MyGroup_AsyncReadComplete内的ItemValues(j)(1))读取S7400 PLC地址DB110.DBW地址数据,我是要读取DBW内1000组数据,,但是发现怎么只能循环读取读取16组DB110.DBW数据,当我将要读取的ITEMVALUES(J)(1)元素剩下从6个减少至三个,显示读取了121组。是否一组数据读取元素是有总的上限?我尝试了新建一个GROUP,但是失败了,也许是我还不知道如何再建一个GROUP,特请教。
程序如下:
MyGroup = MyOPCServer.OPCGroups.Add("OTR") ‘新增GROUP
Call PLC_U_AddItem()
Timer1.Enabled = True
Private Sub PLC_U_AddItem() ‘ 往组里添加元素
For i As Short = 1 To 32
MyOPCItemIDs(i) = "S7:[OTR]DB110,W" & ((i - 1) * 26 + 2) & ",12"
MyOPCItemIsArray(i) = 0
MyClientHandles(i) = i
Next i
MyGroup.OPCItems.AddItems(ItemCount, MyOPCItemIDs, MyClientHandles, MyItemServerHandles, AddItemServerErrors)
Sub PLC_AsyncRead()
MyGroup.AsyncRead(32, MyItemServerHandles, _
MyErrors, lTransID_Rd, lCancelID_Rd)
Private Shared Sub MyGroup_AsyncReadComplete(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Object, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array, ByRef Errors As System.Array) Handles MyGroup.AsyncReadComplete
For j = 1 To 999
Label(j).Text = j & Chr(46) &
Chr("&H" & Mid(Hex(ItemValues(j)(1)), 1, 2)) & Chr("&H" & Mid(Hex(ItemValues(j)(1)), 3, 2)) &
Chr("&H" & Mid(Hex(ItemValues(j)(2)), 1, 2)) & Chr("&H" & Mid(Hex(ItemValues(j)(2)), 3, 2)) &
Chr("&H" & Mid(Hex(ItemValues(j)(3)), 1, 2)) & Chr("&H" & Mid(Hex(ItemValues(j)(3)), 3, 2)) &
Chr("&H" & Mid(Hex(ItemValues(j)(4)), 1, 2)) & Chr("&H" & Mid(Hex(ItemValues(j)(4)), 3, 2)) &
Chr("&H" & Mid(Hex(ItemValues(j)(5)), 1, 2)) & Chr("&H" & Mid(Hex(ItemValues(j)(5)), 3, 2))
Chr("&H" & Mid(Hex(ItemValues(j)(6)), 1, 2)) & Chr("&H" & Mid(Hex(ItemValues(j)(6)), 3, 2))
原程序太长,以上仅粘贴部分GROUP及添加ADD ITEM元素程序。该段程序中Label(j).Text 本应该显示999组数据,但是最后仅显示16组Label(j).Text。请问是怎么回事?
若需要再添加一组GROUP,及在新的GROUP中添加元素OPCItems.AddItems,程序该怎么写?
问题补充:
为什么只读16个就对了?
纠正一下,For i As Short = 1 To 32、MyGroup.AsyncRead(32, MyItemServerHandles, _
MyErrors, lTransID_Rd, lCancelID_Rd)
上两段原来都是1000,只是我后来新建一个GROUP1,也是像 仙道 说的那样新建一个GROUP,只是要注意一点的是MyClientHandles(i) = i这个句柄中i要减掉16。但是这样发现新建的组GROUP1通过断点监视可以看到ItemValues(j)(1)有16组数据,但是Label(j+16)显示只有4组。我又忽视掉哪儿了?
最佳答案
只会读16个就对了,你通过OPC读到的变量个数在MyGroup_AsyncReadComplete()第二个参数NumItems
For j = 1 To 999改成 For j = 1 To NumItems,如果想读1000个,继续往前改对应的传递参数MyGroup.AsyncRead()
添加另一组MyGroup1及在新组添加新元素方法如下:
MyGroup = MyOPCServer.OPCGroups.Add("OTR") ‘新增GROUP
MyGroup1=MyOPCServer.OPCGroups.Add("OTR1") ‘新增MyGroup1
MyGroup1.OPCItems.AddItems() ’一次添加多个元素 跟之前一样
MyGroup1.OPCItems.AddItem(") ‘一次添加一个元素
你用的OPC COM server,以后的趋势是用UA server了
提问者对于答案的评价:
虽然不是我要的答案,但分给你了。
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc152613.html