UART Initialization Failed

Hello Team Toradex,
I am using verdin imx8mp device and for that device i am write a code for UART in visual studio code using qt c++, but whenever i am trying to initialize UART it will show error, Error opening UART device. the code i am using is shown below
int UARTModule::initUART(const char *device, int baudRate) {
uartFd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (uartFd == -1) {
qDebug() << “Error opening UART device”;
return -1;
}

struct termios options;
tcgetattr(uartFd, &options);
cfsetispeed(&options, baudRate);
cfsetospeed(&options, baudRate);
tcsetattr(uartFd, TCSANOW, &options);

return uartFd;

}
so give me the solution for this

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Based on the provided context, it seems like you are encountering an issue with opening the UART device on your Verdin iMX8MP module. Here are a few steps to troubleshoot and potentially resolve the issue:

  1. Check Device Path: Ensure that the device path you are using is correct. For Verdin family, the UART device names are typically /dev/verdin-uart1, /dev/verdin-uart2, etc. Verify that the device path you are passing to the initUART function matches one of these.

  2. Permissions: Ensure that your application has the necessary permissions to access the UART device. You might need to run your application with elevated privileges or adjust the permissions of the device file.

  3. Device Availability: Verify that the UART device is available and not being used by another process. You can list the available UART devices using the command:

    ls /dev/verdin-uart*
    
  4. Error Handling: Enhance your error handling to get more information about why the open call is failing. You can use perror to print the error message:

    if (uartFd == -1) {
        perror("Error opening UART device");
        return -1;
    }
    
  5. Baud Rate: Ensure that the baud rate you are setting is supported. The standard baud rates are predefined, and you should use one of those.

Here is an updated version of your code with enhanced error handling:

#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <QDebug>

int UARTModule::initUART(const char *device, int baudRate) {
    uartFd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
    if (uartFd == -1) {
        perror("Error opening UART device");
        return -1;
    }

    struct termios options;
    if (tcgetattr(uartFd, &options) != 0) {
        perror("Error getting UART attributes");
        close(uartFd);
        return -1;
    }

    cfsetispeed(&options, baudRate);
    cfsetospeed(&options, baudRate);

    options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode
    options.c_cflag &= ~CSIZE; // Mask the character size bits
    options.c_cflag |= CS8; // Select 8 data bits
    options.c_cflag &= ~PARENB; // No parity
    options.c_cflag &= ~CSTOPB; // 1 stop bit
    options.c_cflag &= ~CRTSCTS; // No hardware flow control

    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
    options.c_iflag &= ~(IXON | IXOFF | IXANY); // No software flow control
    options.c_oflag &= ~OPOST; // Raw output

    if (tcsetattr(uartFd, TCSANOW, &options) != 0) {
        perror("Error setting UART attributes");
        close(uartFd);
        return -1;
    }

    return uartFd;
}

If the issue persists, please provide additional details such as the exact error message from perror and the device path you are using.

Please, also check if these links can help you solve your problem:

How to Use UART on Torizon OS | Toradex Developer Center.

Hello Team Toradex,
int UARTModule::initUART(const char *device, int baudRate) {

uartFd = open(device, O_RDWR | O_NOCTTY /| O_NDELAY/);
if (uartFd == -1) {
qDebug() << “Error opening UART device:” << strerror(errno);
return -1;
}

struct termios options;
if (tcgetattr(uartFd, &options) < 0) {
    qDebug() << "Error getting UART attributes:" << strerror(errno);
    close(uartFd);
    return -1;
}

cfsetispeed(&options, baudRate);
cfsetospeed(&options, baudRate);

options.c_cflag |= (CLOCAL | CREAD);  // Enable the receiver and set local mode
options.c_cflag &= ~PARENB;           // No parity
options.c_cflag &= ~CSTOPB;           // 1 stop bit
options.c_cflag &= ~CSIZE;            // Mask the character size bits
options.c_cflag |= CS8;               // 8 data bits
options.c_cflag &= ~CRTSCTS;          // No hardware flow control

options.c_iflag &= ~(IXON | IXOFF | IXANY);  // Disable software flow control

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);  // Raw input mode
options.c_oflag &= ~OPOST;                           // Raw output mode

if (tcsetattr(uartFd, TCSANOW, &options) != 0) {
    qDebug() << "Error setting UART attributes:" << strerror(errno);
    close(uartFd);
    return -1;
}

return uartFd;

}

void UARTModule::sendData(int uart, const char *data) {
//write(uart, data, strlen(data));
ssize_t bytes_written = write(uart, data, strlen(data));
if (bytes_written < 0) {
qDebug() << “Error writing to UART:” << strerror(errno);
} else {
qDebug() << “Successfully wrote” << bytes_written << “bytes to UART.”;
}

}
using the above 2 functions I have used for the UART initialization and send data use for the to write data but whenever i have write data on uart i have see that output on Putty console.

But whenever i have write data on uart it will be torizon login window it will ask me to login torizon device instead of showing my uart data so how to resolve this?

I am using verdin imx8mp device

Hi @Ravi1

I am not able to understand your issue.

@sahil.tx

When I have try to deploy the code from visual studio code it shows the below error

ERROR: failed to solve: commontorizon/qt6-wayland-imx8:3.3.1: failed to resolve source metadata for docker.io/commontorizon/qt6-wayland-imx8:3.3.1: failed to do request: Head “https://registry-1.docker.io/v2/commontorizon/qt6-wayland-imx8/manifests/3.3.1”: dial tcp: lookup registry-1.docker.io on 172.26.64.1:53: read udp 172.26.66.45:37532->172.26.64.1:53: i/o timeout

  • The terminal process “/usr/bin/bash ‘-c’, ‘DOCKER_HOST= docker build --progress=plain --pull -f /home/RaviVanand/test/Dockerfile.sdk /home/RaviVanand/test -t cross-toolchain-arm64-test --build-arg IMAGE_ARCH=arm64 --build-arg GPU=-imx8 --build-arg APP_ROOT=/home/torizon/app’” terminated with exit code: 1

Hi @Ravi1 ,
What OS you are using?

Hii@sahil.tx,
I am using torizon os

Hi @Ravi1,

Please take reference from this article.

Hello @sahil.tx,
By following the this article. I am able to initialize the UART Interface.
But my problem is that When I have try to deploy the code from visual studio code it shows the below error

ERROR: failed to solve: commontorizon/qt6-wayland-imx8:3.3.1: failed to resolve source metadata for docker.io/commontorizon/qt6-wayland-imx8:3.3.1: failed to do request: Head “https://registry-1.docker.io/v2/commontorizon/qt6-wayland-imx8/manifests/3.3.1”: dial tcp: lookup registry-1.docker.io on 172.26.64.1:53: read udp 172.26.66.45:37532->172.26.64.1:53: i/o timeout

  • The terminal process “/usr/bin/bash ‘-c’, ‘DOCKER_HOST= docker build --progress=plain --pull -f /home/RaviVanand/test/Dockerfile.sdk /home/RaviVanand/test -t cross-toolchain-arm64-test --build-arg IMAGE_ARCH=arm64 --build-arg GPU=-imx8 --build-arg APP_ROOT=/home/torizon/app’” terminated with exit code: 1