C Program to read the serial data from arduino


#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <termios.h>

int main(int argc, char *argv[])
{

int fd;
char buff[100];
struct termios options;

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

if (fd == -1) {
perror("Error Opening File\n");
return -1;
}
printf("File Opened Successfully\n");

/* Check if the file descriptor is pointing to a TTY device or not */
if (!isatty(fd)){perror("Not a tty\n");}

/* Get the current configuration of the serial interface */
tcgetattr(fd, &options);

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

options.c_cflag &= ~PARENB; /*No Parity*/
options.c_cflag &= ~CSTOPB; /* 1 Stop Bit */
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8 ; /*8 Bits*/
options.c_cflag &= ~CRTSCTS; /*no hardware flow control*/
options.c_cflag |= CREAD | CLOCAL;/*Enable Receiver, ignore status lines */
options.c_iflag &= ~(IXON|IXOFF|IXANY); /*Disable Input/Output Flow Control, disable restart chars */

options.c_lflag &= ~(ICANON |ECHO| ECHOE|ISIG);
options.c_oflag &= ~OPOST; /*Disable Output Processing*/
options.c_cc[VMIN] = 0; /*Wait for 24 characters to come in before read returns*/
options.c_cc[VTIME] = 20; /*No minimum time to wait before read returns*/


if (!tcsetattr(fd, TCSANOW, &options))
printf("Successfully set the baud rate:\n");

else
perror("Error in Setting the baud rate\n");

do {
int n;
n = read(fd, buff, 1);
if (n == -1) {
usleep(100 *1000);
continue;
}
putchar(buff[0]);
fflush(stdout);
memset(buff, 0,sizeof(buff));
}while(1);
close(fd);
return 0;

}

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig