Jump to content

Poly input


Recommended Posts

Is there a way to create a SubVI with a real "poly" input? I mean an input wire that takes anything without losing information what "anything" is - it may be a custom cluster that sub knows nothing about. What I would want is then serialize this cluster for Data transmission in little-endian order using "Flatten to string", but I want to flatten the raw data, it cannot be encapsulated into a variant (I suppose that "Variant" carries some additional meta-data - I still have to check this).

I hope I have correctly describe what I want to do., I have a feeling this sort of thing is very hard in LabVIEW. I hope I'm wrong.

Thanks in advance Mike

Link to comment

You pretty much need to use a variant, or of course have an army of polymorphic VIs which demand every type be accounted for.

I have done what you're describing using variants. They do have meta information, which you take advantage of. I don't have the info handy, but there is a library meant to handle variants, I believe it is called VariantType.lvlib and is somewhere in your vi.lib/utilities folder. A word of caution though that since these VIs are not on the palette NI might change them some day...

The key is recursion. Take the variant, act accordingly if it is a primitive type (bool, double, etc). If it's a complex type (cluster, array, waveform, etc) do what you need to do then recurse on the contained type(s).

Note because variants do involve a lot of copying, this can get prohibitive for deep/large data structures. You will never get as good performance as doing a "hardwired" VI for whatever your type is.

I ultimately abandoned my variant approach because serializing my main data structures could take minutes using this method (compared to seconds by keeping things type specific). I still use it for simple or small data structs though.

  • Like 1
Link to comment
Is there a way to create a SubVI with a real "poly" input?

Well yes, but no. LabVIEW can't do run-time polymorphism, so somewhere the poly child needs to know explicitly what the data type is so it can act on it. That said, you can acheive what you're looking for with limited functionality with variants (as mje already suggested) or you could put together an OO architecture that uses inheritance. Either way, you're still just kinda pushing the strictness down the line to somewhere else (which is totally fine, if that's what you're trying to do).

Link to comment

You can take a look at the GXML code as a starting point for implementing your 'Anything' to binary conversion.

GXML converts Anything to XML. It uses a variant input and then converts the variant to a flattened string. Then it recursively parses the flattened string, ignoring the outer variant container, to convert its contents to XML. You can replace all of the XML conversion code with your own code.

If all you want to do is to remove the outer variant container from the flattened string then it won't be much work at all and you won't even need to get into recursive parsing.

  • Like 1
Link to comment

You can take a look at the GXML code as a starting point for implementing your 'Anything' to binary conversion.

GXML converts Anything to XML. It uses a variant input and then converts the variant to a flattened string. Then it recursively parses the flattened string, ignoring the outer variant container, to convert its contents to XML. You can replace all of the XML conversion code with your own code.

If all you want to do is to remove the outer variant container from the flattened string then it won't be much work at all and you won't even need to get into recursive parsing.

Yes, yes, yes... If I understand you correctly, I can "Flatten to string" the variant in little-endian byte order, and it will correctly serialize the internal data as well. Then I remove the variant container, and the data remaining would look like I was using the "Flatten to string" directly on the raw data?

For the reverse I would take the raw data, put the variant container around it and use the "Unflatten from string" to get the correct data inside the variant.

If the answer is yes, then this is exactly what I was looking for.

Br, Miha

Link to comment

Christian,

thanks to your answer and some reverse engineering of flattened variant, I now have exactly what I was looking for.

Just if someone who knows can answer this: Does variant always end with four 0 bytes, or can this be something else as well?

Thanks and best regards, Mike

Link to comment

Yes, yes, yes... If I understand you correctly, I can "Flatten to string" the variant in little-endian byte order, and it will correctly serialize the internal data as well. Then I remove the variant container, and the data remaining would look like I was using the "Flatten to string" directly on the raw data?

It sounds like you're already done, but you could save yourself a lot of reverse-engineering work by simply using "Variant to Flattened String," which I believe does exactly what you want - provides a flattened representation of the data without a variant wrapper.

Link to comment

It sounds like you're already done, but you could save yourself a lot of reverse-engineering work by simply using "Variant to Flattened String," which I believe does exactly what you want - provides a flattened representation of the data without a variant wrapper.

Well, yes. But I need the data in little-endian byte order.

Br, Mike

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.