cancel
Showing results for 
Search instead for 
Did you mean: 

I hve to Receive the serial data throught dock light but cant able to receive the proper data .

Yugenthar
Senior

Hello Everyone.

I have one doubt.

Im Try to receive the serial data frame from dock light but .I cant able to receive serially.

my Frame HEX Formate :81 03 00 01

This my code:

#include "main.h"

#define LABEL 0x81

#define SSM 0x03

#define SDI 0x00

#define PARITY 0x01

uint16_t Rx_data[5];

uint16_t Read[1];

uint16_t msg[5];

uint16_t *Receive;

uint16_t RX[5];

int main(void)

{

HAL_Init();

 SystemClock_Config();

MX_GPIO_Init();

 MX_USART1_UART_Init();

while(1)

{

  

 Receive=Transmit();

 for(int j=0;j<4;j++)

   {

 HAL_UART_Transmit(&huart1, (uint8_t*)msg, 5, 10);

 sprintf(msg,"%d ",Receive[j]);

   }

 if(Receive[0]==LABEL)

 {

 if(Receive[1]==SSM)

 {

 if(Receive[2]==SDI)

 {

 if(Receive[3]==PARITY)

 {

 HAL_UART_Transmit(&huart1, (uint8_t*)msg, 5, 10);

 sprintf(msg,"%d ",Receive[0]);

 }

 }

 }

 }

 }

}

int *Transmit()

{

  uint8_t Read[5];

  uint8_t RX[5];

  for(int i=0;i<4;i++)

  {

Read[i]=HAL_UART_Receive (&huart1, Rx_data, 4, 100);

  }

return Read;

}

Please give the solution.

Output:Docklight

[TX]=81 03 00 01

[RX]=08 40 50 00

this my Experted output:

[TX]=81 03 00 01

[RX]=81 03 00 01

1 ACCEPTED SOLUTION

Accepted Solutions
Yugenthar
Senior

its not transmitingthe data actual give your hardware congfiguration

View solution in original post

6 REPLIES 6
Bob S
Principal

So many things.... To start with:

  • Look up what HAL_UART_Receive() actually does, where it puts the data and what values it returns. Hint - it doesn't return the UART data, it returns a status code.
  • Transmit() returns a pointer to a LOCAL array, which disappears as soon as the function returns. You code might APPEAR to work only as long as you don't call any other function or get any interrupts. But don't count on it.
  • Once you figoure out how HAL_UART_Receive() actually works, re-visit your for() loops
Karl Yamashita
Lead III
#define LABEL 0x81
#define SSM 0x03
#define SDI 0x00
#define PARITY 0x01
 
#define DATA_SIZE 4
#define ARRAY_SIZE 16
uint8_t rxData[ARRAY_SIZE] = {0};
uint8_t txData[ARRAY_SIZE] = {0};
bool dataRdy = false;
 
int main(void)
{
 
  // init interrupt
  UART_EnInt();
 
  while (1)
  {
		if (dataRdy)
		{
			dataRdy = false;
 
			if (rxData[0] == LABEL) {
				if (rxData[1] == SSM) {
					if (rxData[2] == SDI) {
						if (rxData[3] == PARITY) {
							memcpy(txData, rxData, DATA_SIZE);
							HAL_UART_Transmit(&huart2, txData, DATA_SIZE, 100);
						}
					}
				}
			}
		}
 
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if(huart->Instance == huart2.Instance)
	{
		UART_EnInt();
 
		if(Size == DATA_SIZE)
		{
			dataRdy = true;
		}
	}
}
 
// enable interrupt with idle
void UART_EnInt(void)
{
	if (HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rxData, ARRAY_SIZE ) != HAL_OK)
	{
		Error_Handler();
	}
}
 
 
 

This code will give you a basic idea of how to receive binary data. When HAL_UARTEx_RxEventCallback() is called, the rxData already holds the data so you can set a flag so you know there is new data ready. Then check the flag in the while loop.

Using HAL_UARTEx_ReceiveToIdle_DMA() allows you to receive a variable length data packet. In this case you are wanting to received 4 bytes. So even though the DMA is expecting 16 bytes, the Idle bit will cause an interrupt after 4 bytes are sent from Docklight. You can check the Size to be sure you did receive 4 bytes for a complete packet and then set a flag.

The 1st packet matches your format so a copy of it is transmitted back to Docklight. The 2nd packet doesn't match so the code ignores it

0693W00000YAQMnQAP.png

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
Yugenthar
Senior

Thank you bro.....i will compile

thank you bro

Yugenthar
Senior

its not transmitingthe data actual give your hardware congfiguration

0693W00000YAQtDQAX.png