title: Cannot send serial data from raspberry pi pico ( c sdk )
body: <p>I'm making a remote controlled machine using a pi pico to drive the motors and read some sensors, and a raspberry pi 4 to send commands to the pi pico via serial and host the web interface.</p>
  <p>I am currently testing the operation of the serial from the pi pico. To do this I have connected the pi pico with the raspberry in the following way:</p>
  <p><a href="https://i.stack.imgur.com/0zBE0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0zBE0.png" alt="connection diagram" /></a></p>
  <p>Currently, I am using the following files:</p>
  <pre><code>1. main.c to receive and send
  2. ring_queue.h where the code for the ring queue is located
  </code></pre>
  <p>main.c</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &quot;pico/stdlib.h&quot;
  #include &quot;hardware/uart.h&quot;
  #include &quot;hardware/irq.h&quot;
  #include &quot;ring_queue.h&quot;
  
  #define UART_ID uart0
  #define BAUD_RATE 115200
  #define DATA_BITS 8
  #define STOP_BITS 1
  #define PARITY    UART_PARITY_NONE
  #define UART_TX_PIN 0
  #define UART_RX_PIN 1
  #define LED_PIN PICO_DEFAULT_LED_PIN
  
  
  volatile int chars_rxed = 0;
  volatile char uCommand[32];
  volatile queue *rx_queue;
  volatile queue *tx_queue;
  
  
  void receive_rx(){
      while(uart_is_readable(UART_ID)){
        char ch = uart_getc(UART_ID);
        printf(&quot;Got a ch! %c\n&quot;, ch);
        if(ch != 10){
          uCommand[chars_rxed] = ch;
        }
        printf(&quot;Should have added it to uCommand: %s\n&quot;, uCommand);
        if(uCommand[chars_rxed] == '/'){
          printf(&quot;End of the command\n&quot;);
          queue_enqueue((queue*)rx_queue, (char*)uCommand);
          memset((char*)uCommand, 0, sizeof(uCommand));
          chars_rxed = 0;
          break;
        }
        if(ch != 10) chars_rxed++;
      }
  }
  
  void send_tx(){
    if(queue_empty((queue*)tx_queue) == 1){
     return;
   }
   else{
     printf(&quot;Trying to send something\n&quot;);
     char *foo = queue_dequeue((queue*)tx_queue);
     uart_write_blocking(UART_ID, (char*)foo, 32);
     //printf(&quot;%s\n&quot;, queue_dequeue((queue*)tx_queue));
   }
  }
  
  int main(){
  
    stdio_init_all();
  
    memset((char*)uCommand, 0, sizeof(uCommand));
    rx_queue = create_queue(32);
    tx_queue = create_queue(32);
  
    uart_init(UART_ID, BAUD_RATE);
    gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
    gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
    uart_set_hw_flow(UART_ID, false, false);
    uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY);
    uart_set_fifo_enabled(UART_ID, true);
  
    int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
  
    irq_set_exclusive_handler(UART_IRQ, receive_rx);
    irq_set_enabled(UART_IRQ, true);
    uart_set_irq_enables(UART_ID, true, false);
  
      while (1){
          tight_loop_contents();
          if(queue_size((queue*)rx_queue) != 0){
            printf(&quot;Moving from rx to tx to print the received command\n&quot;);
            queue_enqueue((queue*)tx_queue, queue_dequeue((queue*)rx_queue));
          }
          send_tx();
        }
  }
  </code></pre>
  <p>ring_queue.h</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  
  typedef struct {
      unsigned int tail;      // current tail
      unsigned int head;      // current head
      unsigned int size;      // current number of items
      unsigned int capacity;  // Capacity of queue
      char** data;                // Pointer to array of data
  } queue;
  
  
  queue *create_queue(unsigned int _capacity){
      printf(&quot;Malloc!\n&quot;);
      queue *myQueue = malloc(sizeof(queue));
      printf(&quot;Malloc done!\n&quot;);
      if (myQueue == NULL ){
          printf(&quot;Malloc failed!\n&quot;);
          return NULL;
      }
      else {
          printf(&quot;Malloc succeed!\n&quot;);
          myQueue-&gt;tail = -1;
          myQueue-&gt;head = 0;
          myQueue-&gt;size = 0;
          myQueue-&gt;capacity = _capacity;
          myQueue-&gt;data = malloc(_capacity * sizeof(char*));
  
          return myQueue;
      }
  }
  
  
  int queue_empty(queue *q) {
      if(q == NULL) return -1;
      else if(q-&gt;size == 0) return 1;
      else return 0;
  }
  
  
  int queue_full(queue *q) {
      if(q == NULL) return -1;
      else if(q-&gt;size == q-&gt;capacity) return 1;
      else return 0;
  }
  
  
  int queue_enqueue(queue *q, const char *item) {
      if (q == NULL) return -1;
      else if (queue_full(q) == 1) return 0;
      else {
        q-&gt;tail = (q-&gt;tail + 1) % q-&gt;capacity;
      q-&gt;data[q-&gt;tail] = strdup(item);
        q-&gt;size++;
  
        return 1;
      }
  }
  
  char *queue_dequeue(queue *q) {
      if(q == NULL) return NULL;
      else if(queue_empty(q) == 1) return '\0';
      else {
          char *item = q-&gt;data[q-&gt;head];
          q-&gt;head = (q-&gt;head + 1) % q-&gt;capacity;
          q-&gt;size--;
  
          return item;
      }
  }
  
  
  unsigned int queue_size(queue *q) {
      if (q == NULL) return - 1;
      else return q-&gt;size;
  }
  
  
  void free_queue(queue *q) {
    for(int i = 0; i &lt; q-&gt;capacity; i++) free(q-&gt;data[i]);
    free(q-&gt;data);
      free(q);
  }
  
  </code></pre>
  <p>I'm using the usb for debbugging and when I send a simple command (via the arduino IDE) like $MOVE / I can receive it correctly but not send it back as serial, instead with the usb I can ( the printf under the uart_write_blocking).
  When I try to send via uart I get random characters on the arduino serial prompt and the pico also seems to receive some of the ones it sent.</p>