简体   繁体   English

将结构转换为成员类型的指针

[英]Converting struct to pointer of member type

Hi I'm a student Electronics-ICT and I'm having some trouble with a I2C project. 嗨,我是一名学生,电子信息通讯技术,我在I2C项目中遇到了一些麻烦。 I'm using FreeRTOS as a scheduler. 我正在使用FreeRTOS作为调度程序。 To pass data between tasks I use the BaseType_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait); 为了在任务之间传递数据,我使用了BaseType_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait); method that requires a item (in my case a struct). 需要一个项目(在我的例子中是一个结构)的方法。 I have 3 struct types, 1 is the mainframe that has a payload field, the second one is a payload request (C-APDU format) and the last struct is a respond payload (R-APDU format). 我有3种结构类型,1种是具有有效负载字段的大型机,第二种是有效负载请求(C-APDU格式),最后一个结构是响应有效负载(R-APDU格式)。

Below a example of my structs. 下面是我的结构示例。

// mainframe I2C
typedef struct I2CMainFrame
{
    //RF or I2C host to M24SR64-Y:  
    //C-APDU M24SR64-Y to RF or I2C host: R-APDU
    uint8_t DID;    
    uint8_t PCB;    // PCB field
    void *Payload;  // Payload
    uint8_t CRC1;   // 2 CRC bytes
    uint8_t CRC2;
}   I2CMainFrame_t;

// payload request
typedef struct PayloadSend
{
    uint8_t CLA;    // Class byte 0x00: standard command 0xA2: ST comman
    uint8_t INS;    // Instruction byte
    uint8_t P1;     // Param Byte 1
    uint8_t P2;     // Param Byte 2
    uint8_t LC;     // Number of bytes of the Data field
    uint8_t *Data;  // Data bytes
    uint8_t Le;     // Number of bytes to be read in the M24SR64-Y memory
} PayloadSend_t;

// payload response
typedef struct PayloadGet
{
    uint8_t *Data;  // Pointer to data
    uint8_t SW1;    // status byte 1
    uint8_t SW2;    // status byte 2
} PayloadGet_t;

The problem is when i want to acces the data. 问题是当我想访问数据时。 I need to pass a pointer to a methode that writes the data byte by byte on the I2C bus or that can calculate the CRC value for example: 我需要传递一个指向方法的指针,该方法在I2C总线上逐字节写入数据或可以计算CRC值,例如:

void CalculateCRC(uint8_t *data, size_t szLen, uint8_t *outputBuffer);
void WriteDataOnI2CBus(uint8_t *data, size_t szLen);

Is it posible to do something like this? 做这样的事情可行吗? I Tried the following code: 我尝试了以下代码:

I2C UART task file simplified I2C UART任务文件已简化

I2CMainFrame_t mainframe;
PayloadSend_t payload;

void rtosUartTask(void)
{
    //Fill payloaddata
    uint8 data[] = {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01};
    payload.CLA = 0x00;      payload.INS = 0xA4;     payload.P1 = 0x04;
    payload.P2 = 0x00;       payload.LC = 0x07;
    payload.Data = data;     payload.Le = 0x00;

    //fill mainframe data
    mainframe.DID = 0xAC; mainframe.PCB = 0x02;
    mainframe.Payload = &payload;

    //methode for passing struct to I2C task
    xQueueSend(I2CQueue, &mainframe,0);
}

I2C UART task file simplified I2C UART任务文件已简化

I2CMainFrame_t mainframe;

void rtosUartTask(void)
{
     //Task manager starts this method when there is a item in the queue
     xQueueReceive(I2CQueue, &mainframe, portMAX_DELAY);
     //This doesnt work
     uint8_t *pointerToStructMembers = &mainframe;
     WriteDataOnI2CBus(pointerToStructMembers, sizeof(mainframe));
}

Am I looking in the right direction here or should I try another approach? 我在这里寻找正确的方向还是应该尝试其他方法?

uint8_t *pointerToStructMembers = &mainframe;

You cannot use typecast since I2CMainFrame contains pointer member void *Payload . 您不能使用类型转换,因为I2CMainFrame包含指针成员void *Payload

What you can try is serialize the mainframe as below. 您可以尝试按以下步骤序列化mainframe

Declare an array of uint8_t then individually copy the contents to it. 声明一个uint8_t数组,然后将内容单独复制到其中。

uint8_t bufferedStructMembers[sizeof(I2CMainFrame_t) + sizeof (PayloadSend_t) + ((PayloadSend_t *)(mainframe.Payload))->LC];


bufferedStructMembers[0] = mainframe.DID;    
bufferedStructMembers[1] = mainframe.PCB;
bufferedStructMembers[2] = ((PayloadSend_t *)(mainframe.Payload))->CLA;
bufferedStructMembers[3] = ((PayloadSend_t *)(mainframe.Payload))->INS;
bufferedStructMembers[4] = ((PayloadSend_t *)(mainframe.Payload))->P1;
bufferedStructMembers[5] =  ((PayloadSend_t *)(mainframe.Payload))->P2;
bufferedStructMembers[6] = ((PayloadSend_t *)(mainframe.Payload))->LC;
memcpy(&bufferedStructMembers[7], ((PayloadSend_t *)(mainframe.Payload))->Data, ((PayloadSend_t *)(mainframe.Payload))->LC);
bufferedStructMembers[((PayloadSend_t *)(mainframe.Payload))->LC+7] =  mainframe.Le;
bufferedStructMembers[((PayloadSend_t *)(mainframe.Payload))->LC+8] =  mainframe.CRC1;   // 2 CRC bytes
bufferedStructMembers[((PayloadSend_t *)(mainframe.Payload))->LC+9] =  mainframe.CRC2;


WriteDataOnI2CBus(bufferedStructMembers, sizeof(mainframe));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM