gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:24:35 26 4
gpt4 key购买 nike

大家好,我是一名电子信息与通信技术专业的学生,​​我在 I2C 项目中遇到了一些问题。我正在使用 FreeRTOS 作为调度程序。为了在任务之间传递数据,我使用了 BaseType_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait); 方法,它需要一个项目(在我的例子中是一个结构)。我有 3 种结构类型,第一种是具有有效载荷字段的大型机,第二种是有效载荷请求(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));
}

我是在寻找正确的方向还是应该尝试另一种方法?

最佳答案

uint8_t *pointerToStructMembers = &mainframe;

您不能使用类型转换,因为 I2CMainFrame 包含指针成员 void *Payload

您可以尝试如下序列化大型机

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

关于将结构转换为成员类型的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53132308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com