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

Compute Module • CM4: Cannot read UART unless minicom is running

$
0
0
Hello,

I am trying to access UART3 -> /dev/ttyAMA3 on my CM4 module. I have "dtoverlay=uart3" in /boot/config.txt. Before running my C program that simply reads /dev/ttyAMA3 and prints to stdout continuously, I first confirmed that I was receiving the data properly from my sensor using minicom - I was. I then ran my c-program, forgetting to quit minicom, and saw the data output from my C program as expected. I realized minicom was still running and exited. My C program froze when I exited minicom. I tried restarting my C program and no joy. I also tried /dev/ttyAMA4 and same result. I am a member of dialout group and also tried running my C program as root.
Any thoughts on as to why I am unable to properly open and read my UART ports with my C code unless minicom or picocom are running on the same port?

Here is my c-code:

Code:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <termios.h>#include <unistd.h>int main() {    int serial_fd;    struct termios tty;    char buffer[256];    int bytes_read;    // Open the serial port    serial_fd = open("/dev/ttyAMA3", O_RDWR);    if (serial_fd == -1) {        perror("Error opening serial port");        return 1;    }    fcntl(serial_fd, F_SETFL, O_NONBLOCK);    // Configure the serial port    memset(&tty, 0, sizeof(tty));    if (tcgetattr(serial_fd, &tty) != 0) {        perror("Error configuring serial port");        return 1;    }    cfsetospeed(&tty, B19200); // Set baud rate     tty.c_cflag |= (CLOCAL | CREAD); // Enable receiver and ignore modem control signals    tty.c_cflag &= ~CSIZE;    tty.c_cflag |= CS8; // 8-bit data    tty.c_cflag &= ~PARENB; // No parity    tty.c_cflag &= ~CSTOPB; // 1 stop bit    tty.c_cflag &= ~CRTSCTS; // No hardware flow control    if (tcsetattr(serial_fd, TCSANOW, &tty) != 0) {        perror("Error configuring serial port");        return 1;    }    // Read data from the serial port until '\r' is received    memset(buffer, 0, sizeof(buffer));    while (1) {        bytes_read = read(serial_fd, buffer, sizeof(buffer) - 1);        if (bytes_read > 0) {            buffer[bytes_read] = '\0'; // Null-terminate the received data            if (strchr(buffer, '\r') != NULL) {                printf("%s\n", buffer);memset(buffer,0,sizeof(buffer));            }        }    }    // Close the serial port    close(serial_fd);    return 0;}

Statistics: Posted by jwh2 — Wed Jan 31, 2024 1:56 pm



Viewing all articles
Browse latest Browse all 8082

Trending Articles