⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comportoperation_re.cpp

📁 这是串口的头文件
💻 CPP
字号:

/*
* Copyright (c) 2009,贝尔核仪器事业部
* All rights reserved.
*
* 文件名称:serialoperation.cpp
* 文件标识:见配置管理计划书
* 摘 要:对文件的读写进行类的封装,该类对应于所有文件的操作。
*
* 当前版本:1.0
* 作 者:谭海
* 完成日期:2009年2月20日
*
* 取代版本:1.0
* 原作者 :输入原作者(或修改者)名字
* 完成日期:2001年5月10日
*/


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

#include <qfile.h>

//#include "fileoperation.h"
#include "comportoperation_re.h"

unsigned short data[] = {
	1,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		2,
		7,
		8,
		4,
		3,
		7,
		8,
		7,
		8,
		11,
		5,
		7,
		7,
		4,
		5,
		9,
		3,
		2,
		4,
		2,
		5,
		6,
		9,
		2,
		5,
		4,
		6,
		4,
		5,
		6,
		8,
		5,
		3,
		4,
		3,
		5,
		3,
		2,
		6,
		11,
		2,
		2,
		5,
		5,
		3,
		2,
		5,
		5,
		2,
		5,
		5,
		3,
		9,
		6,
		5,
		2,
		4,
		4,
		2,
		3,
		3,
		4,
		3,
		1,
		8,
		3,
		4,
		2,
		5,
		3,
		6,
		6,
		2,
		3,
		3,
		4,
		4,
		5,
		2,
		1,
		1,
		9,
		2,
		6,
		7,
		2,
		5,
		3,
		4,
		1,
		5,
		7,
		8,
		6,
		1,
		7,
		6,
		2,
		4,
		1,
		2,
		7,
		0,
		3,
		2,
		4,
		1,
		4,
		3,
		3,
		5,
		8,
		2,
		5,
		5,
		0,
		3,
		1,
		3,
		2,
		3,
		5,
		4,
		2,
		7,
		8,
		4,
		5,
		2,
		7,
		2,
		4,
		3,
		2,
		4,
		5,
		3,
		7,
		8,
		3,
		3,
		1,
		1,
		2,
		6,
		3,
		7,
		6,
		0,
		8,
		5,
		9,
		7,
		2,
		8,
		4,
		3,
		6,
		3,
		6,
		4,
		2,
		7,
		2,
		6,
		2,
		1,
		3,
		1,
		5,
		3,
		1,
		4,
		2,
		6,
		3,
		3,
		2,
		5,
		4,
		3,
		5,
		4,
		0,
		0,
		6,
		2,
		2,
		2,
		3,
		0,
		3,
		5,
		5,
		2,
		1,
		4,
		2,
		5,
		5,
		2,
		0,
		2,
		1,
		0,
		4,
		4,
		4,
		1,
		1,
		4,
		1,
		1,
		1,
		1,
		2,
		3,
		1,
		2,
		2,
		2,
		0,
		2,
		3,
		2,
		2,
		2,
		2,
		2,
		2,
		5,
		0,
		1,
		0,
		0,
		1,
		1,
		1,
		3,
		6,
		2,
		3,
		3,
		2,
		0,
		3,
		4,
		1,
		1,
		1,
		1,
		1,
		1,
		2,
		1,
		2,
		2,
		1,
		1,
		2,
		3,
		3,
		0,
		3,
		2,
		1,
		1,
		0,
		1,
		0,
		2,
		0,
		1,
		1,
		1,
		2,
		1,
		2,
		1,
		4,
		1,
		1,
		6,
		4,
		2,
		2,
		2,
		3,
		2,
		2,
		1,
		2,
		0,
		2,
		2,
		2,
		1,
		4,
		0,
		0,
		1,
		1,
		4,
		0,
		2,
		0,
		1,
		1,
		0,
		1,
		2,
		1,
		2,
		2,
		0,
		1,
		2,
		1,
		3,
		2,
		1,
		0,
		1,
		1,
		0,
		2,
		0,
		1,
		0,
		0,
		3,
		0,
		1,
		0,
		5,
		1,
		2,
		2,
		0,
		4,
		2,
		2,
		0,
		3,
		2,
		1,
		1,
		2,
		1,
		3,
		5,
		2,
		5,
		2,
		4,
		2,
		2,
		4,
		2,
		1,
		2,
		1,
		0,
		2,
		2,
		1,
		3,
		0,
		0,
		0,
		0,
		1,
		3,
		1,
		1,
		1,
		0,
		2,
		2,
		3,
		1,
		2,
		0,
		0,
		1,
		3,
		0,
		2,
		1,
		1,
		3,
		2,
		4,
		1,
		0,
		3,
		1,
		4,
		1,
		2,
		1,
		3,
		1,
		1,
		3,
		1,
		1,
		2,
		1,
		1,
		3,
		1,
		2,
		2,
		2,
		1,
		3,
		1,
		3,
		0,
		1,
		2,
		3,
		2,
		0,
		0,
		5,
		2,
		2,
		5,
		1,
		0,
		2,
		3,
		2,
		3,
		3,
		3,
		4,
		1,
		2,
		3,
		1,
		3,
		4,
		2,
		0,
		0,
		4,
		3,
		1,
		2,
		3,
		3,
		2,
		1,
		2,
		2,
		1,
		2,
		3,
		1,
		0,
		4,
		2,
		4,
		3,
		0,
		2,
		5,
		0,
		4,
		4,
		1,
		1,
		3,
		0,
		1,
		3,
		2,
		0,
		1,
		3,
		1,
		4,
		0,
		3,
		3,
		2,
		4,
		2,
		2,
		4,
		2,
		1,
		0,
		1,
		4,
		3,
		3,
		3,
		1,
		4,
		5,
		5,
		6,
		10,
		9,
		2,
		6,
		4,
		8,
6,
13,
11,
9,
9,
2,
7,
16,
10,
10,
13,
13,
9,
11,
9,
13,
8,
14,
9,
2,
4,
1,
2,
4,
1,
7,
3,
1,
4,
2,
6,
1,
3,
2,
2,
4,
2,
1,
2,
0,
1,
4,
1,
2,
3,
2,
1,
3,
2,
4,
4,
5,
4,
5,
8,
2,
5,
7,
2,
2,
7,
4,
6,
6,
2,
3,
3,
4,
5,
5,
3,
1,
4,
5,
4,
2,
4,
1,
5,
3,
1,
2,
7,
5,
4,
9,
2,
7,
4,
3,
5,
5,
5,
4,
5,
4,
5,
1,
5,
3,
3,
4,
3,
8,
1,
6,
6,
2,
4,
6,
2,
6,
3,
7,
6,
1,
4,
3,
2,
4,
3,
2,
3,
1,
4,
4,
4,
4,
2,
4,
5,
4,
3,
2,
5,
6,
2,
0,
4,
6,
3,
7,
1,
4,
5,
3,
3,
4,
8,
3,
8,
4,
7,
4,
3,
3,
6,
2,
3,
5,
2,
6,
6,
7,
2,
6,
2,
6,
7,
2,
5,
10,
2,
4,
3,
4,
7,
5,
6,
5,
9,
4,
4,
7,
7,
4,
3,
5,
11,
5,
4,
7,
4,
8,
9,
5,
10,
10,
10,
11,
5,
6,
4,
5,
8,
10,
8,
8,
10,
6,
12,
11,
14,
11,
14,
16,
19,
21,
22,
13,
34,
36,
50,
48,
68,
89,
106,
150,
137,
196,
248,
261,
375,
412,
500,
632,
702,
793,
952,
965,
1089,
1350,
1416,
1515,
1743,
1788,
1861,
1969,
2069,
2063,
2011,
2028,
2061,
1930,
1956,
1895,
1693,
1586,
1445,
1342,
1202,
1136,
988,
839,
744,
610,
539,
495,
381,
297,
253,
239,
189,
147,
118,
109,
63,
51,
51,
37,
24,
25,
24,
13,
8,
11,
6,
12,
4,
10,
3,
4,
11,
6,
9,
5,
7,
12,
4,
7,
8,
9,
10,
14,
15,
18,
22,
15,
17,
38,
31,
34,
50,
46,
58,
67,
77,
71,
106,
143,
122,
154,
192,
207,
204,
215,
245,
256,
270,
256,
293,
295,
305,
303,
287,
279,
260,
265,
227,
228,
211,
198,
191,
171,
143,
120,
135,
93,
70,
72,
72,
50,
40,
37,
34,
14,
22,
12,
10,
13,
6,
6,
6,
5,
2,
0,
1,
1,
0,
4,
1,
1,
1,
0,
0,
0,
0,
1,
0,
0,
1,
3,
1,
1,
1,
0,
0,
1,
1,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
1,
0,
1,
0,
0,
0,
1,
0,
0,
0,
129,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
};

Ccomport_operation::Ccomport_operation()
{
}


Ccomport_operation::~Ccomport_operation()
{
}

//打开串口,支持计算机上的三个串口,分别为1,2,3
int Ccomport_operation::comport_open(int comport)
{

 


	int fd;

	//获取系统当前时间,保存在成员comport_data中
	// gettime

	//设置公司和软件版本信息


//	char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
//	long  vdisable;
	if (comport==1)
	{	fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);
		if (-1 == fd){
			perror("Can't Open Serial Port");
			return(-1);
		}
	}
	else if(comport==2)
	{	fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
		if (-1 == fd){
			perror("Can't Open Serial Port");
			return(-1);
		}
	}
	else if (comport==3)
	{
		fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);
		if (-1 == fd){
			perror("Can't Open Serial Port");
			return(-1);
		}
	}

	//if(fcntl(fd, F_SETFL , 0)<0)
	if(fcntl( fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK ) <0)
		printf("fcntl failed!\n");
	else
		printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
	if(isatty(STDIN_FILENO)==0)
		printf("standard input is not a terminal device\n");
	else
		printf("isatty success!\n");
	printf("fd-open=%d\n",fd);
	printf("jinyanfa!!\n");
	return fd;
}



//串口设置:波特率,速度,位数,停止位
int Ccomport_operation::comport_set(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{

	struct termios newtio,oldtio;
	if  ( tcgetattr( fd,&oldtio)  !=  0) { 
		perror("SetupSerial 1");
		return -1;
	}
	bzero( &newtio, sizeof( newtio ) );
	newtio.c_cflag  |=  CLOCAL | CREAD; 
	newtio.c_cflag &= ~CSIZE; 

	switch( nBits )
	{
	case 7:
		newtio.c_cflag |= CS7;
		break;
	case 8:
		newtio.c_cflag |= CS8;
		break;
	}
	printf("\nnEvent %c\n",nEvent);
	switch( nEvent )
	{
	case 'O':
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK | ISTRIP);
		break;
	case 'E': 
		newtio.c_iflag |= (INPCK | ISTRIP);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':  
		newtio.c_cflag &= ~PARENB;
		newtio.c_iflag &= ~INPCK;//
		printf("N\n");
		break;
	}

switch( nSpeed )
	{
	case 2400:
		cfsetispeed(&newtio, B2400);
		cfsetospeed(&newtio, B2400);
		break;
	case 4800:
		cfsetispeed(&newtio, B4800);
		cfsetospeed(&newtio, B4800);
		break;
	case 9600:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	case 115200:
		cfsetispeed(&newtio, B115200);
		cfsetospeed(&newtio, B115200);
		break;
	case 460800:
		cfsetispeed(&newtio, B460800);
		cfsetospeed(&newtio, B460800);
		break;
	default:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	}
	if( nStop == 1 )
		newtio.c_cflag &=  ~CSTOPB;
	else if ( nStop == 2 )
	newtio.c_cflag |=  CSTOPB;
	newtio.c_cc[VTIME]  = 0;
	newtio.c_cc[VMIN] = 0;
	tcflush(fd,TCIFLUSH);
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		return -1;
	}
	printf("set done!\n");
	return 0;
}




//串口接收多道板数据
int Ccomport_operation::comport_receiveboarddata(int fd)
{
	
	int i,nread = 1;
	unsigned short r_data[1024];
	unsigned char temp[2048]={0x0};
	
	do 
	{
		nread = read(fd, temp, 2048);	
		printf("%s",temp);
		printf("%d\n",nread);
		temp[0]='\0';
		getchar();
		//	sleep(1);
	}while(nread != 2048);
		
	for(i=0; i<1024; i++)
	{
		//r_data[i] = (tmp_data[2*i+1]<<8)+tmp_data[2*i];
		r_data[i] = temp[2*i+1]*256u+temp[2*i];
		//data[i]+ = comport_data.r_data[i];							//拷贝接收数据去显示数组,实现动态显示
      //printf("%d,%d,",temp[2*i+1],temp[2*i]);
		// printf("%d,",r_data[i]);
		//printf("%4u:%2u*256+%4u = %4u ",i,temp[2*i+1],temp[2*i],r_data[i]);
		if(i%4 == 0)
		{
		//	printf("\n");
		}
	}
	return nread;
	
}

//串口发送停止位给多道板
int Ccomport_operation::comport_sendboardcommand(int fd)
{

	
		
	unsigned char temp[2048]={0x0};
    for(int i=0;i<1024;i++)
	{
		//printf("%d,",data[i]);
		temp[2*i+1] = data[i]/256u;
		//printf("%d,",temp[2*i+1]);
		temp[2*i] = data[i]%256u;
		//printf("%d,",temp[2*i]);
	//	printf("%d,%d,",temp[2*i+1],temp[2*i]);
	//	printf("%d*256+%d = %d\n",temp[2*i+1],temp[2*i],data[i]);
	//	printf("%4u:%2u*256+%4u = %4u ",i,temp[2*i+1],temp[2*i],data[i]);
		//printf("%d,",data[i]);
		if(i%4 == 0)
		{
	//		printf("\n");
		}
		
	}
	
	write(fd,temp,2048);
	//nread = write(fd, tmp_data, 2048);							//下发停止命令给多道板
	
	//return nread;

}


//串口发送实时数据给PC上位机
int Ccomport_operation::comport_sendpcdata(int fd)
{
	int nwrite;
		int r_data[1024];

	nwrite = write (fd, r_data, 1024);
	
	return nwrite;

}


//串口发送历史文件给PC上位机
int Ccomport_operation::comport_sendpcfile(int fd, QString file_name)
{
	QString str_Line;																	//按行传输
	QFile file(file_name);
	int nwrite = 0;
	int file_Lines = 0;
	if ( !file.open( IO_ReadOnly ) )    return -1;


	do
	{
   		file.readLine(str_Line, 1024);													//读入一行
		nwrite = write(fd, str_Line.latin1(),sizeof(str_Line.latin1()));				//向串口发送数据
		file_Lines ++;
	}while(!file.atEnd());


	file.close();
	return file_Lines;

}

int Ccomport_operation::comport_close(int fd)
{
	close(fd);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -