On an embedded device running a C application, I have defined this struct:
struct TestStruct
{
float first;
int second;
char third;
};
On request, I send this struct via sockets:
else if(strcmp(str, "Get Stru") == 0)
{
TestStruct testStruct;
testStruct.first = 1.2;
testStruct.second = 42;
testStruct.third = 'A';
INT32 sendDataLength = send(m_Socket, (char *)(&testStruct), sizeof(testStruct), 0);
}
and read it from a Python script on my desktop:
import struct
import socket
from ctypes import *
class YourStruct(Structure):
_fields_ = [('v', c_float),
('t', c_int),
('c', c_char)]
s = socket.socket()
host = '127.0.0.1'
port = 1234
s.connect((host, port))
s.send('Get Stru'.encode())
data = s.recv(20)
print(data)
x = YourStruct()
This is the data printed to console on my desktop:
How can i reassemble the data
into a YourStruct
?
Note that the embedded device uses little endian, so I have had to use struct.unpack("<" + "f" * 2048, data)
to reassemble an array of floats.