TRINATH KARRI
Member
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...
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...