Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4919

Interfacing (DSI, CSI, I2C, etc.) • Failing to use Raspberry Pi 4 UART2

$
0
0
I am encountering an issue with reading data from UART2 on my Raspberry Pi using GPIO pins 0 and 3. I have configured the system to enable UART and specified dtoverlay=uart2 in the config.txt file. Below is the C code I am using to read data from UART2:

Code:

#include <stdio.h>#include <string.h>#include <errno.h>#include <wiringSerial.h>int main() {    int uart_fd;    char buffer[256];    // Open the serial connection (UART2) at baud rate 115200    if ((uart_fd = serialOpen("/dev/ttyS1", 115200)) < 0) {        fprintf(stderr, "Error opening UART: %s\n", strerror(errno));        return 1;    }    // Continuously read data from UART2    while (1) {        if (serialDataAvail(uart_fd) > 0) {            // If data is available, read it into the buffer            int len = serialRead(uart_fd, buffer, sizeof(buffer) - 1);            if (len > 0) {                buffer[len] = '\0'; // Null-terminate the string                printf("Received data: %s\n", buffer);            }        }    }    return 0;}
Despite this setup, I am unable to receive any data from UART2. I have confirmed that the UART is enabled (enable_uart=1) and that the dtoverlay=uart2 is properly configured in the config.txt file.

If anyone has insights into why the data reception might be failing in this context, I would greatly appreciate any guidance or troubleshooting steps.

Thank you.

Statistics: Posted by neekamp — Tue Apr 30, 2024 6:51 pm



Viewing all articles
Browse latest Browse all 4919

Trending Articles