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?

john7
  • 1
  • 1
  • 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 https://raspberrypi.stackexchange.com/questions/96534/serial-communication-from-raspbian-using-python-to-arduino

    (2) USB Serial Ports https://raspberrypi.stackexchange.com/questions/96697/how-many-serial-ports-are-on-the-pi-3

    (3) Rpi3 to Arduino Serial UART Communication Tutorial https://raspberrypi.stackexchange.com/questions/96184/rpi3-to-arduino-serial-uart-

    – tlfong01 Jan 08 '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 08 '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: https://penzu.com/p/49c560cf . Happy programming Cheers. – tlfong01 Jan 08 '20 at 13:49
  • Thanks but I develop in C (Qt). – john7 Jan 08 '20 at 14:02
  • You are welcome. Cheers. – tlfong01 Jan 08 '20 at 14:04
  • for pins 14, 15 what should I set dtoverlay=uart0 or dtoverlay=uart1 ? – john7 Jan 08 '20 at 14:07
  • Ah, I forgot to include a pinout in my program. So pins 14, 15 correspond to both UART0 and also UART1. There you are, the same program but with a pinout: https://penzu.com/p/49c560cf. Happy C programming. Cheers. – tlfong01 Jan 08 '20 at 14:17
  • Thank you. So what to pick - dtoverlay=uart0 or dtoverlay=uart1 ? – john7 Jan 08 '20 at 14:24
  • Ah, you just pick your lucky number 0, or 1. You need to use a tool to find the port name. The tool is the last import in Section 2 of my program, just now highlighted in BRIGHT RED. – tlfong01 Jan 08 '20 at 14:33
  • 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 08 '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. – Dmitry Grigoryev Jan 08 '20 at 14:55
  • Ah I vaguely remember that I used the "import serial.tools.list_ports" to list the ports. It is two months ago, so only 50% sure. Ah, it is bed time, so my brain is not working properly. Perhaps tomorrow. Cheers. – tlfong01 Jan 08 '20 at 14:57

0 Answers0