Skip to main content
Visitor II
April 14, 2023
Question

How to transmit structure variables from .h library via the serial port?

  • April 14, 2023
  • 2 replies
  • 1515 views

I am trying to transmit variables via the uart2 serial port from GY-521(mpu6050) library, however, all my attempts are unsuccessful.

typedef init in the main.c:

MPU6050_t sensor_data;

main.c area to put HAL_UART_Transmit():

 while (1)

 {

  MPU6050_Read_All(&hi2c1, &sensor_data);

  HAL_Delay(100);

 }

.h needed structure variables to transmit into serial port:

typedef struct

{

  int16_t Accel_X_RAW;

  int16_t Accel_Y_RAW;

  int16_t Accel_Z_RAW;

  double Ax;

  double Ay;

  double Az;

  int16_t Gyro_X_RAW;

  int16_t Gyro_Y_RAW;

  int16_t Gyro_Z_RAW;

  double Gx;

  double Gy;

  double Gz;

  float Temperature;

  double KalmanAngleX;

  double KalmanAngleY;

} MPU6050_t;

    This topic has been closed for replies.

    2 replies

    Graduate II
    April 14, 2023

    It will send as a binary blob, not ASCII formatted nor synchronized.

    C​ast a pointer to the structure, and use sizeof() to determine length to send

    Graduate II
    April 14, 2023

    In the form HAL_UART_Transmit(&hUart2, (uint8_t *)&sensor_data, sizeof(sensor_data), 1000);

    Unsuccessful how exactly? The MCU doesn't physically send the data structure, or you can't interpret or process it on the host system?

    Graduate II
    April 14, 2023

    In the function MPU6050_Read_All, all you need to do is to read the 14 bytes as raw values. Send those 14 bytes over UART to the receiving device. Then the device can calculate the raw values into double or float values using the same calculations that the MPU6050_Read_All function has in it.