📄 ppi_main.cpp
字号:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#include "ctype.h"
#include "PPI.H"
#define PORT 0x3F8
extern unsigned char PPI_Protocol(unsigned char NO,V_M Cell,Param ParaM,long int *Value);
void Initial_comport(int port);
unsigned char Num_Check(unsigned char *num);
unsigned char Cmd_Legal(unsigned char *);
unsigned char ReadByte(unsigned char *);
unsigned char SendByte(unsigned char );
unsigned char SendStr(unsigned char *mess,int n);
unsigned char ReceiveStr(unsigned char *buff,int n);
V_M Cell_To_Data(unsigned char *);
unsigned short int Dot_Position(unsigned char *);
void main(void)
{
unsigned char Cell[6],input[2],ch;
int NO;
long int value;
unsigned char If_Exit=FALSE,Param_In_Error=TRUE,First_In=TRUE,Operate_Error=FALSE;
unsigned char Value_Error=FALSE,M_Operation_Error=FALSE,Communication_Error=FALSE;
V_M cell;
Param ParaM;
Operation operation=None;
clrscr();
Initial_comport(PORT);
while(1) /* Station NO input */
{
printf("Please the NO. of to communicate PLC station:");
gets(input);
NO=atoi(input);
if((NO<=255)&&(NO>=0)) break;
else
printf("\nThe NO.%d is a illegal Station NO.! Please,Repeat!\n",NO);
}
do /* start loop 1 */
{
/* start first run or modify parameter */
{
if(!First_In) getch();
printf("\nWhat will you do?\n \
Press 'Q' or 'q' to quit.\n \
Press 'O' or 'o' to fetch other data.\n \
Press any other letter to continue!\n");
printf("Which will you choose:");
gets(input);
ch=*input;
switch(ch) /* start to choose what will do */
{
case 'q':
case 'Q':
If_Exit=TRUE;
break; //quit
case 'o':
case 'O':
{
Param_In_Error=TRUE;
while(Param_In_Error) /* start input parameter */
{
printf("\nInput to be Read/Write data cell:");
gets(Cell);
printf("\n");
if(!Cmd_Legal(Cell))
{
Param_In_Error=TRUE;
printf("\nThe expression of data cell is illegal!\nPlease input again!\n");
getch();
continue;
}
do /* start input the parameters of detailed operation. */
{
printf("\nTo Read/Write?\nPress R/r to Read ,Press W/w to Write:");
gets(input);
ch=*input;
switch(ch) /* start input check */
{
case 'R':
case 'r':
{
operation=Read;
Operate_Error=FALSE;
Param_In_Error=FALSE;
{
switch(*Cell)
{
case 'v':
case 'V':
ParaM.Data_Type=Vcell;
{
switch(*(Cell+1))
{
case 'b':
case 'B':
{
ParaM.Byte_Num=1;
break;
}
case 'w':
case 'W':
{
ParaM.Byte_Num=2;
break;
}
case 'd':
case 'D':
{
ParaM.Byte_Num=4;
break;
}
}
break;
}
case 'm':
case 'M':
{
ParaM.Data_Type=Mcell;
switch(*(Cell+1))
{
case 'i':
case 'I': ParaM.Byte_Num=0; break;
case 'y':
case 'Y': ParaM.Byte_Num=1; break;
default : break; }
}
break;
}
}
break;
}
case 'W':
case 'w':
{
Operate_Error=FALSE;
Param_In_Error=FALSE;
switch(*Cell) /* start identify the type of cell */
{
case 'v':
case 'V':
{
ParaM.Data_Type=Vcell;
do //start check the value
{
operation=Write;
printf("\nPlease input the value(by decimal) to the V/v cell:");
scanf("%ld",&value);
switch(*(Cell+1))
{
case 'B':
case 'b':
if((value<0)||(value>2e8-1))
{
printf("\nerror!\n");
getch();
}
else Value_Error=FALSE;
ParaM.Byte_Num=1;
break;
case 'W':
case 'w':
if((value<0)||(value>2e16-1))
{
printf("\nerror!\n");
getch();
}
else Value_Error=FALSE;
ParaM.Byte_Num=2;
break;
case 'D':
case 'd':
if((value<0)||(value>2e32-1))
{
printf("\nerror!\n");
getch();
}
else Value_Error=FALSE;
ParaM.Byte_Num=4;
break;
default :
{
printf("\nerror!\n");
getch();
Value_Error=TRUE;
break;
}
}
}while(Value_Error); //end check the value
break;
}
case 'm':
case 'M':
{
printf("\nChoose the opcode of M cell.\n\
0:WriteByte 1:SetOn 2:ClrOff 3:MomentOn 4:Invert\n");
ParaM.Data_Type=Mcell;
do /* start M Operation Select */
{
printf(" which operation do you choose:\n");
gets(input);
ch=*input;
switch(ch) /* start M Operation judge */
{
case '0':
{
while(1)
{
printf("\nPlease input the value to the M_Byte cell:");
scanf("%d",&value);
if(value>=0&&value<=2e8-1)
{
M_Operation_Error=FALSE;
operation=Write;
ParaM.Byte_Num=1;
break;
}
}
break;
}
case '1':
{
M_Operation_Error=FALSE ;
operation=SetOn;
ParaM.Byte_Num=0;
break;
}
case '2':
{
M_Operation_Error=FALSE ;
operation=ClrOff;
ParaM.Byte_Num=0;
break;
}
case '3':
{
M_Operation_Error=FALSE ;
operation=MomentOn;
ParaM.Byte_Num=0;
break;
}
case '4':
{
M_Operation_Error=FALSE ;
operation=Invert;
ParaM.Byte_Num=0;
break;
}
default :
{
M_Operation_Error=TRUE ;
break;
}
} /* end M Operation judge */
}while(M_Operation_Error); /* end M Operation Select */
break;
}
default :
{
printf("\nerror!\n");
getch();
Operate_Error=TRUE;
break;
}
}/* end identify the type of cell */
break;
}
default :
{
printf("\nerror!\n");
getch();
Param_In_Error=TRUE;
Operate_Error=TRUE;
break;
}
} /* end input check */
}while(Operate_Error); /* end input the parameters of detailed operation. */
} /* end input parameter */
break;
}
default: printf("\nOperation Code is error, Repeat!");
break;
} /* end to choose what will do */
First_In=FALSE;
}/* end first run or modify parameter */
if(!If_Exit)
{
while(!kbhit()) /* Communication and Fatal Error Repeat */
{
switch(operation) /* According the configuration to communication */
{
case Read :
{
ParaM.Op_Type=Read;
Communication_Error=PPI_Protocol(NO,Cell_To_Data(Cell),ParaM,&value);
if(!Communication_Error) Communication_Error=FALSE;
break;
}
case Write :
{
ParaM.Op_Type=Write;
Communication_Error=PPI_Protocol(NO,Cell_To_Data(Cell),ParaM,&value);
if(!Communication_Error) Communication_Error=FALSE;
operation=Read;
break;
}
case SetOn :
{
ParaM.Op_Type=SetOn;
Communication_Error=PPI_Protocol(NO,Cell_To_Data(Cell),ParaM,&value);
if(!Communication_Error) Communication_Error=FALSE;
operation=Read;
break;
}
case ClrOff :
{
ParaM.Op_Type=ClrOff;
Communication_Error=PPI_Protocol(NO,Cell_To_Data(Cell),ParaM,&value);
if(!Communication_Error) Communication_Error=FALSE;
operation=Read;
break;
}
case MomentOn :
{
ParaM.Op_Type=MomentOn;
Communication_Error=PPI_Protocol(NO,Cell_To_Data(Cell),ParaM,&value);
if(!Communication_Error) Communication_Error=FALSE;
operation=Read;
break;
}
case Invert :
{
ParaM.Op_Type=Invert;
Communication_Error=PPI_Protocol(NO,Cell_To_Data(Cell),ParaM,&value);
if(!Communication_Error) Communication_Error=FALSE;
operation=Read;
break;
}
default : First_In=FALSE;
}
if(!Communication_Error)
{
If_Exit=TRUE;
printf("\n Fatal Link Error! \n");
getch();
break;
}
else
{
printf("\n The current operation is success.\n");
if(ParaM.Op_Type==Read)
printf("\n %s: %8lX",Cell,value);
}
} /* Communication and Fatal Error Repeat */
}
}while(!If_Exit); /* end loop 1 */
}
/* The subroutine of reading character. */
unsigned char ReadByte(unsigned char *ch)
{
long int time_limit;
time_limit=500000;
while (((inportb(PORT+5)) & 0x01) !=0x01)
{
time_limit--;
if (time_limit==0)
{
printf("\nReceive timeout!\n");
return FALSE;
}
}
*ch=inportb(PORT);
return TRUE;
}
/* The subroutine of writing character. */
unsigned char SendByte(unsigned char sdata)
{
long int time_limit;
time_limit=500000;
while ((inportb(PORT+5) & 0xf0) != 0x60)
{
time_limit--;
if (time_limit==0)
{
printf("\ntransmit timeout!\n");
return FALSE;
}
}
outportb(PORT,sdata);
return TRUE;
}
/* Initial serial port */
void Initial_comport(int port)
{
char i;
/* baud rate 9600 */
outportb(port+3,0x80);
outportb(port,0x0c);
outportb(port+1,0);
/*8bit 1stop no even */
outportb(port+3 ,0x1B);
outportb(port+1,0x00);
/* i=inportb(port+5) & 0xfe; */
/* outportb(port+5,i); */
}
/* The subroutine of transmiting string */
unsigned char SendStr(unsigned char *mess,int n)
{
unsigned char flag;
for (;n; n--)
{
flag=SendByte(*mess++);
if(!flag)
return FALSE;
}
return TRUE;
}
/* The subroutine of receiving string */
unsigned char ReceiveStr(unsigned char *buff,int n)
{
unsigned char *p,flag;
p=buff;
for(;n;n--)
{
flag=ReadByte(p++);
if(!flag)
return FALSE;
}
return TRUE;
}
unsigned char Cmd_Legal(unsigned char *CMD)
{
unsigned char *p;
p=CMD;
switch(*p)
{
case 'v':
case 'V':
{
switch(*(p+1))
{
case 'b':
case 'B':
case 'w':
case 'W':
case 'd':
case 'D': if(Num_Check(p+2)) return TRUE;
default : break;
}
break;
}
case 'm':
case 'M': if(Num_Check(p+2)) return TRUE;
default : break;
}
return FALSE;
}
unsigned char Num_Check(unsigned char *num)
{
unsigned short int flag=0;
for(;*num;num++)
{
if(*num=='.')
{
flag++;
num++;
}
if(!isxdigit(*num))
return FALSE;
}
if(flag>1)
return FALSE;
return TRUE;
}
V_M Cell_To_Data(unsigned char *Cell)
{
V_M Data;
int i,j;
unsigned short int pos;
unsigned char *M_Byte,*M_Bit;
switch(*Cell)
{
case 'v':
case 'V': Data.v=atoi(Cell+2);break;
case 'm':
case 'M':
{
pos=Dot_Position(Cell);
M_Byte=(unsigned char *)malloc(pos-1);
for(i=2,j=0;i<pos;i++,j++)
*(M_Byte+j)=*(Cell+i);
*(M_Byte+i)='\0';
Data.m.M_byte=atoi(M_Byte);
free(M_Byte);
if(*(Cell+pos))
{
M_Bit=(unsigned char *)malloc(3);
for(i=pos+1,j=0;*(Cell+i);i++,j++)
*(M_Bit+j)=*(Cell+i);
*(M_Bit+j)='\0';
Data.m.M_bit=atoi(M_Bit);
free(M_Bit);
}
else
Data.m.M_bit=0x0;
break;
}
default : Data.v=NULL;
}
return Data;
}
unsigned short int Dot_Position(unsigned char *Cell)
{
unsigned short int i;
for(i=0;*Cell;Cell++,i++)
if(*Cell=='.')
break;
return i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -