I am connected to my Raspberry PI 3 B+ running Raspbian through an SSH terminal, and I am trying to make an application that uses the UART through the GPIO.

Whenever I try to open the port "/dev/serial0" I get in the case where I should not be getting, according to the tutorial I am following 1. I do not know exactly what means the the output of the function open(), but I am trusting the tutorial :

#include <stdio.h>
#include <unistd.h>         //Used for UART
#include <fcntl.h>          //Used for UART
#include <termios.h>        //Used for UART

void setup_uart(){
    int uart0_filestream = -1;
    uart0_filestream = open("/dev/serial0", O_RDWR | O_NOCTTY | O_NDELAY);      //Open in non blocking read/write mode
    if (uart0_filestream == -1)
        printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");

void hello_uart(){
//----- TX BYTES -----
    unsigned char tx_buffer[20];
    unsigned char *p_tx_buffer;

    p_tx_buffer = &tx_buffer[0];
    *p_tx_buffer++ = 'H';
    *p_tx_buffer++ = 'e';
    *p_tx_buffer++ = 'l';
    *p_tx_buffer++ = 'l';
    *p_tx_buffer++ = 'o';

    if (uart0_filestream != -1)
        int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));      //Filestream, bytes to write, number of bytes to write
        if (count < 0)
            printf("UART TX error\n");

int main(){

I have not used the port ttyS0 before (at least not consciously in the program I am working on), and I suspect that the SSH may be the problem, if it is using the miniUART (in serial0). Moreover, when I try to use the port serial1, I do not it compiles and runs without problems, but I can not test if it is working or not.

How can I investigate what is causing it and, even better, do you have any suggestions for how could I use UART through the GPIO?

Would it be a good idea do try to remap the UART PL011 to the GPIO ports 8 and 10, taking the miniUART out from there, or is there anything more straightforward?


2 Answers 2


I bet the problem is that you have enabled serial console. Your tutorial said to turn that off, but it wasn't very clear. Try

sudo raspi-config

and select no for the serial console on boot, but yes for serial hardware. Then /dev/ttyS0 will be set with permissions

crw-rw---- 1 root dialout 4, 64 Feb 24 19:04 ttyS0

Which is what you want. With serial console enabled /dev/ttyS0 is created with permissions

crw--w---- 1 root tty 4, 64 Jan 25 15:21 ttyS0

Which won't work because the default user pi isn't in the group tty and doesn't have group permissions to read ttyS0 anyway.

Also, to learn about open(), open a terminal and read

man 2 open

Your code is using /dev/serial1 which is usually connected to Bluetooth.

There is no need to fiddle with port mappings. This should be done through raspi-config and/or Device Tree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.