Article 5G054 UART read not working..

UART read not working..

by
TRINATH KARRI
from LinuxQuestions.org on (#5G054)
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...latest?d=yIl2AUoC8zA latest?i=JNXrhvuEumw:U4K-csab9rI:F7zBnMy latest?i=JNXrhvuEumw:U4K-csab9rI:V_sGLiP latest?d=qj6IDK7rITs latest?i=JNXrhvuEumw:U4K-csab9rI:gIN9vFwJNXrhvuEumw
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments