OPC-无法将类型为“System.Int32”的对象强制转换为类型“System.Array”

在用VB.NET编写OPC读写西门子S7400程序时候,碰到图中句柄问题,请问该问题如何解决?

Public Shared MyItemServerHandles2 As System.Array

Private Sub PLC_AsyncWrite(ByVal NumItemsW As Integer, ByRef ItemValuesW() As System.Object, ByRef MyErrors As System.Array)
 Dim lHandle(U_NUMITEMS) As System.Array
        Dim i As Integer
        If MyGroup2 Is Nothing Then
            Exit Sub
        End If
        If MyGroup2.OPCItems.Count > 0 Then
 lTransID_Rd2 = lTransID_Rd2 + 1
 For i = 1 To 30
 lHandle(i) = MyItemServerHandles2(NumItemsW)          
                MyGroup2.AsyncWrite(1, lHandle(i), ItemValuesW,
               MyErrors, lTransID_Rd2, lCancelID_Rd2)
    Next
        End If
    End Sub
我另外定义了一个数组STRBUF或STRBUF(30)测试,尝试过定义成System.array 、string、 integer,来接收MyItemServerHandles2这个System.array内的值,但是没办法获得MyItemServerHandles2内的值,
另外定义的数组或string,都显示Nothing
用MSGBOX(MyItemServerHandles2(I))可以显示出MyItemServerHandles2的值


将   Public Shared MyItemServerHandles2(32) As system.array改为    Public Shared MyItemServerHandles2(32) As Integer
错误提示:无法将类型为"system.int32[*]"的对象强制转换为system.int32[]
另外,我用Asyncread却没问题。

问题补充:
在lHandle(i) = MyItemServerHandles2.GetValue(NumItemsW)中能够获取值,但在MyGroup2.AsyncWrite(1, lHandle, ItemValuesW, MyErrors, lTransID_Rd2, lCancelID_Rd2)报错,System.Runtime.InteropServices.SafeArrayTypeMismatchException”类型的未经处理的异常在 X-RAY_BARCODE.exe 中发生 。其他信息: 指定的数组不属于需要的类型。

最佳答案

你要将整数利用循环语句一个一个加入到数组里才行,直接一个数组不可能直接转化成1个整数,1个整数也不能转化成1个数组。整数只能转化为数组里的某一个数

提问者对于答案的评价:
后来确实通过实例化,运行无错误。但是运行AsyncWrite后,DB1.DBW数据依然无改变,新建的AsyncWriteComplete也无运行及数据反映。请问高人我还有哪里有问题?

原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc150255.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2017年6月4日
下一篇 2017年6月4日

相关推荐