I open the port and set the UART

uart_fd = open("/dev/serial0", O_RDWR | O_NOCTTY | O_NDELAY);

if (uart_fd  < 0)
    printf("Failed to open UART\n");
    return -1;

config.c_cflag = UART_SPEED | CS8 | CLOCAL | CREAD;
config.c_iflag = IGNPAR;
config.c_oflag = 0;
config.c_lflag = 0;

if (tcsetattr(uart_fd, TCSANOW, &config) < 0)  
    printf("Error setting UART configuration\n");
    return -1;

And I send strings to terminal

void UART_WriteString(const char *str)
    write (uart_fd, str, strlen(str));

    //wait all chars are sent

now I get gibberish on terminal no matter what baud rate I choose. What could be a problem?

  • Can you give us the tutorial your following? C programs are usually hard to debug. If you would like to try python, the following post might be helpful: (1) Serial Test Python Program raspberrypi.stackexchange.com/questions/96534/… (2) USB Serial Ports raspberrypi.stackexchange.com/questions/96697/… (3) Rpi3 to Arduino Serial UART Communication Tutorial raspberrypi.stackexchange.com/questions/96184/…
    – tlfong01
    Jan 8 '20 at 13:34
  • I would suggest to first do the UART loopback tests, to make sure the software and hardware setup are OK. Please feel free to ask me any newbie questions on python uart/serial programming.
    – tlfong01
    Jan 8 '20 at 13:38
  • There are many reasons to get rubbish output in serial ports, including not flushing buffer, mixing up control characters. Ont troubleshooting way is to test USB UART instead of on board TxD/Rxd pins. The test programs referred above are for Rpi3B+. Below is a version I tested OK on Rpi4B: penzu.com/p/49c560cf . Happy programming Cheers.
    – tlfong01
    Jan 8 '20 at 13:49
  • 1
    The code snippet leaves us guessing as to what is being transmitted. Please add a complete program we can run and test.
    – joan
    Jan 8 '20 at 14:47
  • 1
    Debug your hardware before you start debugging software. Run a terminal emulator on the UART and make sure the transmission works. Or say so if you already did. Jan 8 '20 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.