cancel
Showing results for 
Search instead for 
Did you mean: 

UART 4 bit numeric value communication

soundarya_h_s
Associate III

I am doing the project to control linear motor, For that I am using UART to send start and stop command to start and stop motor, as like this i sending different rpm value from 100 to 3000 randomly to change the ramp of the motor.

The problem that i am facing is for 3 digit number RPM i am getting output, but when is comes to 4 digit value i am not getting proper putput, 
For example:

For 1000 RPM, I am getting 100 RPM as out put

soundarya_h_s_0-1709892543564.png

  • Baud Rate: 115200 bps
  • Data Bits: 8
  • Parity: None
  • Stop Bits: 1

 

void
HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart)
{
  int cmd_ok = 0;
  rxbuffer[strlen(rxbuffer)] = '\0';
  if (strncmp((const char*)rxbuffer, "start", 6) == 0)
  {
    rxbuffer[strlen(rxbuffer)] = '\0';
    flag = 1;
    cmd_ok = 1;
    // MC_StartMotor1();
  }
  else if (strncmp((const char*)rxbuffer, "stop", 5) == 0)
  {
    rxbuffer[strlen(rxbuffer)] = '\0';
    flag = 0;
    cmd_ok = 1;
    // MC_StopMotor1();
  }
  else
  {
    rxbuffer[strlen(rxbuffer)] = '\0';
    char* endptr;
    uint16_t rpmValue = strtol((char*)rxbuffer, &endptr, 10);
    // rpmValue = atoi((char*)rxbuffer);
    if (rxbuffer[0] != '\0' && *endptr == '\0' && rpmValue >= 100 && rpmValue <= 3000)
    {
      MC_ProgramSpeedRampMotor1(rpmValue, 30000);
      // HAL_Delay(60000);
      cmd_ok = 1;
    }
  }

  if (cmd_ok == 1)
  {
    memset(rxbuffer, 0, sizeof(rxbuffer));
    cmd_ok = 0;
    rxindex = 0;
  }
  else
  {
    rxindex++;
  }
  HAL_UART_Receive_IT(&huart2, ((uint8_t*)rxbuffer) + strlen(rxbuffer), 1);
}
18 REPLIES 18

Please use this button to properly post source code:

AndrewNeil_0-1709892866672.png

 

Peter BENSCH
ST Employee

@soundarya_h_s For your convenience, I have prettied it up with a code beautifier and inserted it with </>.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{


		int cmd_ok = 0;

		rxbuffer[strlen(rxbuffer)] = '\0';

		if (strncmp((const char*)rxbuffer, "start", 6) == 0)
		{
			rxbuffer[strlen(rxbuffer)] = '\0';
			flag = 1;
			cmd_ok = 1;
			//MC_StartMotor1();

		}
		else if (strncmp((const char*)rxbuffer, "stop", 5) == 0)
		{
			rxbuffer[strlen(rxbuffer)] = '\0';
			flag = 0;
			cmd_ok = 1;
			//MC_StopMotor1();
		}
		else
		{
			rxbuffer[strlen(rxbuffer)] = '\0';
			char *endptr;
			uint16_t rpmValue = strtol((char*)rxbuffer, &endptr, 10);

			//rpmValue = atoi((char*)rxbuffer);
			if (rxbuffer[0] != '\0' && *endptr == '\0' && rpmValue >= 100 && rpmValue <= 3000)
			{
				MC_ProgramSpeedRampMotor1(rpmValue,30000);
				//HAL_Delay(60000);
				 cmd_ok = 1;
			}

		}
		if(cmd_ok == 1)
		{
			memset(rxbuffer, 0, sizeof(rxbuffer));
			cmd_ok = 0;
			rxindex = 0;
		}
		else
		{
			rxindex++;
		}

		HAL_UART_Receive_IT(&huart2, ((uint8_t *)rxbuffer) + strlen(rxbuffer), 1);

}

A bit confused by this: Is the problem in the sending, or the receiving?

You've only shown receive code, but are you certain that the transmitting code is working properly?

Have you tried stepping the code to see what happens, character-by-character, as the command is received?

It looks like your commands are text-based, so you could test using a terminal ...

 

What is going on here:


@soundarya_h_s wrote:

 

 

 

  rxbuffer[strlen(rxbuffer)] = '\0';

 


strlen is only going to work if the rxbuffer is already NULL-terminated - so this is either pointless, or broken.

 

Start and stop commands are working properly, when it comes to sending 4 digit numeric value through uart, it is taking as 3 digit number


@soundarya_h_s wrote:

 when it comes to sending 4 digit numeric value through uart, it is taking as 3 digit number


Still unclear whether the problem is in the sending or the receiving?

  1. Have you confirmed that the commands are being sent correctly?
  2. If the sending is correct, Have you tried stepping the receiving code to see what happens, character-by-character, as the command is received?

Again, It looks like your commands are text-based, so you could test using a terminal ...


You titled the post, "UART 4 bit (sic?) numeric value communication" - is that a typo?

Did you mean 4-digit ?

 

1. commands are being send and received correctly.

2. I also tried stepping the receive code, characters are receiving properly 

 

Yes i did typo mistake in title, That's not bit , that's 4-digit

 


@soundarya_h_s wrote:

1. commands are being send and received correctly.


But clearly they are not !

You say the problem is that 4-digit numbers are not handled correctly!

 


@soundarya_h_s wrote:

2. I also tried stepping the receive code, characters are receiving properly 


and what did you learn from that?

 

soundarya_h_s_0-1709899135966.png