All pastes #365026 Raw Edit

Untitled

public text v1 · immutable
#365026 ·published 2007-02-20 16:31 UTC
rendered paste body
   #include <stdio.h>   /* Standard input/output definitions */
    #include <string.h>  /* String function definitions */
    #include <unistd.h>  /* UNIX standard function definitions */
    #include <fcntl.h>   /* File control definitions */
    #include <errno.h>   /* Error number definitions */
    #include <termios.h> /* POSIX terminal control definitions */


int main(int argc, char **argv){
	int fd;
	struct termios options;
	char buffer;  /* Input buffer */
	int  nbytes;       /* Number of bytes read */
	int  tries;        /* Number of tries so far */
	
	fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1)perror("open_port: Unable to open /dev/ttyUSB0 - "); fcntl(fd, F_SETFL, 0);
	
	/* set read to block untill read is done */
	fcntl(fd, F_SETFL, 0);

	 /* get the current options */
	tcgetattr(fd, &options);

	/* No parity (8N1): */
	options.c_cflag &= ~PARENB;
	options.c_cflag &= ~CSTOPB;
	options.c_cflag &= ~CSIZE;
	options.c_cflag |= CS8;

	/* Disable HW flow control */
//	options.c_cflag &= ~CNEW_RTSCTS;
	options.c_cflag &= ~CRTSCTS;

	/* Disable SW flow control */
	options.c_iflag &= ~(IXON | IXOFF | IXANY);

	/* Choose RAW Input */
	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

	/* 9600 Baud both ways */
	cfsetispeed(&options, B9600);
	cfsetospeed(&options, B9600);

         
        options.c_cc[VTIME]    = 0;   /* inter-character timer unused */
        options.c_cc[VMIN]     = 2;   /* blocking read until 5 chars received */
        

	 /* set the options */
	tcsetattr(fd, TCSANOW, &options);

	while((nbytes = read(fd,&buffer,1))>0) printf("%x \n",buffer);	

	close(fd);
	return 0;
}