linux c serial communication

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

#define FALSE 0
#define TRUE  1
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,  
	B38400, B19200, B9600, B4800, B2400, B1200, B300, };  
int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,  
	38400,  19200,  9600, 4800, 2400, 1200,  300, };  

void SetBaudRate(int fd, int speed);//Set the Baud Rate
int SetParity(int fd,int databits,int stopbits,int parity);//Set the parity
int OpenDev(char *Dev);//Open device
int WriteToDev(int fd,char *cmd)  ;
char * ReadFromDev(int fd) ;
/** 
* The main function ,for test ,
* This main suits for lakeshore 218 temperature monitor
*/  
int main(int argc, char **argv)  
{  
	int fd;  
	int nRead;  
	char buff[512];  
	char *dev ="/dev/ttyUSB1";  
	fd = OpenDev(dev);  
	if (fd>0)  
		SetBaudRate(fd,9600);  
	else  
	{  
		printf("Can't Open Serial Port!\n");  
		exit(0);  
	}  
	if (SetParity(fd,7,1,'O')== FALSE)  
	{  
		printf("Set Parity Error\n");  
		exit(1);  
	}  
	//write
	printf("Writting......\n");
	WriteToDev(fd,"*idn?\n");
	//read
	printf("Reading......\n");
	char *dataFlow = ReadFromDev(fd) ;
	printf("%s",dataFlow);
	
	printf("\ndone!\n");
	close(fd);  
	exit(0);  
}  
/** 
* Open the device
*/  
int OpenDev(char *Dev)  
{  
	int fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY  
	if (-1 == fd)  
	{
		perror("Can't Open Serial Port");  
		return -1;  
	}  
	else  
		return fd;  
}  
int WriteToDev(int fd,char *cmd)  
{  
	int n_written = 0;
	do {
		n_written += write( fd, &cmd[n_written], 1 );
	}while (cmd[n_written-1] != '\n' && n_written > 0);
	return TRUE;
}  
char * ReadFromDev(int fd)  
{  
	int nRead = 0,flag;
	char buff[510];
	char dataFlow[512]={0};
	while((nRead = read(fd,buff,510))>0)  
	{  
		if(nRead < 0)
		{
			printf("Read error !");
			return "";
		}
		if(buff[nRead-1] == 10 || buff[nRead-1] == 13)break;
		 
		buff[nRead]='\0';  
		strcat(dataFlow, buff);		
	}  
	return dataFlow;
}  
void SetBaudRate(int fd, int speed)  
{  
	int   i;  
	int   status;  
	struct termios   Opt;  
	tcgetattr(fd, &Opt);  
	for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)  
	{  
		if  (speed == name_arr[i])  
		{  
			tcflush(fd, TCIOFLUSH);  
			cfsetispeed(&Opt, speed_arr[i]);  
			cfsetospeed(&Opt, speed_arr[i]);  
			status = tcsetattr(fd, TCSANOW, &Opt);  
			if  (status != 0)  
				perror("tcsetattr fd1");  
			return;  
		}  
		tcflush(fd,TCIOFLUSH);  
	}  
}  
/**
 * param  fd       :  The pointer of device
 * param  databits : Bits number of data ,7 or 8
 * param  stopbits : stop bits , 1 or 2
 * param  parity   : Parity Type , four types ,N,E,O,S 
 */  
int SetParity(int fd,int databits,int stopbits,int parity)  
{  
	struct termios options;  
	if  ( tcgetattr( fd,&options)  !=  0)  
	{  
		perror("SetupSerial 1");  
		return(FALSE);  
	}  
	options.c_cflag &= ~CSIZE;  
	switch (databits) /*Set data bits*/  
	{  
	case 7:  
		options.c_cflag |= CS7;  
		break;  
	case 8:  
		options.c_cflag |= CS8;  
		break;  
	default:  
		fprintf(stderr,"Unsupported data size\n");  
		return (FALSE);  
	}  
	switch (parity)  
	{  
	case 'n':  
	case 'N':  
		options.c_cflag &= ~PARENB;   /* Clear parity enable */  
		options.c_iflag &= ~INPCK;     /* Enable parity checking */  
		break;  
	case 'o':   /* Odd parity*/  
	case 'O':  
		options.c_cflag |= (PARODD | PARENB);  
		options.c_iflag |= INPCK;             /* Disnable parity checking */  
		break;  
	case 'e':   /* Enable parity */  
	case 'E':  
		options.c_cflag |= PARENB;       
		options.c_cflag &= ~PARODD;  
		options.c_iflag |= INPCK;       /* Disnable parity checking */  
		break;  
	case 'S':  /*as no parity*/  
	case 's':  
		options.c_cflag &= ~PARENB;  
		options.c_cflag &= ~CSTOPB;  
		break;  
	default:  
		fprintf(stderr,"Unsupported parity\n");  
		return (FALSE);  
	}  
	/* Set the stop bits */     
	switch (stopbits)  
	{  
	case 1:  
		options.c_cflag &= ~CSTOPB;  
		break;  
	case 2:  
		options.c_cflag |= CSTOPB;  
		break;  
	default:  
		fprintf(stderr,"Unsupported stop bits\n");  
		return (FALSE);  
	}  
	/* Set input parity option */  
	if (parity != 'n')  
		options.c_iflag |= INPCK;  
	options.c_cc[VTIME] = 150; // Stop in 5 seconds
	options.c_cc[VMIN] = 0;  

	tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */  
	if (tcsetattr(fd,TCSANOW,&options) != 0)  
	{  
		perror("SetupSerial 3");  
		return (FALSE);  
	}  
	return (TRUE);  
}  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值