c - 将两个消息结构链接在一起

标签 c embedded can-bus iar type-punning

我有两个略有不同的结构,我想以某种方式链接它们以获得最快的结果。

这是两个结构:

/** 
  * @brief  CAN Tx message structure definition  
  */
typedef struct
{
  uint32_t StdId;    /*!< Specifies the standard identifier.
                          This parameter must be a number between Min_Data = 0 and Max_Data = 0x7FF */

  uint32_t ExtId;    /*!< Specifies the extended identifier.
                          This parameter must be a number between Min_Data = 0 and Max_Data = 0x1FFFFFFF */

  uint32_t IDE;      /*!< Specifies the type of identifier for the message that will be transmitted.
                          This parameter can be a value of @ref CAN_Identifier_Type */

  uint32_t RTR;      /*!< Specifies the type of frame for the message that will be transmitted.
                          This parameter can be a value of @ref CAN_remote_transmission_request */

  uint32_t DLC;      /*!< Specifies the length of the frame that will be transmitted.
                          This parameter must be a number between Min_Data = 0 and Max_Data = 8 */

  uint8_t Data[8];   /*!< Contains the data to be transmitted.
                          This parameter must be a number between Min_Data = 0 and Max_Data = 0xFF */

}CanTxMsgTypeDef;

/**
  * @brief  CAN Rx message structure definition
  */
typedef struct
{
  uint32_t StdId;       /*!< Specifies the standard identifier.
                             This parameter must be a number between Min_Data = 0 and Max_Data = 0x7FF */

  uint32_t ExtId;       /*!< Specifies the extended identifier.
                             This parameter must be a number between Min_Data = 0 and Max_Data = 0x1FFFFFFF */

  uint32_t IDE;         /*!< Specifies the type of identifier for the message that will be received.
                             This parameter can be a value of @ref CAN_Identifier_Type */

  uint32_t RTR;         /*!< Specifies the type of frame for the received message.
                             This parameter can be a value of @ref CAN_remote_transmission_request */

  uint32_t DLC;         /*!< Specifies the length of the frame that will be received.
                             This parameter must be a number between Min_Data = 0 and Max_Data = 8 */

  uint8_t Data[8];      /*!< Contains the data to be received.
                             This parameter must be a number between Min_Data = 0 and Max_Data = 0xFF */

  uint32_t FMI;         /*!< Specifies the index of the filter the message stored in the mailbox passes through.
                             This parameter must be a number between Min_Data = 0 and Max_Data = 0xFF */

  uint32_t FIFONumber;  /*!< Specifies the receive FIFO number.
                             This parameter can be CAN_FIFO0 or CAN_FIFO1 */

}CanRxMsgTypeDef;

它们是用于 ARM 的 IAR 嵌入式工作台的 ST 驱动程序的一部分,因此我无法更改它们。

我的功能主要是进行过滤,这意味着无论我收到什么,我都需要几乎立即传输。

驱动程序函数(我也无法更改)仅允许传输 CanTxMsgTypeDef 类型。所以每次我都需要将 CanRxMsgTypeDef 变量复制到 CanTxMsgTypeDef 变量,这是非常低效的。

我正在寻找获得最快结果的最佳实践,即以最少的复制粘贴传输 CanRxMsgTypeDef 数据。

请注意 CanRxMsgTypeDef 如何只有额外的数据,即所有要传输的信息已经在接收到的 CanRxMsgTypeDef 变量中。

最佳答案

使用两个 structunion,您可以根据 6.5.2.3p6 从公共(public)初始部分访问任何成员:

One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

示例代码:

union common
{
    CanRxMsgTypeDef rx;
    CanTxMsgTypeDef tx;
};

void someFunc(CanTxMsgTypeDef arg)
{
    printf("inside func tx.StdId=%d\n",arg.StdId);
    printf("inside func tx.DLC=%d\n",arg.DLC);
}

int main()
{
    /* Assign RX type to the union */
    union common test = {1,2,3,4,5,{0,0,0,0,0,0,0,0},7,8};

    printf("test.tx.StdId=%d\n",test.tx.StdId);
    printf("test.rx.StdId=%d\n",test.rx.StdId);
    printf("test.tx.DLC=%d\n",test.tx.DLC);
    printf("test.rx.DLC=%d\n",test.rx.DLC);
    printf("test.rx.FMI=%d\n",test.rx.FMI);

    /* Use the union as tx type */
    someFunc(test.tx);
    return 0;
}

关于c - 将两个消息结构链接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53389343/

相关文章:

没有malloc的C编程

c - 内存中有很大的空白空间?

c - 使用 AVR 红外传感器发光 LED 的这段代码有什么问题?

c - 如何使用 STM32 HAL_CAN 库生成格式正确的 TX CAN 帧?

python - 实现 Python CANopen

android - Android 上的 CAN( Controller 区域网络)

c - 从 Linux 移植到 Windows 8 : Struct member names

c - 使用线程的段错误

c++ - 哪些高级语言可以轻松与/C++ 交互?

c - int main() 和 void main() 是如何工作的?