This is not C so you cannot cast raw bytes to a structure.
I suggest you do this: create a class that will represent your data packet and will include all those string/int properties. If you want to send this packet over the wire you need to create a method that will return a byte array filled with values taken from data packet properties. E.g. you add 12 bytes of string property, 4 bytes of each int property etc.
When you receive those bytes over the wire you need to do the oposite. Create a new object of data packet class and initialize its properties acording to what was received. E. g. you take first 12 received bytes, convert them to string (Encoding.UTF8.GetString) and set the property on data packet object.
You could use serialization (i didn’t use it on net mf yet - didn’t event know it has that) but it will require a lot more data to be send.
Coming from C++ and clean binary structures, working binary structures in C# is a pain.
Here is a quick example of what you could do. As I am far to be a C# expert, this is a working example, but you may have a better way. Interrested into knowing it, though : !
byte[] MyArray = new byte[256];
byte[] PacketIdentificationHeader = Encoding.UTF8.GetBytes("GRAPHTEC-RD");
Int32 CommunicationID = 0x1234ABCD;
// Start with a clean array filled with 0s
Array.Clear(MyArray, 0, 256);
// First bytes are text, copyed at offset 0
Array.Copy(PacketIdentificationHeader,0,MyArray,0,PacketIdentificationHeader.Length);
// Adding an int32 at offset 16, (MSB first)
for (int i = 0; i < 4; i++) MyArray[16 + i] = (byte)((CommunicationID >> (8 * (3-i))) & 0xFF);
// Manualy setting up hex data at offset 20
MyArray[20] = 0x88;
MyArray[21] = 0xAA;
MyArray[22] = 0x55;
MyArray[23] = 0xFF;
// Showing 24 first bytes results in hex
string result="";
for (int i = 0; i < 24; i++) result += (i>0?":":"") + (char)((MyArray[i] >> 4) > 9 ? (MyArray[i] >> 4) + 'A' - 10 : (MyArray[i] >> 4) + '0') + (char)((MyArray[i] & 0xF) > 9 ? (MyArray[i] & 0xF) + 'A' - 10 : (MyArray[i] & 0xF) + '0');
Debug.Print(result);
@ Nicolas I agree with you. C# promotes a different approach with its dynamic memory allocation so casting raw bytes to structures can’t be used. On the other hand in regular .NET you have object serialziation to binary/XML where you cant send a far more complex data than just plain structs.
I don’t think so. For example, the struct in the image contains a variable length block (Parameter area). Just let a class manage all that stuff and your code will get nicely separated.
Hello Guys, Thank you all very much for answering my question.
Sorry for replying you late, my internet was down for a while.
Now I have another problem.
I sent a packet which contains the code to activate “ECHO mode” ( so the data logger will reply the same packet to my laptop)
By using a program called “WinDump”, i can see that my laptop sends 256 byte packet to the data logger, and i can see that data logger actually replies with the same length.
However, my program never really receives anything from the data logger because each time the data logger replies to my laptop via DIFFERENT PORT.
the first time it replies via port 60021,
later on, 60023, 60025, 60027 , 60024.
I set my IPEndPoint,
IPEndPoint(IPAdress.Any, 0);
here, by setting the value to 0, supposedly the program can listen to all the ports.
My new question is " How to fix that port of my laptop so the data logger can communicate with my program?"
P.S I did shutdown my firewall and anti-virus software for the experiment.
I just googled it, and i checked some codes on some websites.
some of the websites say by setting that value to 0, the socket will listen to all the ports. :o