mike5 Posted November 23, 2010 Report Share Posted November 23, 2010 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 Quote Link to comment
mje Posted November 23, 2010 Report Share Posted November 23, 2010 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. 1 Quote Link to comment
crelf Posted November 23, 2010 Report Share Posted November 23, 2010 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). Quote Link to comment
Christian_L Posted November 23, 2010 Report Share Posted November 23, 2010 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. 1 Quote Link to comment
mike5 Posted November 24, 2010 Author Report Share Posted November 24, 2010 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 Quote Link to comment
mike5 Posted November 24, 2010 Author Report Share Posted November 24, 2010 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 Quote Link to comment
ned Posted November 24, 2010 Report Share Posted November 24, 2010 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. Quote Link to comment
mike5 Posted November 24, 2010 Author Report Share Posted November 24, 2010 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 Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.