LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Use .Net deserialization in LV?

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
0 Kudos
Message 1 of 11
(4,722 Views)
forgot the attachment...
0 Kudos
Message 2 of 11
(4,717 Views)
bummer - I KNOW i attached it once - one more try...
0 Kudos
Message 3 of 11
(4,715 Views)
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?
0 Kudos
Message 4 of 11
(4,712 Views)

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

0 Kudos
Message 5 of 11
(4,709 Views)

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.  

0 Kudos
Message 6 of 11
(4,686 Views)

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 lv

2) call .Net constructors and methods to serialize a stream

3) call .Net constructors and methods to de-serialize that stream

4) 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

0 Kudos
Message 7 of 11
(4,662 Views)
Attached is an example to do that. Is this what you were looking for?
0 Kudos
Message 8 of 11
(4,640 Views)

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

 

 

0 Kudos
Message 9 of 11
(4,629 Views)

Hey, this is close to what I've been trying to do only that in my case, I am not initializing an array but wanting to have labview pick up memorystream that is running in a .NET (C#) application. Can you help?

Here's my c# code:

 

this.facePoints3D = frame.Get3DShape();

using (MemoryStream stream = new MemoryStream())
{
var sw = new StreamWriter(stream);

 

foreach (var vector in facePoints3D)
{

sw.WriteLine(vector.Z);

sw.Flush();
}

stream.Position = 0;


StreamWriter writemem = new StreamWriter("coordinates.txt", true);

using (writemem)
{
var read = new StreamReader(stream);
var myread = read.ReadToEnd();
writemem.WriteLine(myread);
}

}

0 Kudos
Message 10 of 11
(4,070 Views)