# UART Receive and Transmit Using FreeRTOS, Queue and Semaphore

**URL:** <https://forums.freertos.org/t/uart-receive-and-transmit-using-freertos-queue-and-semaphore/11316>\
**Category:** Microchip\
**Created:** [December 10, 2020, 12:17am UTC](https://forums.freertos.org/t/uart-receive-and-transmit-using-freertos-queue-and-semaphore/11316 "2020-12-10T00:17:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![furkanmola](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/furkanmola/32/1097_2.png) [@furkanmola](https://forums.freertos.org/u/furkanmola)\
**Post date:** [December 10, 2020, 12:17am UTC](https://forums.freertos.org/t/uart-receive-and-transmit-using-freertos-queue-and-semaphore/11316/1 "2020-12-10T00:17:37Z")

</div>

hi,

i’m trying to receive string from computer then transmit to another device by using freertos. when i start on proteus, i only see one message although i’m sending different things. here is my code and screenshot:

```
int main()
{
  uart_start();
  uart_sendstr(uart0, "hello, i'm a transmitter\n\r");
  if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been created.
  {
    xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage the Serial Port
    if ( ( xSerialSemaphore ) != NULL )
      xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the Semaphore.
  }

  queue = xQueueCreate(10, sizeof(char));
  if (queue == NULL) {
    uart_sendstr(uart0, "Queue can not be created");
  }
 
  xTaskCreate(
    TaskRead
    , "Receive" // A name just for humans
    , 128 // This stack size can be checked & adjusted by reading the Stack Highwater
    , NULL
    , 1 // Priority, with 1 being the highest, and 4 being the lowest.
    , NULL );

  xTaskCreate(
    TaskTransmit
    , "Transmit"
    , 128 // Stack size
    , NULL
    , 2 // Priority
    , NULL );
  vTaskStartScheduler();
 /* while(1)
  {
     uart_sendstr(uart0, uart_getstring(uart1, 1));
     _delay_ms(100);
  }*/
}

void loop()
{
  // put your main code here, to run repeatedly:
}

void TaskRead(void *pvParameters)
{
  while(1)
  {
    char* receive;
    if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
    {
     receive = uart_getstring(uart1, 10);
     xQueueSend(queue, &receive, portMAX_DELAY);
     xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
    }
    vTaskDelay(1);
  }
}

void TaskTransmit(void *pvParameters)
{
  char* received;
  while(1){
    if (xSemaphoreTake (xSerialSemaphore, (TickType_t)5) == pdTRUE)
    {
      if(xQueueReceive(queue, &received, portMAX_DELAY) == pdPASS)
      {
        uart_sendstr(uart1, received);
      }
      xSemaphoreGive(xSerialSemaphore);
    }
    vTaskDelay(1);
  }
}

```

![Untitled](https://d2v6vdsk2p900z.cloudfront.net/original/2X/6/6589bdcb6e71d1bde065beb15907002c177748e8.png)

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 10, 2020, 6:59am UTC](https://forums.freertos.org/t/uart-receive-and-transmit-using-freertos-queue-and-semaphore/11316/2 "2020-12-10T06:59:30Z")

</div>

Looking at your code:

`TaskTransmit()` waits in `xQueueReceive()` for ever ( `portMAX_DELAY` ), or until there is a message in queue. But while waiting, the task holds the semaphore, and so the other task can not do anything. In a loop, tt will execute `vTaskDelay()` and try to get the semaphore, which is bound to fail.  
Could that be?  
Also, I do not see why you need both a queue and a semaphore?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [December 10, 2020, 11:41am UTC](https://forums.freertos.org/t/uart-receive-and-transmit-using-freertos-queue-and-semaphore/11316/3 "2020-12-10T11:41:07Z")

</div>

Just an additional hint:  
Your `xTaskCreate` comment regarding task priority is incorrect.  
FreeRTOS task priorities correspond to the given value - a higher value means higher priority.

I think this misunderstanding also leads to a deadlock when starting the scheduler i.e. the created tasks.  
`TaskTransmit` with (higher) prio 2 runs first, takes the semaphore and blocks forever on the queue.  
`TaskRead` (with lower prio) then starts to run and loops forever failing to grab the semaphore already taken by `TaskTransmit` which in turn is blocked forever waiting for the queue…
