Incorrect data from python Socket

Hi!

I am using an Arduino mega 2560 with an Ethernet shield. I am making a project, where you can set your IP address, gateway etc. through a socket. I did a couple of things with this communication and every time it was correct. Now its not. The data i send:

(This socket program is a python program)

my_StringFIXIP = "\x00\x00\x00\x25\x00\x00\x00\x24\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x0D\x00\x96\xA9\xAA\x24"
ws.send(my_StringFIXIP)

And I only need the last 4 bytes. This is gonna be the IP address. The last 4 bytes are (on the Arduino side):

16:25:43.041 -> 194
16:25:43.041 -> 150
16:25:43.085 -> 194
16:25:43.085 -> 169

So this is a complete mess.
But the first 20 bytes are ok.

This is how i want to give value of the IP:

IPAddress GivenIPAddress;
GivenIPAddress[0] = readedData[21];
GivenIPAddress[1] = readedData[22];
GivenIPAddress[2] = readedData[23];
GivenIPAddress[3] = readedData[24];

Can anyone help me with that?

Without seeing your Arduino code and Python code?

I have a suspicion that this has to do with encoding; 0x96, 0xA9 and 0xAA are oitside the usual ASCII range (0x00 .. 0x7F). For testing, see what happens of you change those to e.g. 0x7F.

1 Like

Your binary is getting translated to UTF8 Unicode. The "\x96" character is getting translated to the UTF8 sequence C2 96 and the "\xA9" character is getting translated to the UTF8 sequence C2 A9.

16:25:43.041 -> 194 (0xC2)
16:25:43.041 -> 150 (0x96)
16:25:43.085 -> 194 (0xC2)
16:25:43.085 -> 169 (0xA9)

You need some way to send binary and not Unicode.

1 Like

Perhaps you can try:

my_StringFIXIP = b"\x00\x00\x00\x25\x00\x00\x00\x24\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x0D\x00\x96\xA9\xAA\x24"
1 Like

Finally it works! Thank you, everyone!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.