Advertisement
LeonardoLW

Arduino read string from serial

Nov 15th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. /*
  2. * Serial.c
  3. *
  4. * Created: 18/10/2019 18:14:50
  5. * Author : Leonardo Lisa
  6. */
  7.  
  8. #define F_CPU      16000000UL
  9. #define BAUD       9600
  10. #define MAX_BUFFER_LENGTH 64
  11. #define USART_RX_MAX_BUFFER_LENGTH 20
  12.  
  13. #define BRC    ((F_CPU/16/BAUD) - 1)
  14.  
  15. #include <avr/io.h>
  16. #include <stdlib.h>
  17. #include <avr/eeprom.h>
  18.  
  19. void USART_Begin(unsigned int brc)
  20. {
  21.     // Set baud rate.
  22.     UBRR0H = (brc >> 8);
  23.     UBRR0L = brc;
  24.     // Enable receiver and transmitter with their interrupt.
  25.     UCSR0B |= (1 << TXEN0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << RXCIE0);
  26.     // Set frame format yo 8bit data.
  27.     UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
  28. }
  29.  
  30. // Send one byte.
  31. void USART_Transmit(unsigned char data)
  32. {
  33.     /* Wait for empty transmit buffer */
  34.     while (!(UCSR0A & (1<<UDRE0)));
  35.     /* Put data into buffer, sends the data */
  36.     UDR0 = data;
  37. }
  38.  
  39. // Receive one byte.
  40. unsigned char USART_Receive(void)
  41. {
  42.     /* Wait for data to be received */
  43.     while (!(UCSR0A & (1<<RXC0)));
  44.     /* Get and return received data from buffer */
  45.     return UDR0;
  46. }
  47.  
  48. void USART_Transmit_Buffer(char *buffer)
  49. {
  50.     char error_message[] = "ERROR: Empty message or null pointer\n";
  51.     // Get message length.
  52.     if (buffer == NULL)
  53.     {
  54.         buffer = error_message;
  55.     }
  56.     // Print buffer message.
  57.     for (uint8_t size = 1; *buffer != '\0' && size != MAX_BUFFER_LENGTH; size++, buffer++)
  58.     {
  59.         USART_Transmit(*buffer);
  60.     }
  61. }
  62.  
  63. void *USART_Receive_Buffer(void)
  64. {
  65.     // Allocate n. byte for the message buffer + one byte for '\0' termination character.
  66.     char *buffer = calloc((USART_RX_MAX_BUFFER_LENGTH + 1), 8);
  67.     char *tmp_buffer = buffer;
  68.     if (buffer == NULL)
  69.     {
  70.         return NULL;
  71.     }
  72.     for (uint8_t size = 1; size < USART_RX_MAX_BUFFER_LENGTH; size++, buffer++)
  73.     {
  74.         *buffer = USART_Receive();
  75.         if (*buffer == '\0')
  76.         {
  77.             if (size == 1)
  78.             {
  79.                 free(buffer);
  80.                 return NULL;
  81.             }
  82.             return realloc(tmp_buffer, size);
  83.         }
  84.     }
  85.     // buffer++;
  86.     // *buffer = '\0'; (remember that '\0' == 0).
  87.     // Not necessary because calloc have already cleared the allocated memory to zero.
  88.     return tmp_buffer;
  89. }
  90.  
  91.  
  92. int main(void)
  93. {
  94.     // Same as write: char message[]{'S','e','r','i','a','l',' ','O','N','\n'};
  95.     // Compiler place '\0' at the end of the string.
  96.     char message[] = "Serial On\n";
  97.     // No '\0' character at the end of the string. DANGEROUS!
  98.     char message2[] = {'1','2','3','4','5'};
  99.     char *temp;
  100.  
  101.     USART_Begin(BRC);
  102.     USART_Transmit_Buffer(message);
  103.     USART_Transmit_Buffer(message2);
  104.     while (1)
  105.     {
  106.         temp = USART_Receive_Buffer();
  107.         USART_Transmit_Buffer(temp);
  108.         // IMPORTANT! Clean your memory.
  109.         free(temp);
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement