簡易的串口操作,尚未調試

/*	libtinytty - a library for serial port functioning.
	This v0.01 version is published under GPL temporarily.
*/

#ifndef _STDLIB_H
	#include 
#endif

#ifndef _STDIO_H
	#include 
#endif

#ifndef _TERMIOS_H
	#include 
#endif

#ifndef _FCNTL_H
	#include 
#endif

/* Put the length of data bits into variable *OPT */
int _setdatabits(struct termios (*opt), int databits){
	switch (databits){
		case 5:
			(*opt).c_cflag |= CS5;
			break;
		case 6:
			(*opt).c_cflag |= CS6;
			break;
		case 7:
			(*opt).c_cflag |= CS7;
			break;
		case 8:
			(*opt).c_cflag |= CS8;
			break;
		default:
			return -1;
			break;
	}
	return 0;
}

/* Put the mode of parity into variable *OPT */
int _setparity(struct termios (*opt), int parity){
	switch (parity){
		case 'n': case 'N':
			(*opt).c_iflag &= ~INPCK;		//dis.InPCK
			(*opt).c_cflag &= ~PARENB;	//dis.OutPCK
			break;
		case 'o': case 'O':
			(*opt).c_iflag |= INPCK;		//ena.InPCK
			(*opt).c_cflag |= PARENB;		//ena.OutPCK
			(*opt).c_cflag |= PARODD;		//ena.ParOdd
			break;
		case 'e': case 'E':
			(*opt).c_iflag |= INPCK;		//ena.InPCK
			(*opt).c_cflag |= PARENB;		//ena.OutPCK
			(*opt).c_cflag &= ~PARODD;	//dis.ParOdd
			break;
		default:
			return -1;
			break;
	}
	return 0;
}

/* Put the length of stop bits into variable *OPT */
int _setstopbits(struct termios (*opt), int stopbits){
	switch(stopbits)
	{
		case 1: (*opt).c_cflag &= ~CSTOPB; break;
		case 2: (*opt).c_cflag |= CSTOPB; break;
		default: return -1; break;
	}
	return 0;
}

// Look up the speed value corresponds to the baud rate 
int _baud2speed(int baud){
	int speed;
	switch (baud){
		case 4000000:
			speed = B4000000;
			break;
		case 3500000:
			speed = B3500000;
			break;
		case 3000000:
			speed = B3000000;
			break;
		case 2500000:
			speed = B2500000;
			break;
		case 2000000:
			speed = B2000000;
			break;
		case 1500000:
			speed = B1500000;
			break;
		case 1152000:
			speed = B1152000;
			break;
		case 1000000:
			speed = B1000000;
			break;
		case 921600:
			speed = B921600;
			break;
		case 576000:
			speed = B576000;
			break;
		case 500000:
			speed = B500000;
			break;
		case 460800:
			speed = B460800;
			break;
		case 230400:
			speed = B230400;
			break;
		case 115200:
			speed = B115200;
			break;
		case 57600:
			speed = B57600;
			break;
		case 38400:
			speed = B38400;
			break;
		case 19200:
			speed = B19200;
			break;
		case 9600: 
			speed = B9600;
			break;
		case 4800:
			speed = B4800;
			break;
		case 2400:
			speed = B2400;
			break;
		case 1200:
			speed = B1200;
			break;
		case 300:
			speed = B300;
			break;
		default:
			return -1;
			break;
	}
	return speed;
}

// Flush the input and output buffers of the serial port
int _tcioflush(int fd) {
	tcflush(fd, TCIOFLUSH);
}

// Set the baud rate of the serial port FD */
int setbaudrate(int fd, int baud){
	int err;
	struct termios opt;
	int speed;
	err = tcgetattr(fd, &opt);
	if (0 != err) {return -1;}
	_tcioflush(fd);
	speed = _baud2speed(baud);
	cfsetispeed(&opt, speed);
	cfsetospeed(&opt, speed);
	err = tcsetattr(fd, TCSANOW, &opt);
	if (0 != err) {return -1;}
	_tcioflush(fd);
	return 0;
}

// Set the flow controlling mode of the serial port FD */
int setflowcontrol(int fd, int ena){
	int err;
	struct termios opt;
	err = tcgetattr(fd, &opt);
	if (0 != err) {return -1;}
	_tcioflush(fd);
	switch (ena){
		case 2:
			opt.c_iflag &= ~(IXON | IXOFF | IXANY);
			opt.c_cflag |= CRTSCTS;
		case 1:
			opt.c_cflag &= ~CRTSCTS;
			opt.c_iflag |= (IXON | IXOFF | IXANY);
			break;
		case 0:
			opt.c_cflag &= ~CRTSCTS;
			opt.c_iflag &= ~(IXON | IXOFF | IXANY);
			break;
		default:
			return -1;
			break;
	}
	err = tcsetattr(fd, TCSANOW, &opt);
	if (0 != err) {return -1;}
	_tcioflush(fd);
	return 0;
}

// Set the parity pattern of the serial port FD */
int setparity(int fd, int parity, int databits, int stopbits){
	int err;
	struct termios opt;
	err = tcgetattr(fd, &opt);
	if (0 != err) {return -1;}
	opt.c_cflag &= ~CSIZE;
	_setdatabits(&opt, databits);
	_setparity(&opt, parity);
	_setstopbits(&opt, stopbits);
	opt.c_cc[VTIME] = 150;
	opt.c_cc[VMIN] = 0;
	tcflush(fd, TCIFLUSH);
	err = tcsetattr(fd, TCSANOW, &opt);
	if (0 != err) {return -1;}
	return 0;
}

// Change the regular mode state of the serial port FD */
int setregularmode(int fd, int mode){
	int err;
	struct termios opt;
	_tcioflush(fd);
	err = tcgetattr(fd, &opt);
	if (0 != err) {return -1;}
	switch (mode){
		case 1:
			opt.c_lflag |= ICANON | ECHO | ECHOE;
			opt.c_oflag &= OPOST;
			break;
		case 0:
			opt.c_lflag &= ~(ICANON | ECHO | ECHOE);
			opt.c_oflag &= ~OPOST;
			break;
		default:
			return -1;
			break;
	}
	err = tcsetattr(fd, TCSANOW, &opt);
	if (0 != err) {return -1;}
	_tcioflush(fd);
	return 0;
}

// Open a serial port, and returns it handle as FD */
int openttydev(char *dev, int mode){
	int fd, err;
	struct termios opt;
	fd = open(dev, O_RDWR);
	if (-1 == fd) return -1;
		else return fd;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章