Receive Data from UART's
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!
