#include #include #include #include #include"8000.h"//I-8000 Series Function Call Definition #define F2 130 /* This demo program is for I-87054 Serial type Module,8 Digital Output & 8 digital Input. Before Run this program,connect the digital output to the digital input */ void x87054s(void); void start(void); unsigned char InBufCom0[120]; unsigned char OutBufCom0[60]; int SlotNo1,DOdata,DIdata,InIdx0,recok; long ttcount; void DoCom0(void) { int data; if(IsCom0())//check COM0 { data=ReadCom0();//read COM0 data switch(data) { case '\r': InBufCom0[InIdx0]=0; InIdx0=0; recok = 1; break; default: InBufCom0[InIdx0++]=data; break; } } } int Write87KCom0DO(int slot,int type,unsigned int dd) { switch(type) { case 0://16 bit sprintf(OutBufCom0,"@00%04X\r",dd);//Transfer command string //for detial please refer to 87K users manual break; case 1://8 bit sprintf(OutBufCom0,"@00%02X\r",dd);//Transfer command string //for detial please refer to 87K users manual break; } //all of the 87k serial module at I-8000 series, The address = 0 ChangeToSlot(slot);//Change to specified slot ToCom0Str(OutBufCom0);//Send command recok=0;ttcount=0; while(!recok)//check the command echo,to make sure the module receive command { DoCom0();//check COM0 input ttcount++; if( ttcount>=65535)//check Time Out { Print("Time Out \n\r"); break; } } if(ttcount<65535) { Print("DO Echo = %s\n\r",InBufCom0);//Show the echo result if(InBufCom0[0] != '>') { Print("Receive Error \n\r"); return 1; } return 0; } else return 1; } int Read87KCom0DI(int slot,int type,int *didata) { sprintf(OutBufCom0,"$006\r");//Transfer command string //for detial please refer to 87K users manual ChangeToSlot(slot);//Change to specified slot ToCom0Str(OutBufCom0);//Send command recok=0;ttcount=0; while(!recok)//check the command/data input echo { DoCom0();//check COM0 input ttcount++; if( ttcount>=65535)//check Time Out { Print("Time Out \n\r"); break; } } if(ttcount<65535) { switch(type) { case 0://16 bit *didata = (ascii_to_hex(InBufCom0[1])<<12)+(ascii_to_hex(InBufCom0[2]) << 8) +(ascii_to_hex(InBufCom0[3])<<4)+ascii_to_hex(InBufCom0[4]); break; case 1://simple 8 bit *didata = (ascii_to_hex(InBufCom0[1])<<4)+ascii_to_hex(InBufCom0[2]); break; default: break; } Print("DI data = %s,%x\n\r",InBufCom0,*didata);//Show the digital input value. return(0); } else return 1;//time out error } void x87054s(void) { int ii; int ret; InIdx0=0; InBufCom0[0]=0; InstallCom0(115200L,8,0);//initialize COM0 Print("Please Input DO Module Slot Number(0 to 7) =");//Specific the digital output slot number of I-87054. SlotNo1 = ascii_to_hex(Getch());//MiniOS7 read the slot input. Print(" %d \n\r",SlotNo1);//Show the user input. Print("Please Input DO Data(Hex) = "); Scanf("%x",&DOdata);//MiniOS7 read the user keyin the digital output value. Print("DO data = %x\n\r",DOdata);//Show the user input. ret = Write87KCom0DO(SlotNo1,1,DOdata);//87054 if(!ret)//87054 { Delay_1(300);//for Isolation Type,Wait the photo coupler ret = Read87KCom0DI(SlotNo1,1,&DIdata); if(!ret) Print("DI data = %X\n\r",DIdata);//Show the digital input value. else Print("DI Read Error,Error Code = %d\n\r",ret);//Show the Error Code. } else Print("DO Send Error,Error Code = %d\n\r",ret);//Show the Error Code. RestoreCom0();//free COM0 start(); } void main() { int key; start(); for(;;) { if(Kbhit()) { key=Getch(); if(key==F2)//Get F1 from Iview x87054s(); } } } void start(void) { Print("This demo program is for I-87054 Serial type Module,8 Digital Output & 8 digital Input. "); Print("Press F2 to start\n\r"); }