NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

TS and VB.Net assembly - "object not defined" problem, help please

Hi - new TS user here so treat me kind! Thanks

 

I'm creating a VB.Net assembly (dll) whose functions I want to call from TS to write/read to an instrument using a telnet session.

 

If I create a VB form in my project to use the assembly write/read function calls it works ok. But I keep getting "object not defined" error messages when trying to call the same functions from TS steps using the .net adaptor.

 

Let me simplify the VB.Net code to highlight the key areas and the object that is throwing the error.

 


 

Imports System.Net.Sockets
Imports System.Text

 

Public Class Class1

    Public telnetclient2 As New TcpClient ' object used to open the telnet session
    Public thisstream2 As NetworkStream ' object used to write/read to/from the instrument via the telnet session

    Public Function TelNet_Connect2() ' used to make initial telnet connection and send some default messages to the instrument

        telnetclient2.Connect("10.0.0.16", 23) ' connect

        Threading.Thread.Sleep(2000) ' sleep 2s

        Read2() ' read initial response after connecting

        TelNet_Query2("login") ' send "login" message and read response

        TelNet_Query2("root") ' send "root" message and read response

        TelNet_Query2("wl ver") ' send "wl ver" message and read response

    End Function

    Private Sub Read2() ' used to read telnet response

         thisstream2 = telnetclient2.GetStream()

         '  use thisstream2 object to get telnet response here

   End Sub

 

    Public Function TelNet_Query2(Text As String)

         thisstream2 = telnetclient2.GetStream()

         ' write Text

         ' wait 2s

         ' read response

    End Function

End Class


 

Now in the TS sequence, Setup section I can call TelNet_Connect2() ok. In functions Read2 and TelNet_Query2 then object thisstream2 is available.

 

However if in the TS sequence, Main section I call TelNet_Query2("wl revinfo") so sending a different command then TS throws an error from my VB code saying object thisstream2 is Nothing.

 

How can this be Nothing when previously TelNet_Connect2 called TelNet_Query2 which used object thisstream2 with no problems.

 

Hope somebody can shed some light on this for me please.

 

I know the code is ok in VB.Net since if I call the same functions in the same order from a VB.Net form then it runs ok.

 

Much appreciated.

 

Regards

 

Chris Hargreaves

 

0 Kudos
Message 1 of 6
(4,586 Views)

When you construct your object with the .NET adapter you need to store it in a TestStand Object Reference variable and reuse that as the existing object when you want to make later method calls on the same object (rather than constructing a new object for the method calls). Are you doing that? If you show us what your .NET calls in your sequence look like that would help us better determine the problem.

 

Hope this helps,

-Doug

0 Kudos
Message 2 of 6
(4,541 Views)

Thanks for the reply Doug.

 

The guys at NI Newbury are looking into this issue too. I have passed on your suggestion. They have my full VB.Net code.

 

I'll see if your comments give them any clues.

 

Regards,

 

Chris

 

0 Kudos
Message 3 of 6
(4,534 Views)

Hi again Doug,

 

Here's copy of the info I sent NI direct - still no reply.

 

Plus the attached VB.Net source files and TS sequence.

 

I've added new simple functions to the .Net dll today (not in this version of source code) and calling those functions from TS sequence steps show similar issues.

 

It looks like each time a TS step calls a .Net dll function it creates a new instance. So two adjacent steps calling the same VB.Net function are not sharing variables/objects.

 

Looking at the Module tab in Step Settings, I can see the "Object Reference" under "Type". Its "Value" is empty. I'm guessing here that I need some extra info input into TS step to "link" the two variables/objects together.

 

There seems to be very limited online TS and VB.Net examples. Am I on the right lines and can you provide any more info?

 

Much appreciated,

 

Regards

 

Chris

 

NI email info below...

 

Ok – I’ve stripped down the TS and VB.Net code to an absolute minimum and uploaded all files to your “incoming” folder.

 

2644128 for NI.7z

 

The TS sequence file is “Sequence file 3.seq”

 

The 2013 VB.Net solution file is “example_class2.sln” in sub folder…

 

...for NI\V0.2\example_class2

 

When you open this VB.Net solution you’ll see 2 projects,

 

  1. example_class2
  2. example_form2

 

the common dll code is Class1. This is the dll called by the TS sequence steps.

 

Let me describe first how the code works ok when run in VB.Net using the example_form2 project and then second using TS with its problem.

 

  1. Looking at form1.vb code, Form1 class creates a new instance of Class1 called “chris”
  2. In Class1, lines #1 and #2 create two new objects used for telnet communications using TcpClient and NetworkStream
  3. Form1_Load event first calls TelNet_Connect
  4. In Class1, line #3 connects to IP address 10.0.0.16, port 23
  5. In Class1, line #4 calls TelNet_Query with instrument command “login”
  6. In Class1, line #5 we associate the NetworkStream and TcpClient ready for instrument communications
  7. There would be extra code inside TelNet_Query to send/receive to/from the instrument but we don’t need that here to show the problem
  8. So TelNet_Connect passes ok
  9. Form1_Load event second calls TelNet_Version
  10. This in turn calls TelNet_Query with instrument command “wl ver”
  11. This passes ok
  12. In the form we can use the button press event Button4_Click to call TelNet_Query with no problem

 

Now how this similar sequence doesn’t work with TS

 

  1. In “Sequence File 3”, Setup, we have a step “telnet connect2” which calls our dll function TelNet_Connect
  2. This passes ok
  3. In Main, we have a step “telnet ver2” which calls our dll function TelNet_Version
  4. Here is where the problem occurs. When TelNet_Version calls TelNet_Query, then line #5 throws an error.

<see Document123.rtf attached here>

 

An exception occurred inside the call to .NET member 'TelNet_Version':

System.InvalidOperationException: The operation is not allowed on non-connected sockets.

   at System.Net.Sockets.TcpClient.GetStream()

   at example_class2.Class1.TelNet_Query(String Text) in T:\Anritsu MT8870A\TestStand vb.net control project\for NI\V0.2\example_class2\example_class2\Class1.vb:line 21

   at example_class2.Class1.TelNet_Version() in T:\Anritsu MT8870A\TestStand vb.net control project\for NI\V0.2\example_class2\example_class2\Class1.vb:line 31

 

I hope you can follow this explanation.

Any ideas much appreciated,

Regards

Chris

 

 

Download All
0 Kudos
Message 4 of 6
(4,490 Views)

Have you tried what I suggested in my previous post? If you want to share an object between different .NET steps you need to create a local variable to store the object (an Object Reference variable) and then you should only call the constructor once and store the result in the local variable, in the first step. In subsequent steps you should select the special <Use Existing> item instead of calling a constructor.

 

Also, the example sequence located here shows this:

<TestStandPublic>\Examples\Demos\Computer Motherboard Test\DotNET\Computer Motherboard Test Sequence.seq

 

Hope this helps,

-Doug

0 Kudos
Message 5 of 6
(4,459 Views)

Hi Doug,

 

Thanks for the reply. With your explanation and a little further guidance from NI directly, I now have my code working. The changes make sense.

 

Much appreciated.

 

Regards

 

Chris

 

0 Kudos
Message 6 of 6
(4,418 Views)