基本信息
源码名称:hal 串口通信 示例代码
源码大小:4.86KB
文件格式:.c
开发语言:C/C++
更新时间:2015-06-11
   源码介绍


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

#include <unistd.h>
#include <errno.h>
#include <utils/Log.h>	

#include "minicom.h"

#define BAUDRATE        115200
#define FALSE  -1
#define TRUE   0

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

static int fd;

void set_speed(int fd, int speed)
{
	unsigned int i; 
	
	struct termios options;
	if( tcgetattr( fd,&op4tions)==FALSE)//get termios 
	{ 
		perror("tcgetattr");     
		return;  
	}
	
	for ( i= 0;i<sizeof(speed_arr)/sizeof(int);i  ) 
	{ 
		if(speed == name_arr[i]) 
		{     
			tcflush(fd, TCIOFLUSH);//flush I/O buffer    
			cfsetispeed(&options, speed_arr[i]);//set input speed   设置输入波特率
			cfsetospeed(&options, speed_arr[i]);//set  output speed   
			if(tcsetattr(fd, TCSANOW, &options) == FALSE)//set termios now   
			{        
				perror("tcsetattr");  
				return;     
			}    
			tcflush(fd,TCIOFLUSH);//flush I/O buffer   
		}  
	}
}

int set_Parity(int fd,int databits,int stopbits,int parity)
{ 
	struct termios options; 
	if( tcgetattr(fd,&options)==FALSE)//get termios  对终端设备的操作模式的设置和获取
	{ 
		perror("tcgetattr");     
		return(FALSE);  
	}
	//set databits
	options.c_cflag &= ~CSIZE; 
	switch (databits) 
	{   
	case 7:		
		options.c_cflag |= CS7; 
		break;
	case 8:     
		options.c_cflag |= CS8;
		break;   
	default:    
		fprintf(stderr,"Unsupported data size\n"); 
		return (FALSE);  
	}
	//set parity
	switch (parity) 
	{   
	case 'n':
	case 'N':    
		options.c_cflag &= ~PARENB;//unable parity 奇偶校验位使能  
		options.c_iflag &= ~INPCK;//unable input parity    使能帧和奇偶校验错误检查 
		break;  
	case 'o':   
	case 'O':     
		options.c_cflag |= (PARODD | PARENB);//enable odd parity 使能时字符被接受
		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':     
		options.c_cflag &= ~PARENB;
		options.c_cflag &= ~CSTOPB;//set one stop bit 
		break;  
	default:   
		fprintf(stderr,"Unsupported parity\n");    
		return (FALSE);  
	}  

	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 
	if (parity != 'n')   
		options.c_iflag |= INPCK;
	tcflush(fd,TCIFLUSH);
	
	options.c_cc[VTIME] = 150;//timeout  
	options.c_cc[VMIN] = 0;
	
	if (tcsetattr(fd,TCSANOW,&options) == FALSE)   
	{ 
		perror("tcsetattr");   
		return (FALSE);  
	} 
	options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);
	options.c_oflag  &= ~OPOST;
	return (TRUE);  4
}

int usb_open(void)
{
	//open
	LOGD("------%s-----\n", __FUNCTION__);
	fd = open("/dev/ttyUSB0", O_RDWR);
	if (fd < 0) 
	{
		LOGE("open error: %s\n", strerror(errno));
		exit(1);
	}
	//set
	set_speed(fd,BAUDRATE);
	if (set_Parity(fd,8,1,'N') == FALSE) 
	{
		LOGE("open error: %s\n", strerror(errno));
		exit (1);
	}
	return 0;
}

int usb_device_control(int flag)
{
	LOGD("------%s-----\n", __FUNCTION__);
	
	char on = '0';
	int ret;
	
	if(flag){

		on = '1';	
		ret = write(fd, &on, 1);
		if(ret < 0)
		{
			LOGE("write on error: %s\n", strerror(errno));
			return -1;
		}
	}else{
		on = '0';	
		ret = write(fd, &on, 1);
		if(ret < 0)
		{
			LOGE("write on error: %s\n", strerror(errno));
			return -1;
		}

	}
	return 0;
}

int usb_close(struct hw_device_t* device)
{
	if(device != NULL)
	{
		free(device);
	}
	close(fd);
	return 0;
}

int usb_module_open(const struct hw_module_t *module,const char* id, struct hw_device_t** device)
{
	LOGD("------%s-----\n", __FUNCTION__);
	
	//实例化device对象
	struct usb_hw_device_t *usbdev;
	usbdev = (struct usb_hw_device_t *)malloc(sizeof(struct usb_hw_device_t));
	if(usbdev == NULL)
	{
		LOGE("malloc error\n");
		return -1;
	}
	usbdev->common.tag = HARDWARE_DEVICE_TAG;
	usbdev->common.version = 1;
	usbdev->common.module = module;
	usbdev->common.close = usb_close;
	usbdev->open = usb_open;
	usbdev->control_dev = usb_device_control;

	//将usbdev传递给一个jni
	*device = (struct hw_device_t *)usbdev;

	return 0;
}


//定义一个方法集合
struct hw_module_methods_t usb_module_methods = {
	open : usb_module_open,
};

//用自定义的module对象定义HMI(hal的入口)
struct usb_hw_module_t HMI = {
	common : {
		tag : HARDWARE_MODULE_TAG,
			version_major : 1,
			version_minor : 0,
			id : MINICOM_ID,
			name : "this is PL2303 hal for fs210",
			author : "caiii",
			methods : &usb_module_methods,
	},
};