cancel
Showing results for 
Search instead for 
Did you mean: 

UART HMI Display Receive-Transmit Issue

frkdkk
Associate II

Hi,

I am Faruk.

What i am trying to do is, sending data that i collacted from ADC Ports to Nextion HMI Display. Also at the same time i am getting signals from buttons on the Nextion HMI Display. I use UART pins of Nextion HMI Display. 5V RX TX GND.

If i dont transmit any data to HMI Display i can get signals from buttons on HMI Display. One of the buttons start saving data to SD Card. The other ones increase and decrease the output signal that goes out from DAC. 

When i use receive and transmit at the same time. Only transmit section works. I cannot get signal from buttons on HMI Display. I am uploading the code here. I just couldnt figure it out why it doesent work correctly. I need some help about this situation. This is my final project. I am gonna test it on Friday. So i have to figure it out. I also uploaded the main.c file. 

 

//Data sending to Nextion HMI Display
void nextion_Send_Float(char *obj, float num, int dp)
{
 
int32_t number = num*(pow(10,dp));
 
uint8_t *nextion_Buffer = malloc(50*sizeof(char));
int len = sprintf((char *)nextion_Buffer, "%s.vvs1=%d", obj, dp);
HAL_UART_Transmit(&huart3, nextion_Buffer, len, 10);
HAL_UART_Transmit(&huart3, end_Command, 3, 10);
 
len = sprintf((char *)nextion_Buffer, "%s.val=%ld", obj, number);
HAL_UART_Transmit(&huart3, nextion_Buffer, len, 10);
HAL_UART_Transmit(&huart3, end_Command, 3, 10);
 
 
free(nextion_Buffer);
 
 
 
}
 
 
void nextionData()
{
 
nextion_Send_Float("x6", Vout, 2);
nextion_Send_Float("x7", Iout, 2);
nextion_Send_Float("x8", Vout, 2);
nextion_Send_Float("x9", Iout, 2);
nextion_Send_Float("x10", V1L, 2);
nextion_Send_Float("x11", I1L, 2);
nextion_Send_Float("x12", V2L ,2);
nextion_Send_Float("x13", I2L, 2);
nextion_Send_Float("x14", V3L, 2);
nextion_Send_Float("x15", I3L, 2);
nextion_Send_Float("x16", V12L, 2);
nextion_Send_Float("x17", I12L, 2);
nextion_Send_Float("x18", V23L, 2);
nextion_Send_Float("x19", I23L, 2);
nextion_Send_Float("x20", V13L, 2);
nextion_Send_Float("x21", I13L, 2);
nextion_Send_Float("x22", temp, 2);
nextion_Send_Float("x0", Vout, 2);
nextion_Send_Float("x1", Iout, 2);
 
}
 
 
 
//Getting signal from Buttons on HMI Display. Also changing the output value with DAC.
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == &huart3)
{
 
 
if(RX_Data[2] == 0x03)
{
 
if(dac_V == 0){ dac_V = 0; }
else { dac_V -= 0.5; }
send_V = (int)map(dac_V,0,20,0,1275);
 
 
}
 
if(RX_Data[2] == 0x04)
{
if(dac_V == 20){ dac_V = 20; }
else{ dac_V += 0.5; }
send_V = (int)map(dac_V,0,20,0,1275);
 
 
}
 
if(RX_Data[2] == 0x05)
{
if(dac_I == 0){ dac_I = 0; }
else{ dac_I -= 50; }
send_I = (int)map(dac_I,0,3000,0,1275);
 
}
 
if(RX_Data[2] == 0x06)
{
if(dac_I == 3000){ dac_I = 3000; }
else{ dac_I += 50; }
send_I = (int)map(dac_I,0,3000,0,1275);
 
}
 
 
 
 
if(RX_Data[3] == 0x30)
{
sd_Flag=START;
}
 
else if(RX_Data[3] == 0x31)
{
sd_Flag=STOP;
}
 
 
 
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R,send_V);
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R,send_I);
 
 
  HAL_UART_Receive_IT(&huart3, RX_Data, sizeof(RX_Data));
 
 
}
 
 
 
 
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)  
{
 
if(htim->Instance == TIM1) // Interrupts every 50us for ADC to collact data
{
 
HAL_ADC_Start_DMA(&hadc1, (uint32_t*) adc_Buffer, sizeof(adc_Buffer)); 
 
}
 
if(htim->Instance == TIM8)  // Interrupts every 1sec to tranmit data to HMI Display
{
 
  nextionData();
 
}
 
 
}

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
frkdkk
Associate II

Hi,

I figured out the problem. I was trying to send data to other pages while on a specific page. Basically, what happens is that the Nextion HMI gives an error code if I try to send data when I'm not on the correct page. So, I classified the transmitted data according to the page data received. Here is the code that works fine.

 

 

while (1)
  {
   
	  if(u_Flag==OK){

		if (RX_Data[1] == 0x31) {
			nextion_Send_String("t11", "hello");
		}

		else if (RX_Data[1] == 0x32) {
			nextion_Send_String("t21", "hello");
		}

		else if (RX_Data[1] == 0x33) {
			nextion_Send_String("t31", "hello");
		}

		else if (RX_Data[1] == 0x34) {
			nextion_Send_String("t41", "hello");
		}

		else if (RX_Data[1] == 0x30 || RX_Data[1] == 0x80) {
			nextion_Send_String("t01", "hello");


		}



		u_Flag=NOK;
	  }


	  if(Flag==OKI){

		    if (RX_Data[1] == 0x31) {
				a = 1;
			}

			else if (RX_Data[1] == 0x32) {
				a = 2;
			}

			else if (RX_Data[1] == 0x33) {
				a = 3;
			}

			else if (RX_Data[1] == 0x34) {
				a = 4;
			}

			else if (RX_Data[1] == 0x30 || RX_Data[1] == 0x80) {
				a = 0;
			}


			if (RX_Data[2] == 0x03) {

				b = 3;

			}

			else if (RX_Data[2] == 0x04) {
				b = 4;

			}

			else if (RX_Data[2] == 0x05) {
				b = 5;

			}

			else if (RX_Data[2] == 0x06) {
				b = 6;

			}


			if (RX_Data[3] == 0x30) {
				c = 30;
			}

			else if (RX_Data[3] == 0x31) {
				c = 31;
			}

			Flag=NOKI;

	  }

 

View solution in original post

4 REPLIES 4
Andrew Neil
Evangelist III

@frkdkk wrote:

When i use receive and transmit at the same time. Only transmit section works. I cannot get signal from buttons on HMI Display


Not sure what you mean here?

"Transmit" refers to sending stuff from your STM32 to the Display - yes?

"Receiving" refers to stuff coming from the Display into your STM32 - yes?

What "signal" do expect to get from the Display ?

Is the problem with the Display not sending, or with the STM32 not receiving?

Please see the posting tips for how to properly post source code:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 

Karl Yamashita
Lead III

use the </> when posting code so that it gets properly formatted. Makes it easier to read the code.

 

You're doing to much processing in the HAL_UART_RxCpltCallback

Instead, save the data to a secondary buffer and set a flag, enable the UART interrupt, then exit the interrupt.

Then check the flag in your main while loop and do the processing of the secondary buffer. All the while the UART can still receive data in the primary buffer during your processing. 

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.

Now that i look at more of your code, you are using blocking code when transmitting. You should use interrupts or DMA along with a queue buffer.

See this project https://github.com/karlyamashita/Nucleo-G431RB_Three_UART/wiki

 

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.
frkdkk
Associate II

Hi,

I figured out the problem. I was trying to send data to other pages while on a specific page. Basically, what happens is that the Nextion HMI gives an error code if I try to send data when I'm not on the correct page. So, I classified the transmitted data according to the page data received. Here is the code that works fine.

 

 

while (1)
  {
   
	  if(u_Flag==OK){

		if (RX_Data[1] == 0x31) {
			nextion_Send_String("t11", "hello");
		}

		else if (RX_Data[1] == 0x32) {
			nextion_Send_String("t21", "hello");
		}

		else if (RX_Data[1] == 0x33) {
			nextion_Send_String("t31", "hello");
		}

		else if (RX_Data[1] == 0x34) {
			nextion_Send_String("t41", "hello");
		}

		else if (RX_Data[1] == 0x30 || RX_Data[1] == 0x80) {
			nextion_Send_String("t01", "hello");


		}



		u_Flag=NOK;
	  }


	  if(Flag==OKI){

		    if (RX_Data[1] == 0x31) {
				a = 1;
			}

			else if (RX_Data[1] == 0x32) {
				a = 2;
			}

			else if (RX_Data[1] == 0x33) {
				a = 3;
			}

			else if (RX_Data[1] == 0x34) {
				a = 4;
			}

			else if (RX_Data[1] == 0x30 || RX_Data[1] == 0x80) {
				a = 0;
			}


			if (RX_Data[2] == 0x03) {

				b = 3;

			}

			else if (RX_Data[2] == 0x04) {
				b = 4;

			}

			else if (RX_Data[2] == 0x05) {
				b = 5;

			}

			else if (RX_Data[2] == 0x06) {
				b = 6;

			}


			if (RX_Data[3] == 0x30) {
				c = 30;
			}

			else if (RX_Data[3] == 0x31) {
				c = 31;
			}

			Flag=NOKI;

	  }