cancel
Showing results for 
Search instead for 
Did you mean: 

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

lgang.11
Associate

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;

3 REPLIES 3

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Karl Yamashita
Lead II

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.

I Can't Believe It's Not Butter. If you find my answers useful, click the accept button so that way others can see the solution.