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

时间:2018-11-03 14:33:10

标签: c pointers struct i2c freertos

您好,我是一名电子信息通讯技术专业的学生,​​在I2C项目上遇到了一些麻烦。我正在使用FreeRTOS作为调度程序。为了在任务之间传递数据,我使用了BaseType_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait);方法,该方法需要一个项目(在我的情况下是一个结构)。我有3种结构类型,1种是具有有效负载字段的大型机,第二种是有效负载请求(C-APDU格式),最后一个结构是响应有效负载(R-APDU格式)。

下面是我的结构示例。

// 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;

问题是当我想访问数据时。 我需要传递一个指向在I2C总线上逐字节写入数据或可以计算CRC值的方法的指针 例如:

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

可以做这样的事情吗?我尝试了以下代码:

简化了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任务文件

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));
}

我是在这里寻找正确的方向还是应该尝试其他方法?

1 个答案:

答案 0 :(得分:2)

uint8_t *pointerToStructMembers = &mainframe;

由于I2CMainFrame包含指针成员void *Payload,因此无法使用类型转换。

您可以尝试按以下方式序列化mainframe

声明一个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));