LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

typecast to TCP connection id

Solved!
Go to solution

I have a program running subpanels.  I open a Modbus connection in the first and assign it to a variant type global variable.  I can wire the ID connection number to the variant global but when I try to read the global in the other subpanels I cannot convert the variant back to a connection ID.  Is that possible to do? The variant was the only global variable type I could get to accept the connection ID.  I used the type cast function to try to convert back but the type cast will not accept the variant input. Am I going about this the wrong way or is there a simple way to do this?  Any help or advice is appreciated

0 Kudos
Message 1 of 11
(5,705 Views)

Are you trying to use a global variable or a Functional Global Variable?

 

You can right click on a TCP/IP connection wire and create an indicator or a control which would you could copy to a subVI to be your input or output in a functional global variable.

0 Kudos
Message 2 of 11
(5,694 Views)

Hi,

 

I have a similar problem. I want to pass the TCP connection ID to a subVI. I read your explanation, but did not quite understand it. Can you elaborate, perhaps send a screenshot or sample vi?

I have LabView 7.1.

 

Thanks,

Victor

0 Kudos
Message 3 of 11
(5,523 Views)
Solution
Accepted by topic author jmw50290

Hey jmw,

 

Based on your description of the problem it appears you are just trying to create a global variable that can hold the connection information on a TCP connection.  This can easily be done by dropping a global variable on your block diagram then double clicking it to open up its front panel.  On the front panel of the global you will need to place a TCP Network Connection Refnum.  Once this is done save the global VI and on your original block diagram you should be able to left click the global and select the TCP connection.  The global should now accept a TCP connection.

Justin D
Applications Engineer
National Instruments
http://www.ni.com/support/
0 Kudos
Message 4 of 11
(5,501 Views)

Just be aware that LabVIEW garbage collects all its refnums as soon as the hierarchy that opened that refnum goes idle!

 

Not a problem if you have one single hierarchy (but please pardon me the question, why use globals at all in that case? LabVIEW is a dataflow language and in LabVIEW globals should NEVER be used instead of wires).

 

Once you have a program architecture where you have several top level VIs (for instance plugins started dynamically to run as a seperate deamon in the background) globals can be necessary to pass variables between those deamons and between deamons and the main program. However if you create (open) a refnum in one of those deamons and then try to make use of that refnum in another deamon or the main VI you have to make sure that the deamon who created that refnum stays running until you don't need the refnum elsewhere. As soon as the deamon would go idle (stop executing) the refnums it created get closed automatically and your refnum you hold in a global is totally invalid from that point on.

Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 11
(5,482 Views)

Hi Justin and Rolf,

 

I'm attaching snippets of my code.

 

In the mainVI, I show the TCP Listen.vi function, which use to create the connection ID. I use this throughout the Main VI to handle all TCP related stuff.

The SubVI shown is what I'm trying to do and gives me the problem I'm asking help for.

The SubVI snippet shows what I'm doing in there. I just put a connection ID control, and connected it to the subvi connector panel.

 

Justin recommends making a global. I guess I have to make the global in the mainVI, and then refer to it in the subVI?

 

Rolf recommends using wires, how do I do that?

 

Regards,

Victor

Download All
0 Kudos
Message 6 of 11
(5,474 Views)

Hey vicky,

 

I was making a suggestion earlier for how to pass the connection ID using globals, but I would definitely recommend using wires over globals as rolfk suggested.  Your code uses the sub VI connector panel in order to pass the connection ID which I believe is what rolfk is refering to when he suggested using wires.  Wires are preferred over variables, and variables should only be used when necessary as they can cause performance decreases and race conditions in some cases.

Justin D
Applications Engineer
National Instruments
http://www.ni.com/support/
0 Kudos
Message 7 of 11
(5,460 Views)

Hi Justin,

 

But the code snippets I showed you don't work. I get a connection error. That's why I'm asking for help, to know the proper way to do what I mean to.

 

Thanks,

Victor

0 Kudos
Message 8 of 11
(5,448 Views)

Of course it gives a connection error! You use a timeout of 10 sec for the Listen function. Unless you make sure that your client connects in less than 10 seconds from the moment you start your VI, the Listen VI will timeout with an error and then go on with whatever else is in your diagram. Since you don't wire the error cluster your subVI will try to communicate over the invalid connection refnum and cause an error too.

 

Look at some of the TCP examples in LabVIEW to see a more robust server client architecture than what you have done.

Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 11
(5,442 Views)

If this is the limitation, what would you recommed instead? Wiring directly into VI shows OK, but it doesn't make sense to implement that way in my application. I'm using this Ref ID# to call new VI where it needs it. Probe shows correct ID#, but TCP write gives error.  

0 Kudos
Message 10 of 11
(5,131 Views)