0

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
 tcdrain(uart_fd);
}

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

asked Jan 8, 2020 at 12:55
12
  • 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/… Commented Jan 8, 2020 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. Commented Jan 8, 2020 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. Commented Jan 8, 2020 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. Commented Jan 8, 2020 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. Commented Jan 8, 2020 at 14:55

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.