All pastes #365002 Raw Edit

cruiseoveride

public c v1 · immutable
#365002 ·published 2007-02-20 15:56 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[5];  /* 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);	int i;	for(;;){		nbytes = read(fd,buffer,sizeof(buffer));		for(i=0;i<sizeof(buffer);i++){			if(i%4==0)printf("\n");				printf("%4.4x ",buffer[i]);			}	}	close(fd);	return 0;}