Hi
Here I am writing a simple serial data read application.
I am not able to read the data from serial port (UART_B).
Here is my code for read operation
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<termios.h>
#include <errno.h>
int main(int args,char *argv[])
{
char buff_tx[100];
char buff_rx[100];
int fd;
struct termios tty;
fd = open("/dev/ttymxc1",O_RDWR);
if(fd<0)
{
printf("fail to open /dev/ttymxc1\n");
return -1;
}
tcgetattr(fd,&tty);
cfsetispeed(&tty,B9600);
cfsetospeed(&tty,B9600);
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tcsetattr(fd,TCSANOW,&tty);
tcflush(fd,TCIFLUSH);
while(1)
{
read(fd,buff_rx,sizeof(buff_rx)-1);
printf("buff_rx = %s\n",buff_rx);
buff_rx[0]='\0';
}
close(fd);
return 0;
}
But Write operation working properly
Here is the code for write operation
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
#include<termios.h>
#include <errno.h>
int main(int args,char *argv[])
{
char buff_tx[100];
char buff_rx[100];
int fd;
struct termios tty;
fd = open("/dev/ttymxc1",O_RDWR);
if(fd<0)
{
printf("fail to open /dev/ttymxc1\n");
return -1;
}
tcgetattr(fd,&tty);
cfsetispeed(&tty,B9600);
cfsetospeed(&tty,B9600);
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tcsetattr(fd,TCSANOW,&tty);
tcflush(fd,TCIFLUSH);
strncpy(buff_tx,"TESTING UART\n",13);
while(1)
{
write(fd,(char *)buff_tx,strlen(buff_tx)-1);
sleep(1);
//read(fd,buff_rx,sizeof(buff_rx)-1);
//printf("buff_rx = %s\n",buff_rx);
//buff_rx[0]='\0';
}
close(fd);
return 0;
}
can any one help me to overcome this.