D
D
DaviDOSik2020-07-27 16:01:01
Python
DaviDOSik, 2020-07-27 16:01:01

How to declare an array of structures in Python (cypyton)?

In general, the problem is this. There is a certain library that imports structures from the C library using ctypes.

The library is here python_j2534

In python, a structure class is created accordingly ...

import ctypes as ct
 
from J2534.dllLoader import MyDll
PassThru_Data = (ct.c_ubyte * 4128)
class PassThru_Msg(ct.Structure):
    _fields_ = [
        ("ProtocolID", ct.c_ulong),
        ("RxStatus", ct.c_ulong),
        ("TxFlags", ct.c_ulong),
        ("Timestamp", ct.c_ulong),
        ("DataSize", ct.c_ulong),
        ("ExtraDataIndex",ct.c_ulong),
        ("Data", PassThru_Data)]


Here this structure is inherited by classes:

ptData = PassThru_Data
class baseMsg(PassThru_Msg):
    def _setData(self, data):
        print (data)
        self.DataSize = len(data)
        self.Data = ptData()
        for i in range(self.DataSize):
            self.Data[i] = data[i]
    def setID(self, ID):
        d = Func.IntToID(ID)
        self._setData(d)
    def setIDandData(self, ID, data = []):
        d = Func.IntToID(ID) + data
        self._setData(d)</spoiler>
 
class pt15765Msg(baseMsg):
    def __init__(self, TxFlag):
        self.ProtocolID = ProtocolID.ISO15765
        self.TxFlags = TxFlag
class ptMskMsg(pt15765Msg):
    pass
class ptPatternMsg(pt15765Msg):
    pass
class ptFlowControlMsg(pt15765Msg):
    pass
class ptTxMsg(baseMsg):
    def __init__(self, ProtocolID, TxFlags):
        self.ProtocolID = ProtocolID
        self.TxFlags = TxFlags
class ptRxMsg(baseMsg):
    def show(self):
        print(self.ProtocolID, self.RxStatus, self.Data[:self.DataSize])

Then in the file I create a structure variable and can pass it to the function that uses it, and back the function returns an array of data to me in it:

msg = J2534.ptRxMsg()
print(msg.Data[:100])


This only works for one message... the documentation for the library, which is not written in C, has this excerpt
:

The PassThruWriteMsgs function is used to transmit network protocol messages over an existing logical
communication channel. The network protocol messages will flow from the User Application to the
PassThru device.
long PassThruWriteMsgs(
 unsigned long ChannelID,
 PASSTHRU_MSG *pMsg,
 unsigned long *pNumMsgs,
 unsigned long Timeout
);

Parameters
ChannelID
The logical communication channel identifier assigned by the J2534 API/DLL when the communication
channel was opened via the PassThruConnect function.
pMsg
Pointer to the message structure containing the UserApplication transmit message(s).
For sending more than one message, this must be a pointer to an array of PASSTHRU_MSG structures.
Refer to PASSTHRU_MSG structure definition on page 46.

PNumMsgs
Pointer to the variable that contains the number of PASSTHRU_MSG structures that are allocated for
transmit frames. On function completion this variable will contain the actual number of messages sent to
the vehicle network. The transmitted number of messages may be less than the number requested by the
UserApplication.
Timeout
Timeout interval(in milliseconds) to wait for transmit completion. A value of zero instructs the API/DLL
to queue as many transmit messages as possible and return immediately. A nonzero timeout value
instructs the API/DLL to wait for the timeout interval to expire before returning. The API/DLL will not
wait the entire timeout interval if an error occurs or the specified number of messages have been sent.


As I understand it, in order to receive more than one message, I need to declare an array of structures

In C, this is done like this: and then I can handle it like this: In Python, I do this and it works: And when I do this, it makes me understand what it is impossible : Actually, how do I declare this variable in Python as an array of structures so that everything works like in C.? PS I hope I explained it intelligibly...
PASSTHRU_MSG Msg[2];

msg[0].Data[12]

print(msg.Data[:100])

print(msg[0].Data[:100])




Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DaviDOSik, 2020-07-28
@DaviDOSik

msg = (PassThru_Msg * 3)()
     pMsg = ctypes.cast(msg, ctypes.POINTER(PassThru_Msg))

     numMsgs = ctypes.c_ulong(3)
     pNumMsgs = ctypes.pointer(numMsgs)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question