linux 串口通信

/*
 * UARTOperate.c
 *
 *  Created on: 2010-10-20
 *  Author: vv
 */
#include     <stdio.h>      /*标准输入输出定义*/
#include     <stdlib.h>     /*标准函数库定义*/
#include     <unistd.h>     /*Unix 标准函数定义*/
#include     <sys/types.h> /*定义系统类型 */
#include     <sys/stat.h>   /*定义文件信息*/
#include     <fcntl.h>        /*文件控制定义*/
#include     <termio.h>   /*终端控制定义*/
#include     <errno.h>       /*错误号定义*/
#include <string.h>
#include <pthread.h>

#define PA_dev_ttys0	0x00	
#define PA_speed	9600	
#define PA_databits	8		
#define PA_parity	'N'		
#define PA_stopbits	1		
#define PA_ReadLength	6

int speed_arr[]={B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {115200,38400,  19200,  9600,  4800,  2400,  1200,  300};

int OpenDev(int num)      //打开串口
{
	char *Dev[11]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
	int	fd = open(Dev[num], O_RDWR );         //| O_NOCTTY | O_NDELAY
	if (-1 == fd)
	{
		perror("Can't Open Serial Port");
		return -1;
	}
	else
		return fd;
}
int set_speed(int fd, int speed)
{
	struct termios options;
	int i,status;
	if(tcgetattr(fd, &options) != 0)
	{
		perror("SetupSerial 1");
		return -1;
	}

	//设置波特率
	for ( i= 0;  i < sizeof(speed_arr)/sizeof(int);  i++)
	{
		if  (speed == name_arr[i])
		{
			printf("i=%d\n",i);
			tcflush(fd, TCIOFLUSH);
			cfsetispeed(&options, speed_arr[i]);
			cfsetospeed(&options, speed_arr[i]);
			status = tcsetattr(fd, TCSADRAIN, &options);
			if(status !=0)
			{
				printf("set_speed error\n");
				return -1;
			}
			tcflush(fd,TCIOFLUSH);
		}
	}//for
	return 0;

}
/******************校验位和数据位的设置******************************
无校验			8位	Option.c_cflag &= ~PARENB;
					Option.c_cflag &= ~CSTOPB;
					Option.c_cflag &= ~CSIZE;
					Option.c_cflag |= ~CS8;
奇校验(Odd)		7位 Option.c_cflag |= ~PARENB;
					Option.c_cflag &= ~PARODD;
					Option.c_cflag &= ~CSTOPB;
					Option.c_cflag &= ~CSIZE;
					Option.c_cflag |= ~CS7;
偶校验(Even)	   7位 Option.c_cflag &= ~PARENB;
					Option.c_cflag |= ~PARODD;
					Option.c_cflag &= ~CSTOPB;
					Option.c_cflag &= ~CSIZE;
					Option.c_cflag |= ~CS7;
Space校验		7位 Option.c_cflag &= ~PARENB;
					Option.c_cflag &= ~CSTOPB;
					Option.c_cflag &= &~CSIZE;
					Option.c_cflag |= CS8;
*******************************************************************/

//设置串口波特率,数据位,校验位,停止位,
//fd         int  	串口标识符
//databits   short   7 或 8
//parity     char  	校验类型:N,E,O,S
//stopbits   short	1 或 2

int setDev(int fd, int databits, char parity, int stopbits)
{
	struct termios options;
	int i;
	if(tcgetattr(fd, &options) != 0)
	{
		printf("setDev error\n");
		return -1;
	}
	
	options.c_cflag &= ~CSIZE;

	//设置数据位
	switch (databits)
	{
	case 7:
		options.c_cflag |= CS7;
		break;
	case 8:
		options.c_cflag |= CS8;
		break;
	default:
		printf("Unsupported data size\n");
		return -1;
	}//switch (databits)

	//设置校验类型
	switch (parity)
	{
	case 'n':
	case 'N':
		options.c_cflag &= ~PARENB;    /* Clear parity enable */
		options.c_iflag &= ~INPCK;     /* 关闭输入奇偶校验 */
		break;
	case 'o':
	case 'O':
		options.c_cflag |= PARODD; /* 输入/输出奇校验*/
		options.c_cflag |= PARENB;
		options.c_iflag |= INPCK;             /* 启动输入奇偶校验 */
		break;
	case 'e':
	case 'E':
		options.c_cflag |= PARENB;     /* 打开校验 */
		options.c_cflag &= ~PARODD;   /* 不允许奇校验*/
		options.c_iflag |= INPCK;       /* 启动输入奇偶校验*/
		break;
	case 'S':
	case 's':  /*as no parity*/
	   	options.c_cflag &= ~PARENB; //不允许校验
		options.c_cflag &= ~CSTOPB;
		options.c_iflag |= INPCK;
		break;
	default:
		printf("Unsupported parity\n");
		return -1;
	}//switch (parity)

	//设置停止位
	switch (stopbits)
	{
	case 1:
		options.c_cflag &= ~CSTOPB;//设置1个停止位
		break;
	case 2:
		options.c_cflag |= CSTOPB;  //设置2个停止位
	  break;
	default:
		printf("Unsupported stop bits\n");
		return -1;
	}//switch (stopbits)
	
	options.c_cflag &= ~CRTSCTS;				//no hard flow control
	options.c_cflag |=(CLOCAL | CREAD);			//enabled receiver
	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);   //自动添加回车
	options.c_oflag &= ~OPOST;
	options.c_oflag &= ~(ONLCR | OCRNL);
	
	options.c_iflag &= ~(ICRNL | INLCR);
	options.c_iflag &= ~(IXON | IXOFF | IXANY);		//no sw flow control

	tcflush(fd,TCIFLUSH);
	options.c_cc[VTIME] = 0; /* 延时15 seconds*/
	options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
	if (tcsetattr(fd, TCSANOW, &options) != 0)
	{
		printf("SetupSerial 3");
		return -1;
	}
	return 0;
}

void main()
{
	unsigned char buf1[]={0x7E,0x06,0x00,0x00,0x00,0x7E};
	//unsigned char buf1[]={0x7E,0x04,0x00,0xC8,0x00,0x7E};
	unsigned char buf2[20];
	int i,fd,wnum,rnum,res,num;
	pthread_t thread;
	num = 1;
	fd =open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
	set_speed(fd, PA_speed);
	if((setDev(fd, PA_databits,PA_parity, PA_stopbits))==-1)
	{
		printf("setDev error!\n");
	}

	printf("fd=%d\n",fd);
	wnum=write(fd,buf1,6);
	printf("wnum=%d\n",wnum);
	for(i=0;i<wnum;i++)
		{
		printf("0x%2.2x\t",buf1[i]);	
		}
	printf("\n");
	sleep(1);

	fcntl(fd,F_SETFL,0);
	//fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); 
	rnum = -1;
	while(rnum<0)
	{
		rnum=read(fd,buf2,20);	
	}
	printf("rnum=%d\n",rnum);
	for(i=0;i<rnum;i++)
	{
		printf("0x%2.2x\t",buf2[i]);	
	}
	printf("\n");
	
}


附录

c_cflag用于设置控制参数,除了波特率外还包含以下内容: 
EXTA         External rate clock
EXTB         External rate clock
CSIZE         Bit mask for data bits
CS5         5个数据位
CS6         6个数据位
CS7         7个数据位
CS8         8个数据位
CSTOPB         2个停止位(清除该标志表示1个停止位
PARENB         允许校验位
PARODD         使用奇校验(清除该标志表示使用偶校验)
CREAD         Enable receiver
HUPCL         Hangup (drop DTR) on last close
CLOCAL         Local line – do not change “owner” of port
LOBLK         Block job control outpu
c_cflag标志可以定义CLOCAL和CREAD,这将确保该程序不被其他端口控制和信号干扰,同时串口驱动将读取进入的数据。CLOCAL和CREAD通常总是被是能的。

c_lflag用于设置本地模式,决定串口驱动如何处理输入字符,设置内容如下:
ISIG            Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals 
ICANON         Enable canonical input (else raw) 
XCASE         Map uppercase \lowercase (obsolete) 
ECHO         Enable echoing of input characters 
ECHOE         Echo erase character as BS-SP-BS 
ECHOK         Echo NL after kill character 
ECHONL         Echo NL 
NOFLSH         Disable flushing of input buffers after interrupt or quit characters 
IEXTEN         Enable extended functions 
ECHOCTL         Echo control characters as ^char and delete as ~? 
ECHOPRT         Echo erased character as character erased 
ECHOKE         BS-SP-BS entire line on line kill 
FLUSHO         Output being flushed 
PENDIN         Retype pending input at next read or input char 
TOSTOP         Send SIGTTOU for background output

c_iflag用于设置如何处理串口上接收到的数据,包含如下内容:
INPCK         Enable parity check 
IGNPAR         Ignore parity errors 
PARMRK      Mark parity errors 
ISTRIP         Strip parity bits 
IXON         Enable software flow control (outgoing) 
IXOFF         Enable software flow control (incoming) 
IXANY         Allow any character to start flow again 
IGNBRK         Ignore break condition 
BRKINT         Send a SIGINT when a break condition is detected 
INLCR         Map NL to CR 
IGNCR         Ignore CR 
ICRNL         Map CR to NL 
IUCLC         Map uppercase to lowercase 
IMAXBEL      Echo BEL on input line too long

c_oflag用于设置如何处理输出数据,包含如下内容:
OPOST         Postprocess output (not set = raw output) 
OLCUC         Map lowercase to uppercase 
ONLCR         Map NL to CR-NL 
OCRNL         Map CR to NL 
NOCR         No CR output at column 0 
ONLRET      NL performs CR function 
OFILL         Use fill characters for delay 
OFDEL         Fill character is DEL 
NLDLY         Mask for delay time needed between lines 
NL0            No delay for NLs 
NL1            Delay further output after newline for 100 milliseconds 
CRDLY      Mask for delay time needed to return carriage to left column 
CR0            No delay for CRs 
CR1            Delay after CRs depending on current column position 
CR2            Delay 100 milliseconds after sending CRs 
CR3            Delay 150 milliseconds after sending CRs 
TABDLY      Mask for delay time needed after TABs 
TAB0            No delay for TABs 
TAB1         Delay after TABs depending on current column position 
TAB2         Delay 100 milliseconds after sending TABs 
TAB3         Expand TAB characters to spaces 
BSDLY      Mask for delay time needed after BSs 
BS0         No delay for BSs 
BS1         Delay 50 milliseconds after sending BSs 
VTDLY      Mask for delay time needed after VTs 
VT0         No delay for VTs 
VT1         Delay 2 seconds after sending VTs 
FFDLY      Mask for delay time needed after FFs 
FF0         No delay for FFs 
FF1         Delay 2 seconds after sending FFs

c_cc定义了控制字符,包含以下内容:
VINTR  Interrupt  CTRL-C 
VQUIT  Quit    CTRL-Z 
VERASE   Erase    Backspace (BS) 
VKILL   Kill-line   CTRL-U 
VEOF   End-of-file   CTRL-D 
VEOL   End-of-line   Carriage return (CR) 
VEOL2   Second    end-of-line Line feed (LF) 
VMIN   Minimum number of characters to read  
VSTART   Start flow   CTRL-Q (XON) 
VSTOP   Stop flow   CTRL-S (XOFF) 
VTIME   Time to wait for data (tenths of seconds) 

注意:控制符VTIME和VMIN之间有复杂的关系。VTIME定义要求等待的时间(百毫米,通常是unsigned char变量),而VMIN定义了要求等待的最小字节数(相比之下,read函数的第三个参数指定了要求读的最大字节数)。
如果VTIME=0,VMIN=要求等待读取的最小字节数,read必须在读取了VMIN个字节的数据或者收到一个信号才会返回。
如果VTIME=时间量,VMIN=0,不管能否读取到数据,read也要等待VTIME的时间量。
如果VTIME=时间量,VMIN=要求等待读取的最小字节数,那么将从read读取第一个字节的数据时开始计时,并会在读取到VMIN个字节或者VTIME时间后返回。
如果VTIME=0,VMIN=0,不管能否读取到数据,read都会立即返回。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值