|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
最近朋友给了段程序,怎么也看不明白.他让改个界面.高手们帮帮忙了
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#define LEN 1024
HANDLE hndCom;
char r_buf[LEN],r_b[LEN];
int head,tail;
COMSTAT ComSt;
DWORD nb;
void main()
{
DCB comd;
char cm[40],rtx[50],rty[50];
float x,y;
char sn[]="0";
head=tail=0;
hndCom = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,
0,0);
if (hndCom==INVALID_HANDLE_VALUE)
{
printf("Could n`t Open COM1! Press any key exit");
getch();
return;
}
else
{
gotoxy(20,24);
printf("ress any key exit");
}
strcpy(cm,"COM1:9600,n,8,1,");
GetCommState(hndCom,&comd);
BuildCommDCB(cm,&comd);
SetupComm(hndCom,1000,1000);
SetCommState(hndCom,&comd);
int i,j,len,okx=0,oky=0;
bool loop=true;
long times=0;
DWORD n;
while (loop)
{
if(ClearCommError(hndCom,&nb,&ComSt)==TRUE)
{
if (ComSt.cbInQue>0)
{
ReadFile(hndCom,&r_buf,ComSt.cbInQue,&n,NULL);
r_buf[n]=0;
for(i=0;i<n;++i)
{
r_b[tail]=r_buf;
++tail;
if (tail>=LEN) tail=0;
}
if (tail>=head) len=tail-head;
else
len=LEN-head+tail;
i=head;
j=0;
while (len>0)
{
if (r_b=='X' && len>=11)
{
for (j=0;j<9;++j)
rtx[j]=r_b[i+j+2];
rtx[j]=0;
head=head+11;
len=len-11;
if (head>=LEN) head=head-LEN;
if (rtx[5]=='.') okx=1;
}
if (r_b=='Y' && len>=11)
{
for (j=0;j<9;++j)
rty[j]=r_b[i+j+2];
rty[j]=0;
head=head+11;
len=len-11;
if (head>=LEN) head=head-LEN;
if (rty[5]=='.') oky=1;
}
if (okx || oky)
{
times=0;
WriteFile(hndCom,sn,1,&nb,NULL);
}
--len;
++i;
if (i>=LEN) i=0;
}//end while
times=0;
}//rec >0
else
{
++times;
if (times>20000)
{
WriteFile(hndCom,sn,1,&nb,NULL);
times=0;
}
}
}
if (okx)
{
gotoxy(10,10);
printf(rtx);
if (rtx[0]!='-')
{
rtx[0]=' ';
x=atof(rtx);
}
else
{
rtx[0]=' ';
x=atof(rtx)*-1;
}
printf(" X=%7.3f",x);
okx=0;
rtx[0]=0;
}
if (oky)
{
gotoxy(10,12);
printf(rty);
if (rty[0]!='-')
{
rty[0]=' ';
y=atof(rty);
}
else
{
rty[0]=' ';
y=atof(rty)*-1;
}
printf(" Y=%7.3f",y);
oky=0;
rty[0]=0;
}
if (kbhit())
{
loop=false;
}
}
CloseHandle(hndCom);
}
|
|