From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Array parameter to .NET method results in runtime error

Solved!
Go to solution

Tried to use .NET to read binary file, but results in unexpected runtime error!

 

Class: System.IO.FileStream

Method: Read

 

This method requires a numeric array as out-parameter. Further a offset-value and a count-value can be passed to the method. Passing count = 1 everything works fine. If count > 1 runtime error occurs as below:

 

-------------- 

Exception has been thrown by the target of an invocation.
Root Exception:Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
Source:  mscorlib   at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)

-------------- 

 

Looks like called method cannot handle TestStand numeric array or array isn't passed correctly!

 

 

I know that there are a lot of possible workarounds using other languages, libraries, etc. but I'd like to stick on this .NET solution.

 

 

Would be nice if someone can give me a hint!

 

(seq-file attached)

 

 

0 Kudos
Message 1 of 7
(3,787 Views)
The value 5 for the parameter count is not valid and causes the .NET run-time error. If you use 1 it works.
Andreas Stark
LabVIEW Lead @ Rocket Factory Augsburg
0 Kudos
Message 2 of 7
(3,764 Views)

@ AndreasS:

 

This is exactly what I already mentioned in my message (see above).

 

But I'd like to read more than one byte from file!

 

 

rgds.

0 Kudos
Message 3 of 7
(3,762 Views)

Sorry, missed the "count > 1" part.

 

The Run-time error is passed back from the .NET assembly and has nothing to do with TestStand.

 

I'm not a .NET expert, but I think you get the .NET error because you are missing some steps. Here a Visual Basic example from:http://msdn.microsoft.com/de-de/library/system.io.filestream.read.aspx

 

 

Imports System
Imports System.IO

Class FSRead

Public Shared Sub Main()
'Create a file stream from an existing file.
Dim fi As New FileInfo("c:\csc.txt")
Dim fs As FileStream = fi.OpenRead()

'Read 100 bytes into an array from the specified file.
Dim nBytes As Integer = 100
Dim ByteArray(nBytes) As Byte
Dim nBytesRead As Integer = fs.Read(ByteArray, 0, nBytes)
Console.WriteLine("{0} bytes have been read from the specified file.", nBytesRead.ToString())
End Sub 'Main
End Class 'FSRead

 

 Regards,

 

Andreas Stark
LabVIEW Lead @ Rocket Factory Augsburg
0 Kudos
Message 4 of 7
(3,758 Views)
Solution
Accepted by topic author bestware

Bestware,

 

Try this sequence. Put both files in the same directory. I guess if it works with text files, it should also work with binary files too.

 

Probably a better solution would be to use ReadByte instead of read, inside a loop.

 

I agree with you that this method cannot handle Teststand array.

 

Regards

Download All
Message 5 of 7
(3,745 Views)
Hi Bestware,

I think the reason for this is because your datatypes are not consistant. The .NET Assembly expects an array of bytes (8bits) but you are passing it an array of numbers. 
It works for a single byte at a time because the starting location in memory is the same regardless and so it gets "stuffed" into the numeric variable okay. However, if you try to do more than a single byte at a time, the assembly would try to put the second byte 8bits after the first, which is still part of the first "number"'s memory.


As minime suggested, your best best is probably to read byte by byte in a loop, or to read the entire array in a .NET Code Module instead of directly from TestStand.
Jervin Justin
NI TestStand Product Manager
Message 6 of 7
(3,732 Views)

Thank you all!

 

I'm wondering why it's not possible to define certain datatype (e.g. array of bytes) to pass to .NET methods, as

 

it's possible to define e.g. array of bytes to pass to C/C++ DLL!

0 Kudos
Message 7 of 7
(3,720 Views)