Structure converstion to Bytearray
-
Hi All, I have a struct in C# application used for serial port communication. I have to read the struct value send from C# application to a C application. What is to be done??? Thanks in Advance San
You are running serial port communication between 2 managed program?
-
You are running serial port communication between 2 managed program?
-
okey then the best way to do this is to define a sort of a 'communication protocol' between two devices : First byte : message lenght (or data length) data bytes : your data byte that holds checksum [data length][.....data.....][checksum] then in your C# program you have to create a class that build the message you have to send : calculate length convert your structure on a data array (*) calculate the checksum -> So here we are in the core of your question: the best way is to loop over EACH element of your strucuture convert it into byte array and the add it to data array use BitConverter class : BitConverter.GetBytes (T) -> T is an elementary type (float, double, char, int, etc...)
-
okey then the best way to do this is to define a sort of a 'communication protocol' between two devices : First byte : message lenght (or data length) data bytes : your data byte that holds checksum [data length][.....data.....][checksum] then in your C# program you have to create a class that build the message you have to send : calculate length convert your structure on a data array (*) calculate the checksum -> So here we are in the core of your question: the best way is to loop over EACH element of your strucuture convert it into byte array and the add it to data array use BitConverter class : BitConverter.GetBytes (T) -> T is an elementary type (float, double, char, int, etc...)
-
Could u please send any sample code or link to imlement this. I am a beginner to this Regards, San
-
Could u please send any sample code or link to imlement this. I am a beginner to this Regards, San
let suppose your have this Class :
class SomeClass
{
double l;
double [] m = new double [10]; //double array
string s;byte [] ToByteArray ()
{
List<byte> b = new List<byte> ();
b.AddRange (BitConverter.GetBytes (l));
for (int i = 0; i < m.Length ; i++)
b.AddRange (BitConverter.GetBytes (m [i]));
char [] tabc = s.ToCharArray();
for ( int i = 0 ; i < m.Lenght ; i++)
b.AddRange (BitConverter.GetBytes (tabc [i]);return b.ToArray();
}
}
//usage :
SomeClass c = new SomeClass ();
byte [] b = c.ToByteArray(); -
San wrote:
I am a beginner to this
Then start with something simpler, like Hello World. If however you're being paid to do this I suggest you go to your employer and tell them you are not yet capable of the task you have been set.
Hello ! Don't be so Rude :) we all were beginners some time !
-
Hello ! Don't be so Rude :) we all were beginners some time !
-
+1 dude !
-
let suppose your have this Class :
class SomeClass
{
double l;
double [] m = new double [10]; //double array
string s;byte [] ToByteArray ()
{
List<byte> b = new List<byte> ();
b.AddRange (BitConverter.GetBytes (l));
for (int i = 0; i < m.Length ; i++)
b.AddRange (BitConverter.GetBytes (m [i]));
char [] tabc = s.ToCharArray();
for ( int i = 0 ; i < m.Lenght ; i++)
b.AddRange (BitConverter.GetBytes (tabc [i]);return b.ToArray();
}
}
//usage :
SomeClass c = new SomeClass ();
byte [] b = c.ToByteArray();