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

📄 amain.cpp

📁 汽车气门生产自控系统控制算法及源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		if((dtime++)>3000)      //循环3000次未接收到应答或数据,退出
		{
			comerror=1;
			break;
		}
		if(receivecount==1&&sendready==1)
		{
			if((combuf[0]&0xff)==0x80)
			{
				dtime=0;
				sendready=0;
                                SendByte(0x01);     //发送第二字节0x01,机台号
			}
			else
			{
				comerror=1;
				break;
			}
		}
		if(receivecount==2&&sendready==1)
		{
                        if((combuf[1]&0xff)==0x01)
			{
				dtime=0;
				sendready=0;
				SendByte(0x01);     //发送第三字节0x01,工件号
			}
			else
			{
				comerror=1;
				break;
			}
		}
                if(receivecount==3&&sendready==1)
		{
			if((combuf[2]&0xff)==0x01)
			{
				dtime=0;
				sendready=0;
				SendByte(0x02);    //发送第四字节0x02,读数命令
			}
			else
			{
				comerror=1;
				break;
			}
		}
                if(receivecount>3&&receivecount<204&&sendready==1)
		{
			dtime=0;
			sendready=0;    //发送第5----204字节,作为应答
			SendByte(combuf[receivecount-1]&0xff);
		}
                if(receivecount==204)
                {
			SendByte(0xff); //发送第205字节,结束通讯
			break;
                }
        };
	if(comerror)
	{
		ClearState();
		PutCC16(81,461,0,14,inferr);      //显示错误信息
	}
	else
	{
		ClearState();
		PutCC16(81,461,0,14,infok);       //显示成功信息
		for(i=0;i<200;i++)
		{
			if(i<100)                      //保存数据
			{       head[i].type=PRESSURE;
				head[i].color=PCOLOR;
				head[i].x=i;
			}
			else
			{       head[i].type=VOLTAGE;
				head[i].color=VCOLOR;
				head[i].x=i-100;
			}
                        head[i].y=combuf[i+3]&0xff;
		}
		graphexist=TRUE;   //图形已调入内存
		ShowBkGrnd(1);      //显示作图背景网格
		ShowCoord(10,20);  //显示坐标轴刻度
		ShowGraph(1);       //显示曲线
	}
	free(combuf);
	ComIntrClose();
	delay(500);
	ClearState();
	ShowCurState();
	return;
}

//----------------------------------------------------//
//函数名:WRITETOIFD()
//功能:  向外设---串口写数据
//入口参数:
//出口参数:
//----------------------------------------------------//
void WriteToIFD()
{
	int key,dtime=0;
        char *inferr="通讯失败";
	char *infok="通讯成功";
	combuf=(char*)malloc((FILE_SIZE+4)*sizeof(char));
	receivecount=0;
	sendcount=0;
	sendready=0;
	comerror=0;

	SetComIntr();
	InitCom();

        SendByte(0x80);         //发送首字节0x80
	for(;;)
	{
		key=bioskey(1);
		if(key==ESCKEY) break;
                if((dtime++)>3000)      //循环3000次未接收到应答或数据,退出
		{
			comerror=1;
			break;
		}
		if(receivecount==1&&sendready==1)
		{
			if((combuf[0]&0xff)==0x80)
			{
				dtime=0;
				sendready=0;
                                SendByte(0x01);     //发送第二字节0x01,机台号
			}
			else
			{
				comerror=1;
				break;
			}
		}
		if(receivecount==2&&sendready==1)
		{
                        if((combuf[1]&0xff)==0x01)
			{
				dtime=0;
				sendready=0;
				SendByte(0x01);     //发送第三字节0x01,工件号
			}
			else
			{
				comerror=1;
				break;
			}
		}
                if(receivecount==3&&sendready==1)
		{
                        if((combuf[2]&0xff)==0x01)
			{
				dtime=0;
				sendready=0;
				SendByte(0x01);    //发送第四字节0x01,写数命令
			}
			else
			{
				comerror=1;
				break;
			}
		}
                if(receivecount==4&&sendready==1)
		{
                        if((combuf[3]&0xff)==0x01)
			{
				dtime=0;
				sendready=0;    //开始发送数据,第五字节
				SendByte(head[receivecount-4].y);
                        }
			else
			{
				comerror=1;
				break;
			}
		}
                if(receivecount>4&&receivecount<204&&sendready==1)
		{
                        if((combuf[receivecount-1]&0xff)==(head[receivecount-5].y&0xff))
			{
				dtime=0;
                                sendready=0;    //应答有效,发送6-204字节,数据
                                SendByte(head[receivecount-4].y);
			}
			else
			{
				comerror=1;
				break;
			}
		}
                if(receivecount==204&&sendready==1)
		{
			if((combuf[203]&0xff)==(head[199].y&0xff))
			{
				SendByte(0xff); //发送第205字节0xff,结束通讯
                                break;
			}
		}
	};
	free(combuf);
	if(comerror)
	{
		ClearState();
		PutCC16(81,461,0,14,inferr);      //显示错误信息
	}
	else
	{
		ClearState();
		PutCC16(81,461,0,14,infok);       //显示成功信息
	}
	ComIntrClose();
	delay(500);
	ClearState();
	ShowCurState();
	return;
}

//----------------------------------------------------//
//函数名:GETFILE()
//功能:  获取有效文件名
//入口参数:
//      flag:调用方式
//           WRITE:写文件时调用
//           READ: 读文件时调用
//出口参数:
//      0:无效
//      1:有效
//----------------------------------------------------//
int GetFile(char flag)
{
	int x1,y1,len,high,i,j;
	FILE *fp;
	char *buf;
	char oldcolor,filenm[12],yn[2],ret=0;
        char *help="按ESC键取消";
	char *msg="文件名";
	char *filenot="文件不存在,重新输入吗?";
	char *filexist="文件存在,覆盖吗?";

	ClearState();
	PutCC16(81,461,0,14,help);      //显示帮助信息
	HideMouse();
	oldcolor=getcolor();
	setfillstyle(1,7);
	setcolor(15);
	x1=80;
	if (flag==READ) y1=61;
	if (flag==WRITE) y1=306;
	len=230;
	high=50;
	buf=(char*)malloc(imagesize(x1,y1,x1+len,y1+high));
	getimage(x1,y1,x1+len,y1+high,buf);
	bar(x1,y1,x1+len,y1+high);
	rectangle(x1,y1,x1+len,y1+high);
	rectangle(x1+2,y1+2,x1+len-2,y1+high-2);
	for (;;)
	{
		setfillstyle(1,7);
		bar(x1+4,y1+4,x1+len-4,y1+high-4);
		PutCC16(x1+18,y1+17,0,14,msg);
		GetData(NAME,x1+18,y1+17,msg,filenm,12);
		if (strchr(filenm,' ')||!strcmp(filenm,"")) {ret=1;break;}
		if (!strchr(filenm,'.')) strcat(filenm,".DAT");
		setfillstyle(1,3);
		bar(x1+4,y1+4,x1+len-4,y1+high-4);
		fp=fopen(filenm,"r");
		if (flag==WRITE && (!fp)) break;        //写盘且文件不存在
		if (flag==READ && (fp)) break;          //读入且文件存在
		if (flag==READ && (!fp))                //读入且文件不存在
		{
			PutCC16(x1+18,y1+17,0,14,filenot);
			GetData(YN,x1+18,y1+17,filenot,yn,1);
			if (!strcmp(yn,"Y")) continue;  //重新输入文件名
			else { ret=1;break;}
		}
		if (flag==WRITE && (fp))                //写盘且文件存在
		{
			PutCC16(x1+18,y1+17,0,14,filexist);
			GetData(YN,x1+18,y1+17,filexist,yn,1);
			if (!strcmp(yn,"Y")) break;     //重写
			else continue;
		}
	}
	putimage(x1,y1,buf,COPY_PUT);
	ClearState();
	ShowCurState();
	free(buf);
	ShowMouse();
	setcolor(oldcolor);
	if (ret) return 0;
	strcpy(filename,filenm);
	fclose(fp);
	return(1);
}

//----------------------------------------------------//
//函数名:EDITGRAPH()
//功能:  在作图区编辑已有图形
//入口参数:
//出口参数:
//----------------------------------------------------//
void EditGraph()
{
	int x1,y1,xx1,xx2;
	int x2,y2,yy1,yy2;
	int i;
	char inboxfg=FALSE;
	void *buf;
        char *pos1="指定取线上一点,右键退出";
	char *pos2="指定目标位置";
        char *help="左键确定,右键取消";

        if (graphexist!=TRUE) return;   //当前无图可编辑,返回
	setcolor(drcolor);
	for(;;)
	{
		inboxfg=FALSE;
		ClearState();
		PutCC16(81,461,0,14,pos1);      //显示帮助信息
		if (!Get1stPoint(&x1,&y1)) break;   //获取作图区内欲修改的点
		for (i=0;i<200;i++)
		{
			xx1=(head[i].x-x0)*10+130;
			yy1=-(head[i].y-y0)*5-2+410;
			if(MsInBox(xx1-2,yy1-2,xx1+2,yy1+2,x1,y1))
			{
				inboxfg=TRUE;   //判断是否为曲线上的点
				break;
			}
		}
		PutCC16(81,461,0,14,pos2);
		PutCC16(200,461,0,15,help);
                if (!Get2ndPoint(x1,y1,&x2,&y2)) continue; //获取目标点坐标
		if (inboxfg==TRUE)
		{
			buf=malloc(imagesize((head[i].x-x0)*10-2+130,
			     -(head[i].y-y0)*5-2+410,(head[i].x-x0)*10+2+130,
			     -(head[i].y-y0)*5+2+410));
			getimage((head[i].x-x0)*10-2+130,
			     -(head[i].y-y0)*5-2+410,(head[i].x-x0)*10+2+130,
			     -(head[i].y-y0)*5+2+410,buf);
			putimage((head[i].x-x0)*10-2+130,
			     -(head[i].y-y0)*5-2+410,buf,XOR_PUT);
			putimage(x2-2,y2-2,buf,COPY_PUT);
			head[i].y=(410-y2)/5+y0;
			free(buf);
			inboxfg=FALSE;    //重置标志位,以便下一次修正曲线
			modified=TRUE;    //文件修改标志置1
		}
	}
	InitMouse();
	SetMouseCoord(x2,y2);
	ShowMouse();
	setlinestyle(0,0,1);
	ClearState();
	ShowCurState();
}

//----------------------------------------------------//
//函数名:SHOWBKGRND()
//功能:  在作图区显示图形界面背景:网格
//入口参数:
//      yratio:    网格绘制比例
//                 1----2:1
//                 2----1:1
//出口参数:
//----------------------------------------------------//
void ShowBkGrnd(int yratio)
{
	int i,oldcolor;

	oldcolor=getcolor();
	setfillstyle(1,0);
	bar(128,8,632,412);
	setbkcolor(8);
	setcolor(14);
	setlinestyle(0,0,1);
	i=0;
	line(130+i*10,10,130+i*10,410); //画坐标轴
	line(130,410+i*10,630,410+i*10);
	line(131+i*10,10,131+i*10,410);
	line(130,409+i*10,630,409+i*10);
	setcolor(1);
	for(i=1;i<=50;++i)      //画网格
	{
		line(130+i*10,10,130+i*10,409);
		if ((i%5)==0) line(131+i*10,10,131+i*10,410);
	}
	for(i=0;i<80/yratio;++i)
	{
		line(131,10+i*5*yratio,630,10+i*5*yratio);
		if ((i%(10/yratio))==0) line(131,11+i*5*yratio,630,11+i*5*yratio);
	}
	setcolor(oldcolor);
}

//----------------------------------------------------//
//函数名:SHOWCOORD()
//功能:  在作图区显示坐标线
//入口参数:
//      xstep:    水平坐标线步距
//      ystep:    垂直坐标线步距
//出口参数:
//----------------------------------------------------//
void ShowCoord(int xstep,int ystep)
{
	int i,oldcolor;
	char *pstr="压力曲线";
	char *vstr="电压曲线";
	char hor[4],ver[4];

	oldcolor=getcolor();
	setfillstyle(0,1);
	bar(80,6,128,430);
	bar(80,418,638,430);
	setcolor(14);
	settextstyle(0,0,1);
	setwritemode(XOR_PUT);
	for(i=0;i<=4;++i)       //画纵向坐标刻度
	{
		sprintf(ver,"%3d",(y0+i*ystep));
		outtextxy(102,406-i*100,ver);
	}
	for(i=0;i<=5;++i)       //画横向坐标刻度
	{
		sprintf(hor,"%3d",(x0+i*xstep));
		outtextxy(114+i*100,418,hor);
	}
	setfillstyle(1,PCOLOR);

⌨️ 快捷键说明

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