?? amain.cpp
字號:
if((dtime++)>3000) //循環(huán)3000次未接收到應答或數據,退出
{
comerror=1;
break;
}
if(receivecount==1&&sendready==1)
{
if((combuf[0]&0xff)==0x80)
{
dtime=0;
sendready=0;
SendByte(0x01); //發(fā)送第二字節(jié)0x01,機臺號
}
else
{
comerror=1;
break;
}
}
if(receivecount==2&&sendready==1)
{
if((combuf[1]&0xff)==0x01)
{
dtime=0;
sendready=0;
SendByte(0x01); //發(fā)送第三字節(jié)0x01,工件號
}
else
{
comerror=1;
break;
}
}
if(receivecount==3&&sendready==1)
{
if((combuf[2]&0xff)==0x01)
{
dtime=0;
sendready=0;
SendByte(0x02); //發(fā)送第四字節(jié)0x02,讀數命令
}
else
{
comerror=1;
break;
}
}
if(receivecount>3&&receivecount<204&&sendready==1)
{
dtime=0;
sendready=0; //發(fā)送第5----204字節(jié),作為應答
SendByte(combuf[receivecount-1]&0xff);
}
if(receivecount==204)
{
SendByte(0xff); //發(fā)送第205字節(jié),結束通訊
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); //發(fā)送首字節(jié)0x80
for(;;)
{
key=bioskey(1);
if(key==ESCKEY) break;
if((dtime++)>3000) //循環(huán)3000次未接收到應答或數據,退出
{
comerror=1;
break;
}
if(receivecount==1&&sendready==1)
{
if((combuf[0]&0xff)==0x80)
{
dtime=0;
sendready=0;
SendByte(0x01); //發(fā)送第二字節(jié)0x01,機臺號
}
else
{
comerror=1;
break;
}
}
if(receivecount==2&&sendready==1)
{
if((combuf[1]&0xff)==0x01)
{
dtime=0;
sendready=0;
SendByte(0x01); //發(fā)送第三字節(jié)0x01,工件號
}
else
{
comerror=1;
break;
}
}
if(receivecount==3&&sendready==1)
{
if((combuf[2]&0xff)==0x01)
{
dtime=0;
sendready=0;
SendByte(0x01); //發(fā)送第四字節(jié)0x01,寫數命令
}
else
{
comerror=1;
break;
}
}
if(receivecount==4&&sendready==1)
{
if((combuf[3]&0xff)==0x01)
{
dtime=0;
sendready=0; //開始發(fā)送數據,第五字節(jié)
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; //應答有效,發(fā)送6-204字節(jié),數據
SendByte(head[receivecount-4].y);
}
else
{
comerror=1;
break;
}
}
if(receivecount==204&&sendready==1)
{
if((combuf[203]&0xff)==(head[199].y&0xff))
{
SendByte(0xff); //發(fā)送第205字節(jié)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()
//功能: 在作圖區(qū)編輯已有圖形
//入口參數:
//出口參數:
//----------------------------------------------------//
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; //獲取作圖區(qū)內欲修改的點
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()
//功能: 在作圖區(qū)顯示圖形界面背景:網格
//入口參數:
// 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()
//功能: 在作圖區(qū)顯示坐標線
//入口參數:
// 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 + -