我在用vb.net 写opc client遇到报错{指定数组不属于需要的类型}有没有大神给指点下

weixin_41733520 2018-02-13 09:51:41
Imports OPCAutomation

Public Class Form1
Public WithEvents AutoServer As OPCServer
Public ServerIndex As Object
Public WithEvents AutoSerGroups As OPCGroups
Public WithEvents AutoSerGroup As OPCGroup
Public AutoSerItems As OPCItems
Public AutoSerItem As OPCItem
Public AutoSerBrowser As OPCBrowser
Public ss As TreeNode
Public s As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AutoServer = New OPCServer
ServerList.Items.Clear()
Try
ServerIndex = AutoServer.GetOPCServers
Dim i As Integer
For i = LBound(ServerIndex) To UBound(ServerIndex)
ServerList.Items.Add(ServerIndex(i))
Next
ServerList.Text = ServerList.Items(0)
Catch ex As Exception
MessageBox.Show("搜索失败!!")
End Try
AutoServer = Nothing

End Sub
Private Sub sershow(ByVal node As TreeNode)
Dim i As Integer
Dim total As Integer
Dim strname() As String
AutoSerBrowser.ShowBranches()
total = AutoSerBrowser.Count
If total <= 0 Then
Else
ReDim strname(total)

For i = 1 To total
AutoSerBrowser.ShowBranches()
strname(i) = AutoSerBrowser.Item(i)
Dim nodx As TreeNode = node.Nodes.Add(strname(i))
AutoSerBrowser.MoveDown(strname(i))
sershow(nodx)
AutoSerBrowser.MoveUp()
Next
End If
End Sub

Private Sub AutoSerGroup_DataChange(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array) Handles AutoSerGroup.DataChange
Dim id As Integer
MessageBox.Show("adm")
For i = 1 To NumItems
id = ClientHandles(i)
Dim item As ListViewItem
item = lv1.Items.Add(id)
With item
.SubItems(1) = ItemValues(i)
.SubItems(2) = Qualities(i)
.SubItems(3) = TimeStamps(i)
End With
Next
End Sub


Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim sa(5) As String
Dim ct As Integer
Dim ABSPath(5) As String
If e.Node Is Nothing Then
Else
sa = e.Node.FullPath.Split("\")
For i = 1 To sa.Length - 1
ABSPath(i - 1) = sa(i)
Next
AutoSerBrowser.MoveToRoot()
AutoSerBrowser.MoveTo(ABSPath)
AutoSerBrowser.ShowLeafs()
ListBox1.Items.Clear()
ct = AutoSerBrowser.Count
For i = 1 To ct
ListBox1.Items.Add(AutoSerBrowser.Item(i))
Next
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AutoServer = New OPCServer
MessageBox.Show("sd")
TreeView1.Nodes.Clear()
'Try
AutoServer.Connect(ServerList.Text, "") ' 建立链接
AutoSerBrowser = AutoServer.CreateBrowser '浏览数据项名字
AutoSerGroups = AutoServer.OPCGroups
AutoSerGroup = AutoSerGroups.Add("group1")
AutoSerGroup.UpdateRate = 2000
AutoSerGroup.IsActive = True
AutoSerGroup.IsSubscribed = True
AutoSerItems = AutoSerGroup.OPCItems
Dim IItemSrvHandles(10) As Object
Dim IClientHandles(10) As Object
Dim strOPCltemlDs(10) As Object
Dim Errors(10) As Object
IClientHandles(1) = 1
strOPCltemlDs(1) = "S-BUS.dd.Flag.standby.DI1"

'Catch ex As Exception
'MessageBox.Show("无法建立链接")
'End Try
Dim nodx As TreeNode = TreeView1.Nodes.Add("S-BUS")
sershow(nodx)

AutoSerGroup.OPCItems.AddItems(1,strOPCltemlDs ,IClientHandles ,IItemSrvHandles ,Errors)


End Sub


End Class
就在 AutoSerGroup.OPCItems.AddItems(1,strOPCltemlDs ,IClientHandles ,IItemSrvHandles ,Errors) 这一步上报的错
我系统win7 64位vs2010
有没有大神指点下为什么菜鸟一只
...全文
1076 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
minfugan 2018-02-27
  • 打赏
  • 举报
回复
楼主,按下面的修改再试试看。
Dim IItemSrvHandles(0) As Object
Dim IClientHandles(0) As Object
Dim strOPCltemlDs(0) As Object
Dim Errors(0) As Object
IClientHandles(0) = 1
strOPCltemlDs(0) = "S-BUS.dd.Flag.standby.DI1"
weixin_41733520 2018-02-27
  • 打赏
  • 举报
回复
引用 4 楼 danding_ge 的回复:
OPCAutomation.OPCItems.AddItems(Int32 NumItems, Array& ItemIDs, Array& ClientHandles, Array& ServerHandles, Array& Errors, Object RequestedDataTypes, Object AccessPaths)
不了解OPC,但是结合看这个构造函数和你的错误,应该是Array& ItemIDs, Array& ClientHandles, Array& ServerHandles, Array& Errors这几个数组类型和你传入的参数类型Object不匹配导致的。 百度了一下,https://www.ad.siemens.com.cn/service/answer/solution.aspx?Q_ID=2359&cid=1031 这里的参数是这样声明的:
Dim WithEvents ServerObj As OPCServer  ''OPC Server对象,连接OPC服务器
 Dim GroupsObj As OPCGroups          ''OPC Groups对象,添加OPC组
 Dim WithEvents GroupObj As OPCGroup  ''OPC Group对象
 Dim ItemsObj As OPCItems             ''OPC Item集合
 Dim ServerHandles() As Long           ''服务器端OPC Item的句柄
 Dim ClientHandles() as Long            ''客户端OPC Item的句柄
 Dim ItemId(2) As String
 Dim Errors() As Long
试着改一下,看看是否有用?
我一开始是这样写的也是报一样的错误, vs
minfugan 2018-02-26
  • 打赏
  • 举报
回复
只定义数组不定义数组的数量试一下,即 Dim IItemSrvHandles(10) As Object Dim IClientHandles(10) As Object Dim strOPCltemlDs(10) As Object Dim Errors(10) As Object 改为 Dim IItemSrvHandles() As Object Dim IClientHandles() As Object Dim strOPCltemlDs() As Object Dim Errors() As Object
weixin_41733520 2018-02-25
  • 打赏
  • 举报
回复
引用 1 楼 danding_ge 的回复:
错误详情贴一下
未处理 System.Runtime.InteropServices.SafeArrayTypeMismatchException HResult=-2146233037 Message=指定的数组不属于需要的类型。 Source=mscorlib StackTrace: 在 System.StubHelpers.MngdSafeArrayMarshaler.ConvertSpaceToNative(IntPtr pMarshalState, Object& pManagedHome, IntPtr pNativeHome) 在 OPCAutomation.OPCItems.AddItems(Int32 NumItems, Array& ItemIDs, Array& ClientHandles, Array& ServerHandles, Array& Errors, Object RequestedDataTypes, Object AccessPaths) 在 WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) 位置 F:\vb\WindowsApplication3\WindowsApplication3\Form1.vb:行号 113 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(ApplicationContext context) 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 在 WindowsApplication1.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:
weixin_41733520 2018-02-25
  • 打赏
  • 举报
回复
@ 媲美爱 不好意思今天刚上班,家里有没网。 未处理 System.Runtime.InteropServices.SafeArrayTypeMismatchException HResult=-2146233037 Message=指定的数组不属于需要的类型。 Source=mscorlib StackTrace: 在 System.StubHelpers.MngdSafeArrayMarshaler.ConvertSpaceToNative(IntPtr pMarshalState, Object& pManagedHome, IntPtr pNativeHome) 在 OPCAutomation.OPCItems.AddItems(Int32 NumItems, Array& ItemIDs, Array& ClientHandles, Array& ServerHandles, Array& Errors, Object RequestedDataTypes, Object AccessPaths) 在 WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) 位置 F:\vb\WindowsApplication3\WindowsApplication3\Form1.vb:行号 113 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(ApplicationContext context) 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 在 WindowsApplication1.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:
公西雒 2018-02-25
  • 打赏
  • 举报
回复
OPCAutomation.OPCItems.AddItems(Int32 NumItems, Array& ItemIDs, Array& ClientHandles, Array& ServerHandles, Array& Errors, Object RequestedDataTypes, Object AccessPaths)
不了解OPC,但是结合看这个构造函数和你的错误,应该是Array& ItemIDs, Array& ClientHandles, Array& ServerHandles, Array& Errors这几个数组类型和你传入的参数类型Object不匹配导致的。 百度了一下,https://www.ad.siemens.com.cn/service/answer/solution.aspx?Q_ID=2359&cid=1031 这里的参数是这样声明的:
Dim WithEvents ServerObj As OPCServer  ''OPC Server对象,连接OPC服务器
 Dim GroupsObj As OPCGroups          ''OPC Groups对象,添加OPC组
 Dim WithEvents GroupObj As OPCGroup  ''OPC Group对象
 Dim ItemsObj As OPCItems             ''OPC Item集合
 Dim ServerHandles() As Long           ''服务器端OPC Item的句柄
 Dim ClientHandles() as Long            ''客户端OPC Item的句柄
 Dim ItemId(2) As String
 Dim Errors() As Long
试着改一下,看看是否有用?
公西雒 2018-02-13
  • 打赏
  • 举报
回复
错误详情贴一下

16,552

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧