We no longer sell products directly from the GHI Electronics website, click here for more details. Please find a Distributor to complete your online order. If you have any questions regarding this notice please contact us at support@ghielectronics.com.

Serialization

Last modified July 26, 2014

Required

Tutorials: First NETMF Project

Introduction

A simple definition of serialization would be a way of converting an object (class/structure) to an array of bytes or vice versa. But why do we need this? Lets say we have an object for something and want to transfer it to another device over the network or from a device to a PC over a USB cable. It would be impossible to transfer the object (how would we transmit methods?) but if we have a way to convert the object's data into a byte array then we can easily transfer it. The receiver will then take the data into a byte array which then is used to reconstruct the object data.

.NET Micro Framework does include some serialization methods but because NETMF is targeted for small devices, the implementation of serialization on NETMF is incompatible with the full .NET Framework. In other words, you can't transfer serialized data from a NETMF device to a PC and expect it to be de-serialized correctly. This can be easily covered by implementing serialization on the PC that is compatible with NETMF. Before you decide to do this, read some more in this tutorial.

A basic example

Here is an example:

using System;
using Microsoft.SPOT;
 
public class Program
{
    [Serializable]  
  	public class MyClass
    {
        public int ii;
        public float ff;
    }
    public static void Main()
    {
        MyClass before = new MyClass();
        before.ii = 1;
        before.ff = 1;
 
        byte[] SerializedData = Reflection.Serialize(before, typeof(MyClass));
 
        // we can now trandsfer the data 
        MyClass after = (MyClass)Reflection.Deserialize(SerializedData, typeof(MyClass));
 
        // the 2 objects, "before" and "after" are identical now
    }
}

Adding on, serialization is pretty complex so it requires some decent amount of memory and its complexity makes it run slow on small devices.

More Efficient

To use serialization efficiently on small NETMF device, especially if speed is needed, we can implement our own serialization. If we were using C/C++ then serialization can be easily done using pointers.

Something like

float ff = 5;
unsigned char data[4];//float is 4 bytes
float * fp = (float*)data;//assign the pointer to point to our byte array
*fp = ff;// we now have moved the 4 bytes from the float into the byte array
 
// using shift
int ii = 123;
unsigned char data[4];// integer is 4 bytes
data[0] = ii;
data[1]=ii>>8;
data[2]=ii>>16;
data[3]=ii>>24;

This can still be done in RLP of course. This is useful if you are transferring objects from C# to RLP.

When multi-byte data is stored in memory there has to be a way to know if the most significant byte is at the high memory address or low memory address. Remember an int is 4 bytes so they has to be some order.

This example shows the data stored in both endianess. So just keep this in mind when working with pointers and packing data.

// little endian
int ii = 123;
unsigned char data[4];// integer is 4 bytes
data[0] = ii;
data[1]=ii>>8;
data[2]=ii>>16;
data[3]=ii>>24;
 
//big endian
int ii = 123;
unsigned char data[4];// integer is 4 bytes
data[0]=ii>>24;
data[1]=ii>>16;
data[2]=ii>>8;
data[3] = ii;

Pointer Replacement in NETMF

C# on NETMF doesn't have unprotected code or pointers but there are some methods added by Microsoft and GHI to overcome this.

To move an integer into a byte array and reconstruct it back:

using System;
using Microsoft.SPOT;
 
public class Program
{
    public static void Main()
    {
        uint before = 123;
        byte[] data = new byte[4];
        //place value into an array, serialize
        Microsoft.SPOT.Hardware.Utility.InsertValueIntoArray(data, 0, 4, before);
 
        // send the byte array somewhere....
 
        //construct a value from an array, deserialize
        uint after = Microsoft.SPOT.Hardware.Utility.ExtractValueFromArray(data, 0, 4);
    }
}

To move a float into byte array and construct it back. This is going to require the GHI.Hardware assembly.

using System;
using Microsoft.SPOT;
using GHI.Utilities;

public class Program
{
    public static void Main()
    {
        float before = 123;
        byte[] data = new byte[4];
        //place value into an array, serialize
        Arrays.InsertFloat(data, 0, before);

        // send the byte array somewhere....

        //construct a value from an array, deserialize
        float after;
        after = Arrays.ExtractFloat(data, 0);
    }
}
Leave feedback about this document.
Let us know if the information presented here was accurate, helpful and if you have any suggestions.
Leave feedback about this document.
Let us know if the information presented here was accurate, helpful and if you have any suggestions.

* Indicates required fields.
This form is only for feedback not support.
Review our how to find information guide on locating helpful resources.