All pastes #47193 Raw Edit

serial setup

public c v1 · immutable
#47193 ·published 2006-03-27 19:48 UTC
rendered paste body
int cel_ser_setup(){	static int done = 0;	struct termios term;	int ret;		if(done) {		fprintf(stderr, "cel_ser_setup: Setup already done.\n");		return 0;	}	// Open serial port	fd = open(SER_DEVICE, O_RDWR | O_NOCTTY); // | O_NONBLOCK);	if (fd < 0) {		fprintf(stderr, "cel_ser_setup: Failed to open %s: %s\n", SER_DEVICE, strerror(errno));		return 0;	}	// Read in current serial port settings	ret = tcgetattr(fd, &term);	if (ret < 0) {		fprintf(stderr, "cel_ser_setup: Failed get status of %s: %s\n", SER_DEVICE, strerror(errno));		cel_ser_shutdown();		return 0;	}	// Backup current serial port settings	memcpy(&term_before, &term, sizeof(struct termios));	// Clear termios structure	cfmakeraw(&term);	// Set serial port speed	ret = cfsetspeed(&term, SER_SPEED);	if (ret < 0) {		fprintf(stderr, "cel_ser_setup: Failed to speed of %s to %s: %s\n", SER_DEVICE, SER_SPEED, strerror(errno));		cel_ser_shutdown();		return 0;	}		// Set data size	term.c_cflag &= ~CSIZE;	term.c_cflag |= CS8;		// Set no parity	term.c_cflag &= ~PARENB;		// Set stop bits	term.c_cflag &= ~CSTOPB;		//Set no software flow control	term.c_cflag &= ~(IXON | IXOFF | IXANY);		//Set no hardware flow control	term.c_cflag &= ~CRTSCTS;		//Set raw mode input mode	term.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);	//Set raw output mode	term.c_oflag &= ~OPOST;		// Write out new serial port settings	ret = tcsetattr(fd, TCSANOW, &term);	if (ret < 0) {		fprintf(stderr, "cel_ser_setup: Failed to write out new settings to %s: %s\n", SER_DEVICE, strerror(errno));		cel_ser_shutdown();		return 0;	}		done = 1;	printf("Setup of %s done\n", SER_DEVICE);	return 1;}