vb.net做二次开发,原厂提供了vba和C#的例程,前面都还比较顺利,直到遇到NsInteropReadProfile这个函数无法正确传出数组。
我觉得应该是ref和动态数组的问题,望各位不吝赐教。
我的代码
Dim RetCode As Object = Nothing
Dim Aperture As Integer
Dim StartPosition As Single
Dim EndPosition As Single
Dim SampleResolution As Single
Dim DecimationFactor As Integer
Dim numOfPoints As Integer = 10
'Dim ArrayOfSingle(0 To 100) As Object
'Profile amplitude has to be a variant that packs one dimension array of singles
Dim Amplitude(0 To 10) As Object
'Amplitude = ArrayOfSingle
'Profile position has to be a variant that packs one dimension array of singles
Dim Position(0 To 10) As Object
'Position = ArrayOfSingle
'Aperture 1 (index = 0)
Aperture = 0
'Read aperture limits
RetCode = ns.NsAsGetApertureLimits(Aperture, StartPosition, EndPosition)
'Determine the Decimation factor (want 100 points for the entire aperture)
'分辨率
RetCode = ns.NsAsGetSamplingResolution(Aperture, SampleResolution)
DecimationFactor = (EndPosition - StartPosition) / SampleResolution / numOfPoints
'Read Data
RetCode = ns.NsAsReadProfile(Aperture, StartPosition, EndPosition, DecimationFactor, Amplitude, Position)
原厂提供的vba代码
Dim RetCode As Variant
Dim Aperture As Integer
Dim StartPosition As Single
Dim EndPosition As Single
Dim DecimationFactor As Integer
Dim SampleResolution As Single
Dim ArrayOfSingle(0 To 100) As Single
'Profile amplitude has to be a variant that packs one dimension array of singles
Dim Amplitude As Variant
Amplitude = ArrayOfSingle
'Profile position has to be a variant that packs one dimension array of singles
Dim Position As Variant
Position = ArrayOfSingle
' To avoid flickering of the screen, do not update anything until we fill everything
Application.ScreenUpdating = False
'Clear previous position and amplitude arrays
Range("Profiles!B17:E116").ClearContents
'Aperture 1 (index = 0)
Aperture = 0
'Read aperture limits
RetCode = NSServer.NsAsGetApertureLimits(Aperture, StartPosition, EndPosition)
'Determine the Decimation factor (want 100 points for the entire aperture)
RetCode = NSServer.NsAsGetSamplingResolution(Aperture, SampleResolution)
DecimationFactor = (EndPosition - StartPosition) / SampleResolution / 100
'Read Data
RetCode = NSServer.NsAsReadProfile(Aperture, StartPosition, EndPosition, DecimationFactor, Amplitude, Position)
C#例程
//currently we are only reading data from Aperture 1 (or X axis) to simplify the code.
short aperture = 0;
float leftBound = 0;
float rightBound = 0;
float samplingRes = 0;
short decimation = 1;
int numOfPoints = 10;
/* Initialize new arrays that will be marshaled through the COM interop. The interop
* will handle conversion from VARIANT to .NET Array. In this case, they will come
* back as an array of floats.
*/
object amplitude = new float[numOfPoints];
object position = new float[numOfPoints];
//Calculate the decimation factor to only retrieve numOfPoints
NsInteropGetApertureLimits(aperture, ref leftBound, ref rightBound);
NsInteropGetSamplingResolution(aperture, ref samplingRes);
decimation = (short)((rightBound - leftBound) / samplingRes / numOfPoints);
//Read the profile data
NsInteropReadProfile(aperture, leftBound, rightBound, decimation, ref amplitude, ref position);
函数定义
private static extern int NsInteropReadProfile(
short sAperture,
float fStartPosition,
float fEndPosition,
short sDecimationFactor,
[In][Out][MarshalAs(UnmanagedType.Struct)] ref object ProfileAmplitude,
[In][Out][MarshalAs(UnmanagedType.Struct)] ref object ProfilePosition);
开发文档
NsAsReadProfile
Method Return Value:
VT_ERROR – error code
Method Parameter List:
VTS_I2 – axis number
VTS_R4 – start position (μm)
VTS_R4 – end position (μm)
VTS_I2 – decimation factor
VTS_PVARIANT – profile amplitude array. This variant packs a dynamic array
of VTS_R4 and is passed by reference to the method.
VTS_PVARIANT – profile position array. This variant packs a dynamic array
of VTS_R4 and is passed by reference to the method.
Remarks:
This method reads the profile amplitude and position for the specified axis
within the specified limits (start position and end position). Decimation factor
can be used to read a smaller amount of data. For example, if the decimation
factor is set to 10, profile amplitude and position will have data from indexes
10, 20, 30, etc. Decimation factor does not affect the precision of computations
because all the computations are done using the entire set of acquired data.
Start position and end position have to be within the axes acquisition limits
returned by NsAsGetApertureLimits.
System.Runtime.InteropServices.COMException:“类型不匹配,错误代码:80020005
用C#的例程跑通过,传出的应该是两个single数组,vb.net里试了各种办法都没法跑通,定义成object赋初始值nothing可跑通但是不能正常传出数值。
以上