moon
moon

Reputation: 1

How to receive and read user input through UART (Embedded C)

I am trying position control of DC motor with AS5600 encoder with position as input from user through UART but I am unable to do so. If I give a 4 digit input say 1222, only the '1' in ASCII value is stored in Buffer array.

I tried creating an array of array to store each digits of the input through a for loop which are stored as ASCII value in the array and then convert them back to decimal.

  for (int j = 0; j < NUM_DIGITS; j++)
  {
    HAL_UART_Receive(&huart2, rxBuffer[i][j], 1, 1000);
    intValue = (intValue * 10) + (rxBuffer[i][j] - '0');
  }

Upvotes: 0

Views: 822

Answers (1)

0___________
0___________

Reputation: 67820

If rxBuffer[i][j] is a 2D array then it means that you do not read the warnings. If it is the case you need to pass address of this element (ie (uint8_t *)&rxBuffer[i][j])

You also do not check the result of the HAL function.

Example of simple line reading function (:

HAL_StatusTypeDef getString(char *buff, size_t size)
{
    size_t pos = 0;
    HAL_StatusTypeDef result;
    while(pos < size - 1 && (result = HAL_UART_Receive(huart, (uint8_t *)(buff + pos), 1)) == HAL_OK)
    {
        if(buff[pos++] == '\n') {pos--;break;}
    }
    buff[pos] = 0;
    return result;
}

Upvotes: 0

Related Questions