Discussion:
Use .Net deserialization in LV?
(too old to reply)
brimcd
2008-08-12 21:40:22 UTC
Permalink
I'm trying to deserialize a memory stream sent from a MS .Net3.5 application via UDP to a Labview client. My options seemed to be:
1) a CIN - which I have mostly done and can probably finish up
2) a DLL - but I can't figure out how to invoke managed code
3) use the LV .Net Constructor node and Invoke node. This seemed to be the simplest, but I cannot get the deserialization to work.
 
To test,
 --I create a Binary Formatter (Constructor Node) and a Memory Stream (Constructor Node), ,then use the Binary Formatter (Invoke Node) to serialize a .Net Object dreated from an array. This seems to work, though I cannot figure out how to 'see' the serialized stream.
 
The main problem is deserialization, where I cannot create any method (using Invoke Node) of the Binary formatter that allows me to input the stream.
 
The equivalent c++ code is fairly simple:
 
Create a stream and serialize the object, and send to byte array:
  MemoryStream ms = new MemoryStream();  string theSerializedObject;
  System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter              = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  formatter.Serialize(ms, ObjectToSerialize);  byte[] thisByteArray = ms.ToArray();
 
 
Grab byte array from transmitted string, create the mem stream, and deserialize the data:
  byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);  MemoryStream ms = new MemoryStream(thisByteArray);  System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter        = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Attached is an attempt to duplicate these steps in LV, without any luck.
I appreciate any ideas or samples.
Thanks,
Brian
smercurio_fc
2008-08-12 22:10:07 UTC
Permalink
forgot the attachment...
brimcd
2008-08-12 22:10:07 UTC
Permalink
bummer - I KNOW i attached it once - one more try...


TestSerialize1.vi:
http://forums.ni.com/attachments/ni/170/348838/1/TestSerialize1.vi
smercurio_fc
2008-08-12 22:40:17 UTC
Permalink
Either your pseudo-code is incomplete, or something else, because your LabVIEW code does not implement your pseudo-code, at least for the second part. Is that your actual .NET code?
brimcd
2008-08-12 22:40:18 UTC
Permalink
The attached TestSerialize.vi is simply an attempt to implement a standalone serialize-deserialize example,  similar to the MS c++ .Net code shown, but written entirely in LV constructors and methods. If I could get that to work, I could probably implement the deserializer where I really need it. I just wanted to start off with a simple case to see if I could do it.
BTW - I don't think the wiring in this vi makes sense (there should be a serialized stream output going to an input for deserialization) but i canot find an appropriate method/connector.
I thought this would be a fairly easy example to implement in LV (serialize and deserialize _anything_ in the same app), but now my head hurts.
Thanks for your time,
Brian
smercurio_fc
2008-08-13 15:10:10 UTC
Permalink
Well, my head hurts too because I can't match your LabVIEW code to your text code. The LabVIEW constructors and methods are what the class exposes. It's not like LabVIEW makes up its own. Your text code says this:Grab byte array from transmitted string, create the mem stream, and deserialize the data:  byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
  MemoryStream ms = new MemoryStream(thisByteArray);
  System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
        = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();


but your LabVIEW code (second frame) doesn't look anything like this, so I have no idea what I'm supposed to compare.  
brimcd
2008-08-13 22:40:20 UTC
Permalink
Sorry - obviously I'm not being clear.  Just ignore all the c++ code (pretend I never sent it), and see if is there any way to get my lv code to work?  I'd like to see an example where you 1) create an array in lv2) call .Net constructors and methods to serialize a stream3) call .Net constructors and methods to de-serialize that stream4) re-create the array from the stream. That is what I attempted to do in the lv code I attached, but never got the de-serialization part to work. The problem appears to be that I cannot find an output for the stream that is filled with data in the first sequence panel (the serializationStream) The BinaryFormatter-Serialize node in the first sequence panel has no output for that serialized stream - it is input only. So, how do I get access to that stream? Through the reference? My logic says I need to connect that stream in the first sequence panel with the 'serializationStream' input in the second panel, but I have no stream exiting the first panel, and cannot find one in any method available.  The code shows an attempt to use the MemoryStream reference, along with an attempt to make sure I'm reading from the start of the stream, but it fails. I can do this with a CIN, but it gets a bit messy and I'd prefer this method if possible.  Thanks for your time,Brian


TestSerialize.vi:
http://forums.ni.com/ni/attachments/ni/170/349162/1/TestSerialize.vi
smercurio_fc
2008-08-14 16:10:08 UTC
Permalink
Attached is an example to do that. Is this what you were looking for?


TestSerialize2.vi:
http://forums.ni.com/ni/attachments/ni/170/349328/1/TestSerialize2.vi
brimcd
2008-08-14 18:10:07 UTC
Permalink
EXACTLY!! Many thanks - I couldn't put the pieces together like you did. I can work with this to do what I need. Thanks,Brian  
Loading...