Linux 串口通訊代碼

Linux 串口通訊代碼:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

enum Baud{
     b_9600=0,
	 b_19200,
	 b_38400,
	 b_115200
};

int baudset[]={B9600,B19200,B38400,B115200};

#ifdef X86
char *prtnumset[]={"/dev/ttyS0","/dev/ttyS1"};
#else
char *prtnumset[]={"/dev/ttySAC0","/dev/ttySAC1"};
#endif

void setDatabit(char pflag,struct termios *opt)
{
	opt->c_cflag &=~CSIZE;
	switch(pflag)
	{
		case '7':
			opt->c_cflag |= CS7;
			break;
		case '8':
		    opt->c_cflag |= CS8;
			break;
		default:
			break;
	}
}

void setParity(char bflag,struct termios *opt)
{
	switch(bflag)
	{
		case 'n':
		case 'N':
			opt->c_cflag &= ~PARENB;
			opt->c_iflag &= ~INPCK;
			break;

		case 'o':
		case 'O':
			opt->c_cflag |=(PARODD | PARENB);
			opt->c_iflag |= INPCK;
			break;

		case 'e':
		case 'E':
			opt->c_cflag |= PARENB;
			opt->c_cflag &= ~PARODD;
			opt->c_iflag |= INPCK;
			break;

		default:
			break;
	}
}

void setStopbit(char sflag,struct termios *opt)
{
	switch(sflag)
	{
		case '1':
			opt->c_cflag &= ~CSTOPB;
			break;
		
		case '2':
		    opt->c_cflag |= CSTOPB;
			break;
		default:
		 	break;
	}
}

/* attr : "databit,parity,stopbit" */
int setSrlAttr(int fd,int baud,char *attr)
{
	char *attropt ,*separator = ",";
	int attrindex = 0;
	struct termios srlopt;
	if( tcgetattr(fd,&srlopt) == -1)
	{
		perror("get tty attribute fail!");
		return -1;
	}
	cfsetispeed(&srlopt,baudset[baud]);   // set input speed
	cfsetospeed(&srlopt,baudset[baud]);   // set output speed
    
	attropt = strtok(attr,separator);
	while(attropt != NULL && attrindex < 3)
	{
		switch(attrindex)
		{
			case 0:
				setDatabit(attropt[0],&srlopt);
				break;
			case 1:
				setParity(attropt[0],&srlopt);
				break;
			case 2:
				setStopbit(attropt[0],&srlopt);
				break;
			default:
			 	break;
		}
	    attropt = strtok(NULL,separator);
		attrindex++;
	}
	/* non standar mode */
	srlopt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
	srlopt.c_oflag &= ~OPOST;
	/*ignor modem's statu line and enable receive*/
	srlopt.c_cflag |= (CLOCAL | CREAD);
	/*case d:MIN=0,TIME =0  */
	srlopt.c_cc[VMIN] = 0;
	srlopt.c_cc[VTIME] = 0; 

	tcflush(fd,TCIOFLUSH);
	if(tcsetattr(fd,TCSANOW,&srlopt))
	{
		perror("setup serial ");
		return -1;
	}
	return 0;
}

/******************************************
function:  open serail
parametre: prtnum (int)-- port number
            baud (char *)-- transission rate
return: file descriptor if success,else -1
******************************************/
int openSrl(int prtnum,int baud, const char *attr)
{
    //_Bool bb;
  	//struct termios srlopt; 
	char *attrtemp;
    int srlfd;

    srlfd = open(prtnumset[prtnum],O_RDWR|O_NOCTTY|
	            O_NDELAY|O_NONBLOCK);
	if(srlfd == -1)
	{
		printf("open %s fail\n",prtnumset[prtnum]);
		return -1;
	}
	else
		printf("open %s success\n",prtnumset[prtnum]);
   
    attrtemp = (char *)malloc(strlen(attr)+1);
	strcpy(attrtemp,attr);

	if(setSrlAttr(srlfd,baud,attrtemp)!= 0)
		return -1;
	free(attrtemp);
	attrtemp = NULL;
	return srlfd;
}

int  main()
{
    int  sfd ,rdnum = 0; 
	char srbuf[200] = {0};
	char *swbuf ="pc:hello;msn from embed";
    //openSrl(0,b_19200,"8,n,1");
    if((sfd = openSrl(0,b_19200,"8,n,1")) == -1)
	{
		exit(1);
	}
	while(1)
	{
	#ifdef ARM
		rdnum = read(sfd,srbuf,sizeof(srbuf));
		if(rdnum > 0)
		{
			printf("rdnum = %d\n",rdnum);
			printf("msn:%s\n",srbuf);
		}
		else if(rdnum == -1)
			perror("error: ");
		else
		{
		    printf("rdnum = %d  ",rdnum);
			printf("no msn\n");
		}
	#else
	    rdnum = write(sfd,swbuf,strlen(swbuf)); 
	#endif
		sleep(1);
		//printf("enhh !\n");
	}
	return 1;
}


 

 

 

 

 

 

發佈了53 篇原創文章 · 獲贊 31 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章