Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this answer is a bit late to the game, but may at least prove useful to other people finding this question in their search-results. Because you say the Delphi code sends and receives "C compatible data" it seems that for the sake of the answer about Python's handling it is irrelevant whether it is Delphi (or any other language) on the other end...</p> <p>The python <em>struct</em> and <em>socket</em> modules have all the functionality for the basic usage you describe. To send the example record you would do something like the below. For simplicity and sanity I have presumed signed integers and doubles, and packed the data in "network order" (bigendian). This can easily be a one-liner but I have split it up for verbosity and reusability's sake:</p> <pre><code>import struct t_packet_struc = '&gt;iiid' t_packet_data = struct.pack(t_packet_struc, pid, data_type, size, value) mysocket.sendall(t_packet_data) </code></pre> <p>Of course the mentioned "presumptions" don't need to be made, given tweaks to the format string, data preparation, etc. See the <em>struct</em> inline help for a description of the possible format strings - which can even process things like Pascal-strings... By the way, the <em>socket</em> module allows packing and unpacking a couple of network-specific things which <em>struct</em> doesn't, like IP-address strings (to their bigendian int-blob form), and allows explicit functions for converting data bigendian-to-native and vice-versa. For completeness, here is how to unpack the data packed above, on the Python end:</p> <pre><code>t_packet_size = struct.calcsize(t_packet_struc) t_packet_data = mysocket.recv(t_packet_size) (pid, data_type, size, value) = struct.unpack(t_packet_struc, t_packet_data) </code></pre> <p>I know this works in Python version 2.x, and suspect it should work without changes in Python version 3.x too. Beware of one big gotcha (because it is easy to not think about, and hard to troubleshoot after the fact): Aside from different endianness, you can also distinguish between packing things using "standard size and alignment" (portably) or using "native size and alignment" (much faster) depending on how you prefix - or don't prefix - your format string. These can often yield wildly different results than you intended, without giving you a clue as to why... (there be dragons).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload