2024-03-08 02:10 AM - last edited on 2024-03-08 02:32 AM by Peter BENSCH
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
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);
}
2024-03-08 02:14 AM - edited 2024-03-08 02:15 AM
Please use this button to properly post source code:
2024-03-08 02:34 AM
@soundarya_h_s For your convenience, I have prettied it up with a code beautifier and inserted it with </>.
2024-03-08 02:43 AM
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);
}
2024-03-08 02:47 AM
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.
2024-03-08 02:52 AM
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
2024-03-08 03:09 AM
@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?
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 ?
2024-03-08 03:19 AM
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
2024-03-08 03:38 AM
@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?
2024-03-08 03:59 AM