Search code examples
pythonudparraysctypespack

Appending the byte representation of a float to a python bytearray


I am using python with ctypes to read an array of bytes and store this in a python bytearray. This array is then transmitted as a UDP packet.

I would like to append the time to this array of bytes, and believe the way to do this is to do:

t = time.time()

Which returns a python float. Then find the location this is stored in memory, and extract the array of bytes (either 4 or 8, I'm not sure) which represent the float, and append these bytes to my existing bytearray.

Any suggestions for time-stamping a UDP packet made of a bytearray gratefully received.

My minimal code is as follows:

class Host:
    def __init__(self):
        self.data = (ctypes.c_ubyte * 4112)()
        self.data_p = ctypes.byref(self.data)
        self.byte_data = bytearray(4120)
        self.t = (ctypes.c_ubyte * 8)()
        self.t_p = ctypes.byref(self.t)
        self.t = time.time()
        self.Data_UDP_IP = "127.0.0.1"
        self.Data_UDP_PORT = 8992
        self.DataSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    def genBytearray(self)
        <function that fills self.data with actual data>
        data = ctypes.cast(self.data_p, ctypes.POINTER(ctypes.c_ubyte))
        self.t  = time.time()
        t_cast = ctypes.cast(self.t_p, ctypes.POINTER(ctypes.c_ubyte))
        self.byte_data[0:4112] = data[0:4112]
        self.byte_data[4112:4120] = t_cast[0:8]



import time
import ctypes
import socket

Host1 = Host()
genBytearray(Host1)
Host1.DataSock.sendto(self.byte_data, (self.Data_UDP_IP, self.Data_UDP_PORT))

Problem is, it seems t_cast is always equal to 0. I think by calling:

self.t = time.time()

I am not actually writing the time to the location pointed to by self.t_p. Been stuck on this for a while and can't help but think there's an easier way. I have come across struct.pack, but python seems to crash when I try to use it.


Solution

  • In case anyone stumbles across this:

    t = (ctypes.c_double * 1)(time.time())
    tnew = ctypes.cast(t, ctypes.POINTER((ctypes.c_uint8 * 8)))
    tnew.contents[0:8]
    

    Extracts the uint8 representations of the 64 bit double returned by time.time().