LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-31-2021, 12:09 AM   #1
TRINATH KARRI
LQ Newbie
 
Registered: Mar 2021
Posts: 4

Rep: Reputation: Disabled
UART read not working..


Hi,
I am unable to read UART data from COM port ttyS0. Read is being blocked (since it is a blocking call) and it gets triggered and data is read from UART once i run cutecom application. Please let me know if any changes are required for my code given below.

Code :

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main()
{
int fd, n;
char buf[100]= {0};
struct termios options;

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY );

if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 ");
}
else
{
fcntl(fd, F_SETFL, O_RDWR | O_NOCTTY);
}


if(tcgetattr(fd, &options) != 0)
{
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
return 1;
}

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
options.c_cflag |= (CREAD | CLOCAL);

options.c_lflag &= ~ICANON;
options.c_lflag &= ~ECHO;
options.c_lflag &= ~ECHOE;
options.c_lflag &= ~ECHONL;
options.c_lflag &= ~ISIG;

options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);

options.c_oflag &= ~OPOST;
options.c_oflag &= ~ONLCR;

options.c_cc[VTIME] = 100;
options.c_cc[VMIN] = 0;

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

if (tcsetattr(fd, TCSANOW, &options) != 0)
{
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
return 1;
}

while(1)
{
printf("Waiting to read\n");

n = read(fd, buf, 100);

if(n != 0)
buf[n]='\0';

if (n <= 0)
fputs("read() failed!\n", stderr);
else
printf("read bytes = %d, read val = %s\n", n, buf);

sleep(1);
}
}


Thanks in advance...
 
Old 03-31-2021, 08:28 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by TRINATH KARRI View Post
Hi,
I am unable to read UART data from COM port ttyS0. Read is being blocked (since it is a blocking call) and it gets triggered and data is read from UART once i run cutecom application. Please let me know if any changes are required for my code given below.
Code:
Code :

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main()
{
int fd, n;
char buf[100]= {0};
struct termios options;

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY );

if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 ");
}
else
{
fcntl(fd, F_SETFL, O_RDWR | O_NOCTTY);
}


if(tcgetattr(fd, &options) != 0)
{
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
return 1;
}

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
options.c_cflag |= (CREAD | CLOCAL);

options.c_lflag &= ~ICANON;
options.c_lflag &= ~ECHO;
options.c_lflag &= ~ECHOE;
options.c_lflag &= ~ECHONL;
options.c_lflag &= ~ISIG;

options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);

options.c_oflag &= ~OPOST;
options.c_oflag &= ~ONLCR;

options.c_cc[VTIME] = 100;
options.c_cc[VMIN] = 0;

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

if (tcsetattr(fd, TCSANOW, &options) != 0)
{
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
return 1;
}

while(1)
{
printf("Waiting to read\n");

n = read(fd, buf, 100);

if(n != 0)
buf[n]='\0';

if (n <= 0)
fputs("read() failed!\n", stderr);
else
printf("read bytes = %d, read val = %s\n", n, buf);

sleep(1);
}
}
Thanks in advance...
Read the "Question Guidelines" link in my posting signature. You haven't told us anything about your system, version/distro of Linux, or even what the actual error(s)/message(s) you're getting are. We're happy to try to help you, but we can't guess...and we aren't going to debug your code for you (which you should post in CODE tags). Also, cutecom appears to be last updated in 2009....may want to consider using minicom or something else more current.

And if your program is opening/reading/writing to a serial port...why do you need cutecom or any other program to ALSO open that port?

Provide details and relevant messages, and we can try to assist.

Last edited by TB0ne; 03-31-2021 at 08:40 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Blocked Read N bytes from UART zvivered Linux - Software 1 06-09-2017 07:55 AM
Uart not working when communicate with board to board biyabani Linux - Newbie 1 07-12-2013 02:11 AM
UART not working while kernel starts, but working in uboot ramesh.khokhani Linux - Kernel 2 06-24-2013 03:35 AM
Where does uart console get its value of uart port membase? unifoxz Linux - Kernel 1 12-20-2011 02:28 PM
[SOLVED] Power PC PSC UART Kernel Object just not working DamOTclese Linux - Software 1 05-03-2010 06:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration