Packing data in Python with struct from C example

1

I am using a C example and manufacturer's documentation to retrieve data from a data colleciton module. I have succesfully implemented multiple functions (e.g. request firmware version, get/set system mode etc), but i am stuck with one particular function. I think the issue is how i am packing my data that is sent to the module. Below is my latest attempt.

def SetSubObject(soc):
    tx = struct.pack('hh32sIIHhfBBH', 30, 127, 'test_name', 7, 0xffffffff, 0 , 0x00, 1.0, 0x03, 0x00, 0)
    soc.send(tx)

This does not work. The module becomes unresponsive until the socket is closed and the connection is re-established.

Below is a C example i was provided by the manufacturer.

****** header file ******
#define MSG_SET_SUBOBJECT 30
#define MSG_GET_SUBOBJECT 31

typedef struct
{
    short msgId;
    short id;
    char name[32];
    unsigned int jobType;
    unsigned int variant;
    unsigned short kind;
    short objectId;
    float quantifier;
    char sensor_mask;
    char event_mask;
    unsigned short _1;
}msgSubobject_t;
****************

***** Practical example *********
msgSetSubobject_t msgSetSubobject;
msgSetSubobject.msgId = MSG_SET_SUBOBJECT;
msgSetSubobject.id = 127;
strcpy_s(&(msgSetSubobject.name), "test_name");
msgSetSubobject.jobType = 7;         
msgSetSubobject.variant = 0xffffffff;         // subobject active on every variant of the VSE
msgSetSubobject.kind = 0;
msgSetSubobject.objectId = 0;
msgSetSubobject.quantifier = 1.0;
msgSetSubobject.sensor_mask = 0x03;           // sensor1|sensor2|~sensor3|~sensor4
msgSetSubobject.event_mask = 0x00;            // ~IN1|~IN2
msgSetSubobject._1 = 0;                                              // reserved
************

I've put together the parts of the documentation relevant to what i'm working on, see the link below.

Manufacturer documentation

I have tried packing the data in multiple ways, and also sending slightly different data (e.g. numbers in hex formats or decimal, binary etc). I have used the Format Characters table for reference found at https://docs.python.org/2/library/struct.html.

Is there any obvious error in my Python code? One thing i noticed on the manufacturer documentation (in the MsgSubobject table) they seem to state that the object_id should be a s16 and is 6 bytes long. The byte count goes from 42 to 48, from object_id to quantifier. This confused me, also because this parameters is declared as a short in the C example.

-------------------- EDIT ADDED MORE INFO ----------------------

As suggested below in the comments, i have tried using tcpdump to see what is actually sent to the device i'm trying to communicate to. After some googling i tried:

sudo tcpdump host 192.168.0.1 and port 3321 -vvv -X -qns 0

Running my code (open socket, send packet, close socket) this is what i get from the tcpdump command above (192.168.0.1 port 3321 is the module):

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
15:13:56.847427 IP (tos 0x0, ttl 64, id 62124, offset 0, flags [DF], proto TCP (6), length 60)
    192.168.0.3.36648 > 192.168.0.1.3321: tcp 0
    0x0000:  4500 003c f2ac 4000 4006 c6ba c0a8 0003  E..<..@.@.......
    0x0010:  c0a8 0001 8f28 0cf9 0383 467b 0000 0000  .....(....F{....
    0x0020:  a002 7210 8183 0000 0204 05b4 0402 080a  ..r.............
    0x0030:  112a c966 0000 0000 0103 0307            .*.f........
15:13:56.847659 IP (tos 0x0, ttl 255, id 58375, offset 0, flags [none], proto TCP (6), length 44)
    192.168.0.1.3321 > 192.168.0.3.36648: tcp 0
    0x0000:  4500 002c e407 0000 ff06 566f c0a8 0001  E..,......Vo....
    0x0010:  c0a8 0003 0cf9 8f28 00c7 91b1 0383 467c  .......(......F|
    0x0020:  6012 0110 a1bc 0000 0204 0110 0000       `.............
15:13:56.847759 IP (tos 0x0, ttl 64, id 62125, offset 0, flags [DF], proto TCP (6), length 40)
    192.168.0.3.36648 > 192.168.0.1.3321: tcp 0
    0x0000:  4500 0028 f2ad 4000 4006 c6cd c0a8 0003  E..(..@.@.......
    0x0010:  c0a8 0001 8f28 0cf9 0383 467c 00c7 91b2  .....(....F|....
    0x0020:  5010 7210 816f 0000                      P.r..o..
15:13:56.848424 IP (tos 0x0, ttl 64, id 62126, offset 0, flags [DF], proto TCP (6), length 96)
    192.168.0.3.36648 > 192.168.0.1.3321: tcp 56
    0x0000:  4500 0060 f2ae 4000 4006 c694 c0a8 0003  E..`..@.@.......
    0x0010:  c0a8 0001 8f28 0cf9 0383 467c 00c7 91b2  .....(....F|....
    0x0020:  5018 7210 81a7 0000 1e00 7f00 7465 7374  P.r.........test
    0x0030:  5f6e 616d 6500 0000 0000 0000 0000 0000  _name...........
    0x0040:  0000 0000 0000 0000 0000 0000 0700 0000  ................
    0x0050:  ffff ffff 0000 0000 0000 803f 0300 0000  ...........?....
15:13:56.848641 IP (tos 0x0, ttl 64, id 62127, offset 0, flags [DF], proto TCP (6), length 40)
    192.168.0.3.36648 > 192.168.0.1.3321: tcp 0
    0x0000:  4500 0028 f2af 4000 4006 c6cb c0a8 0003  E..(..@.@.......
    0x0010:  c0a8 0001 8f28 0cf9 0383 46b4 00c7 91b2  .....(....F.....
    0x0020:  5011 7210 816f 0000                      P.r..o..
15:13:56.848788 IP (tos 0x0, ttl 255, id 58631, offset 0, flags [none], proto TCP (6), length 40)
    192.168.0.1.3321 > 192.168.0.3.36648: tcp 0
    0x0000:  4500 0028 e507 0000 ff06 5573 c0a8 0001  E..(......Us....
    0x0010:  c0a8 0003 0cf9 8f28 00c7 91b2 0383 46b5  .......(......F.
    0x0020:  5011 0110 b49b 0000 0000 0000 0000       P.............
15:13:56.848857 IP (tos 0x0, ttl 64, id 62128, offset 0, flags [DF], proto TCP (6), length 40)
    192.168.0.3.36648 > 192.168.0.1.3321: tcp 0
    0x0000:  4500 0028 f2b0 4000 4006 c6ca c0a8 0003  E..(..@.@.......
    0x0010:  c0a8 0001 8f28 0cf9 0383 46b5 00c7 91b3  .....(....F.....
    0x0020:  5010 7210 816f 0000                      P.r..o..
^C
7 packets captured
7 packets received by filter
0 packets dropped by kernel
python
c
struct
asked on Stack Overflow Jan 30, 2020 by PySoft • edited Jan 30, 2020 by PySoft

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0