cancel
Showing results for 
Search instead for 
Did you mean: 

Receive Data from UART's

Cpete.838
Associate II

Hi there

I have a question..

I have 6 Laser Sensors and I want to receive the data via 6 Uarts and then send all data via the 7th Uart. Every laser sends 4 Byte of data. Start byte starts with 1.

My questions are:

how can I make a array of buffers? I need a receive buffer for every laser.

I can distinguish the uarts with:

UART_HandleTypeDef* huart[6]={&huart1,&huart2,&huart3,&huart4,&huart5,&huart6};

but how can I do it with the different buffers?

Second question:

How do I finally transmit the data via the 7th uart?

Now what I have now:

/* Private variables ---------------------------------------------------------*

UART_HandleTypeDef huart1;

UART_HandleTypeDef huart2;

UART_HandleTypeDef huart3;

UART_HandleTypeDef huart4;

UART_HandleTypeDef huart5;

UART_HandleTypeDef huart6;

UART_HandleTypeDef huart7;

UART_HandleTypeDef huart8;

/* USER CODE BEGIN PV */

UART_HandleTypeDef* huart[6]={&huart1,&huart2,&huart3,&huart4,&huart5,&huart6};

#define LASER_COUNT 6

uint8_t transmitBuffer[4]; // every laser sends 4 byte of data

uint8_t receiveBuffer[4]; //

uint8_t rxSensor[LASER_COUNT][4]; // data from 6 lasers with 4 bytes each

uint8_t rxSensorIdx[LASER_COUNT]; // ID from which laser data are

bool rxSensorDone[LASER_COUNT]; // are all 4 byte received?

int idx;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);

static void MX_USART2_UART_Init(void);

static void MX_USART3_UART_Init(void);

static void MX_USART4_UART_Init(void);

static void MX_USART5_UART_Init(void);

static void MX_USART6_UART_Init(void);

static void MX_USART7_UART_Init(void);

static void MX_USART8_UART_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

for(int i=0;i<=5;i++){          //which laser is sending?

if(huart==&huart[i]){

idx=i;

break;

}

};

if(!rxSensorDone[idx]){         //

if(receiveBuffer[idx] == 0x80){

rxSensorIdx[idx] = 0;

}

rxSensor[idx][rxSensorIdx[idx]] = ??

rxSensorIdx[idx]++;

if(rxSensorIdx[idx] == 4){

rxSensorDone[idx]=true;

rxSensorIdx[idx] = 0;

}

}

HAL_UART_Receive_IT(&huart[idx], &receiveBuffer[idx], 1);

};

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

  

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USART1_UART_Init();

 MX_USART2_UART_Init();

 MX_USART3_UART_Init();

 MX_USART4_UART_Init();

 MX_USART5_UART_Init();

 MX_USART6_UART_Init();

 MX_USART7_UART_Init();

 MX_USART8_UART_Init();

 /* USER CODE BEGIN 2 */

  

*/

 // Start receiving the data via USART

 HAL_UART_Receive_IT(&huart1, receiveBuffer, 1);

 HAL_UART_Receive_IT(&huart2, receiveBuffer, 1);

 HAL_UART_Receive_IT(&huart3, receiveBuffer, 1);

 HAL_UART_Receive_IT(&huart4, receiveBuffer, 1);

 HAL_UART_Receive_IT(&huart5, receiveBuffer, 1);

 HAL_UART_Receive_IT(&huart6, receiveBuffer, 1);

 HAL_UART_Receive_IT(&huart7, receiveBuffer, 1);

 // Transmit data via USART // 

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

thanks so much for your help!

13 REPLIES 13
Cpete.838
Associate II

Ok. Well I though I could save some lines of code if I distinguish the different sensor data with this:

UART_HandleTypeDef* huart[6]={&huart1,&huart2,&huart3,&huart4,&huart5,&huart6};

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

Do I need more of these Callbacks?

And yes I know I need a receive buffer for all UARTs. That is what I wanted to do in the last line of my the Callback:

HAL_UART_Receive_IT(&huart[idx], &receiveBuffer[idx], 1);

I wanted to use &reveiceBuffer[idx].

Same as &huart[idx], which I achieved with this: UART_HandleTypeDef* huart[6]={&huart1,&huart2,&huart3,&huart4,&huart5,&huart6};.

I hope you understand what I mean - its maybe a bit confusing. 🙂

Why do I need 16 bytes buffer when data is only 4 bytes? Maybe 4 bytes for data and 1 byte for index?

Cpete.838
Associate II

sorry posted it twice.

Cpete.838
Associate II

OK I wrote some more code.

The idea is that in the while-Loop I check if all 4 Bytes are received. Then I copy the receiveBuffer and transmit the copied version. So that nothing gets lost or overwritten?

Is this possible so?

And:

What exactly do I Need to write in the void TxCpltCallback(UART_HandleTypeDef *huart)?

And where/how can I put the header from the sensor?

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart1;

UART_HandleTypeDef huart2;

UART_HandleTypeDef huart3;

UART_HandleTypeDef huart4;

UART_HandleTypeDef huart5;

UART_HandleTypeDef huart6;

UART_HandleTypeDef huart7;

UART_HandleTypeDef huart8;

/* USER CODE BEGIN PV */

UART_HandleTypeDef* huart[6]={&huart1,&huart2,&huart3,&huart4,&huart5,&huart6};

#define LASER_COUNT 6

//uint8_t transmitBuffer[4];

uint8_t receiveBuffer1[4];

uint8_t receiveBuffer2[4];

uint8_t receiveBuffer3[4];

uint8_t receiveBuffer4[4];

uint8_t receiveBuffer5[4];

uint8_t receiveBuffer6[4];

uint8_t receiveBufferCopy1[4];

uint8_t receiveBufferCopy2[4];

uint8_t receiveBufferCopy3[4];

uint8_t receiveBufferCopy4[4];

uint8_t receiveBufferCopy5[4];

uint8_t receiveBufferCopy6[4];

uint8_t *receiveBuffer[6]= {&receiveBuffer1[4], &receiveBuffer2[4], &receiveBuffer3[4], &receiveBuffer4[4], &receiveBuffer5[4], &receiveBuffer6[4]};

uint8_t *receiveBufferCopy[6]= {&receiveBufferCopy1[4], &receiveBufferCopy2[4], &receiveBufferCopy3[4], &receiveBufferCopy4[4], &receiveBufferCopy5[4], &receiveBufferCopy6[4]};

uint8_t rxSensor[LASER_COUNT][4];

uint8_t rxSensorIdx[LASER_COUNT];

bool rxSensorDone[LASER_COUNT];

int idx;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);

static void MX_USART2_UART_Init(void);

static void MX_USART3_UART_Init(void);

static void MX_USART4_UART_Init(void);

static void MX_USART5_UART_Init(void);

static void MX_USART6_UART_Init(void);

static void MX_USART7_UART_Init(void);

static void MX_USART8_UART_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

void RxCpltCallback(UART_HandleTypeDef *huart, uint8_t *receiveBuffer)

{

 for(int i=0;i<=5;i++){

  if(huart==&huart[i]){

   idx=i;

   break;

  }

 };

 if(!rxSensorDone[idx]){

  if(receiveBuffer[idx] == 0x80){

   rxSensorIdx[idx] = 0;

   }

 rxSensor[idx][rxSensorIdx[idx]] = receiveBuffer[idx];

 rxSensorIdx[idx]++;

 if(rxSensorIdx[idx] == 4){

  rxSensorDone[idx]=true;

  rxSensorIdx[idx] = 0;

 }

 }

 HAL_UART_Receive_IT(&huart[idx], &receiveBuffer[idx], 1);

};

void TxCpltCallback(UART_HandleTypeDef *huart)

{

}

int main(void)

{

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* Configure the system clock */

 SystemClock_Config();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USART1_UART_Init();

 MX_USART2_UART_Init();

 MX_USART3_UART_Init();

 MX_USART4_UART_Init();

 MX_USART5_UART_Init();

 MX_USART6_UART_Init();

 MX_USART7_UART_Init();

 MX_USART8_UART_Init();

 // Start receiving the data via USART

 HAL_UART_Receive_IT(&huart[idx], &receiveBuffer[idx], 1);

 while (1)

 {

  for(int k=0;k<5;k++){

   if(rxSensorDone[k]==true){

    receiveBuffer[k]=receiveBufferCopy[k];

    HAL_UART_Transmit_IT(&huart7, &receiveBufferCopy[k], 1);

    rxSensorDone[k]=false;

   }

  };

 }

}

Piranha
Chief II

> What exactly do I Need to write in the void TxCpltCallback(UART_HandleTypeDef *huart)?

So you have done absolutely nothing, except for a few clicks in CubeMX and generating that useless bloatware, and know you are asking for a "little help" - others doing essentially all of your project instead of you. No? Then here is a precise instruction:

  1. Use "Code Snippet" button.
  2. Learn using arrays, multidimensional arrays, structures, etc. and C language in general.
  3. Learn MCU register programming and driver development.
  4. Make actually working UART driver, not a non-working crap like HAL drivers are.
  5. Make your UART driver to be usable with multiple instances.
  6. Design the communication synchronization principles for your multiple laser usage scenario.
  7. Profit!