Netduino home hardware projects downloads community

Jump to content

The Netduino forums have been replaced by new forums at This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

convert data types to byte[] and back for streaming

arrays data conversion data streams byte[]

  • Please log in to reply
2 replies to this topic

#1 nb63


    New Member

  • Members
  • Pip
  • 2 posts

Posted 26 June 2014 - 06:35 PM

Forgive me if this has been covered elsewhere but I did not find the answer on this forum.

I need to send/recieve data over the ethernet(Socket) to a number of devices. Most of those are running old C code so the data needs to be sent as a byte array. I cannot find a way to convert my data structures to byte[] arrays in order to read and write them. On the desktop environment, the keywords Unsafe and fixed allow me to get pointers and do the data conversion but when i tried this on the netduino the code builds without a problem but immediately and consistently will brick the device.


I tried byte[] myByteArray = Reflection.Serialize(foof, typeof(float));


but got an exception that this is not implemented. MemoryStream is also not implemented.

I had high hopes for the nerduino but if I cannot do this data conversion it is useless. The ability to send data between different devices seems like a fundamental requirement. Does anyone out there know of a way to do the data conversion?

#2 sfx


    Advanced Member

  • Members
  • PipPipPip
  • 52 posts

Posted 27 June 2014 - 02:35 AM

Hi nb63,
The .NET Micro Framework is only a subset of its much larger parent (a highly abridged version, if you will). As such, much of the BCL is not included in order to keep the on-device memory footprint appreciably smaller.
I have had a similar issue to you when dealing with custom objects, but have managed to cross process boundaries by using interfaces along with my own Xml messaging/command transport. In essence, this meant that I had to devolve my objects into a meaningful string representation (e.g. an Xml or JSON payload containing state, type and command data) that could then be later restored via a shared parser with interfaces. The NETMF has the facility to convert UTF8 strings to and from byte arrays, so making something similar to old-school .NET Remoting is one way of handling your problem, albeit without the same degree of flexibility.
Of course, all of this depends on whether you're using NETMF on each of your edge devices. If you're not, then these other devices will need a way to parse the content and then instantiate the state of your message within the context of its own environment.
I can understand that this would be a real bugbear for you, but hang in there. Given the electric (pun intended) pace of the IoT, I'm sure something like this would be on the NETMF team's radar.
Take care,

#3 baxter


    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 27 June 2014 - 05:47 AM

I previously confronted this issue and found a couple of references,

I copied the structure serialize code (near the bottom of the page) from the second reference and it compiles
just fine on the Mini using firmware

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.NetduinoMini
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic.Constants
Imports System.Text
Imports System.Threading
Imports System.Collections

Module Module1
    Sub Main()
        Dim s As Struct = New Struct
        Dim ms As MyStruct = New MyStruct With {.el1 = 1, .el2 = 2, .el3 = 3}
        Dim sb() As Byte = Struct.Convert(ms)
    End Sub

    <StructLayout(LayoutKind.Sequential, Pack:=1, Size:=12)>
    Private Structure MyStruct
        Dim el1 As Byte
        Dim el2 As Int16
        Dim el3 As Byte
    End Structure

    Public Class Struct
    'Charles Law 

        Public Shared Function Convert(ByVal MyStruct As Object) As Byte()
            Dim al As ArrayList
            Dim Fields As FieldInfo() = MyStruct.GetType.GetFields
            al = New ArrayList

            For Each fld As FieldInfo In Fields
                If fld.FieldType.Equals(GetType(Byte)) Then
                    ' Add byte to array list

                ElseIf fld.FieldType.Equals(GetType(Int16)) Then
                    ' Add 16-bit value to array list
                    Dim i16 As Int16

                    i16 = CType(fld.GetValue(MyStruct), Int16)
                    al.Add(CByte(i16 >> 8))
                    al.Add(CByte(i16 And &HFF))
                    Throw New Exception("Cannot convert type.")
                End If
            Next fld

            Return DirectCast(al.ToArray(GetType(Byte)), Byte())

        End Function
    End Class

    Public Function PrintArray(title As String, ByVal Arr() As Byte) As String
        Dim s As String = title & vbCrLf
        For i = 0 To Arr.Length - 1
            s &= "i = " & i.ToString & "  " & "Byte = " & Arr(i).ToString("X2") & vbCrLf
        Return s.Trim
    End Function

End Module

I didn't run it for this post because my Mini is in storage. I lost interest in this because I found for my purposes it was easier to just convert the data type to a string and then convert the string to bytes and then reverse this to go back to the data type (not too efficient, but it works)

Also tagged with one or more of these keywords: arrays, data conversion, data streams, byte[]

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.