/* 87K_DIO.c :The demo program is for 87K DIO Module in Com0. It can communicate with CM0 via DMA. If without DMA, it may caused communication lost data in send/receive long data. User needs to know: 1. The communication protocol for COM0 is always 115200,N,8,1 and Checksum disable. User can't modify it. 2. User need to know the module's location on Slot (Slot 0 ~ 7). Step on this Demo: Step 1: Open COM Port Step 2: Write Digital Output value, @AA(Data) Step 3: Read Digital Input value, @AA Step 4: Close COM Port Compiler: BC++ 3.1, Turbo C ++ 1.01(3.01) MSC 6.0, MSVC 1.52. Compile mode: large Project: 87K_DIO.c ..\Lib\(8000.Lib, 8000E.lib) Hardware: 8000 [21 Jan,2014] by Hans */ #include #include"..\..\lib\8000a.h" int Write87KDIO_DO(int Slot,int TotalChannel,int DOdata); int Read87KDIO_DI(int Slot,int TotalChannel,unsigned int *DIdata); char *_Demo_Date=__DATE__; int Write87K_87024AO(int iSlot,int iChannel,float fValue) { /* DCON protocol to write the 87024 value Command: #AAN(data) Response: > Slot: 0 ~ 7 Channel: 0 ~ 1 */ int iRet; unsigned char OutBufCom0[20],InBufCom0[20]; OutBufCom0[0]=0x23;//0x23 = # sprintf(OutBufCom0+1,"00%01d%+06.3f",iChannel,fValue); ChangeToSlot(iSlot);//Change to specified slot iRet=SendCmdTo7000(0,OutBufCom0,0); //Com port=0, CheckSum=0(0:disable 1:Enable) iRet=ReceiveResponseFrom7000_ms(0,InBufCom0,300,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable) if(iRet==NoError) { if(InBufCom0[0] =='>') //Valid response first character: > { return NoError; } else return -1; //Response string error. } else return iRet; } int Read87K_AI(int iSlot,int iChannel,float *fValue1) { /* DCON protocol to read the AI value Command: #AAN Response: >(data) Slot: 0 ~ 7 Channel: 0 ~ 8 */ unsigned char OutBufCom01[20],InBufCom01[20]; int iRet; OutBufCom01[0]=0x23; sprintf(OutBufCom01+1,"00%01X",iChannel); ChangeToSlot(iSlot); iRet=SendCmdTo7000(0,OutBufCom01,0); //Com port=0, CheckSum=0 (0:disable 1:Enable) //Delay(1000); iRet=ReceiveResponseFrom7000_ms(0,InBufCom01,100,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable) //Print ("OutBufCom01 = %s ",OutBufCom0); //Print ("InBufCom01 = %s \n\r",InBufCom0); if(iRet==NoError) { if(InBufCom01[0]== '>') { *fValue1=atof(InBufCom01+1); //Delay(1000); return NoError; } else return -999;//Response string error. } else return iRet; } int Read87K_AI_Hex(int iSlot,int iChannel,int *iValue) { /* DCON protocol to read the AI value Command: #AAN Response: >(data) Slot: 0 ~ 7 Channel: 0 ~ 8 */ unsigned char OutBufCom0[20],InBufCom0[20]; int iRet,iHex; OutBufCom0[0]=0x23; sprintf(OutBufCom0+1,"00%01d",iChannel); ChangeToSlot(iSlot); iRet=SendCmdTo7000(0,OutBufCom0,0); //Com port=0, CheckSum=0 (0:disable 1:Enable) iRet=ReceiveResponseFrom7000_ms(0,InBufCom0,100,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable) Print ("OutBufCom0 = %s ",OutBufCom0); Print ("InBufCom0 = %s \n\r",InBufCom0); if(iRet==NoError) { if(InBufCom0[0]== '>') { sscanf(InBufCom0+1,"%04X",&iHex); *iValue=iHex; return NoError; } else return -1;//Response string error. } else return iRet; } void main() { int DOdata,TotalChannel,iRet,iSlot,iRET; unsigned int DIdata; unsigned char cTemp[10]; unsigned long loop=0; float fValue,faiValue; int iChannel=0; int iyear,imonth,iday,ihour,imin,isec; int oyear,omonth,oday,ohour,omin,osec; int lastday=0; int iValue; InitLib(); InstallCom_0(115200L,8,0,0); Print("/*******************/\n\r"); Print("/* 87K_DIO demo */\n\r"); Print("/* */\n\r"); Print("/* [8 Nov,2004] */\n\r"); Print("/*******************/\n\r"); Print("\n"); /* Print("Please Input Slot Number(0~3 or 0~7) ="); LineInput(cTemp,10); sscanf(cTemp,"%d",&iSlot); Print("Please Input Total DO Channel Number(4,8,16) = "); LineInput(cTemp,10); sscanf(cTemp,"%d",&TotalChannel); */ iSlot=0; TotalChannel=16; DOdata=0; for(;;) { GetDate(&oyear,&omonth,&oday); if (lastday!=oday) Print("\n\n\r Date: %d/%d/%d \n\r",omonth,oday,oyear); lastday=oday; Print ("\r Loop=%lu ",loop); GetTime(&ihour,&imin,&isec); Print("Time: %d:%d:%d ",ihour,imin,isec); /* Print("Please Input DO Data(Hex) = "); LineInput(cTemp,10); sscanf(cTemp,"%X",&DOdata); */ iRet = Write87KDIO_DO(iSlot+3,TotalChannel,DOdata);//Execute DO. if(iRet==NoError) { //Print("DO output OK! "); } else { Print("DO receive response error. Error Code = %d\n\r",iRet); } Delay(100); iRET=Read87KDIO_DI(iSlot+2,TotalChannel,&DIdata); if(iRET==NoError) { if (DIdata!=DOdata) Print ("DOdata= %u , DIdata= %u fail\n\r",DOdata,DIdata); else Print ("DOdata= %u , DIdata= %u ok ",DOdata,DIdata); //Print("DI data = %02X\n\r",DIdata); } else { Print("DI receive response error. Error Code = %d\n\r",iRet); } if (++DOdata>=65535) DOdata=0; Delay(100); // iRet=Write87K_87024AO(iSlot+1,iChannel,fValue); if(iRet==NoError) { //Print("AO Output success!!"); } else { Print("AO receive response error. Error Code = %d\n\r",iRet); } Delay(100); //iRet=Read87K_AI_Hex(iSlot,iChannel,&iValue); Print ("iChannel= %d,iSlot=%d",iChannel,iSlot); iRet=Read87K_AI(iSlot,iChannel,&faiValue); if(iRet==NoError) { //Print ("iValue=%4X,faiValue=%6.3f",iValue,faiValue); //faiValue=(float)(iValue*20/32767); //Print ("QiValue=%4X,QfaiValue=%6.3f",iValue,faiValue); if (faiValue==fValue) Print ("AI,CH%d=%6.3f ,AO,CH%d=%6.3f OK",iChannel,faiValue,iChannel,fValue); else Print ("AI,CH%d=%6.3f ,AO,CH%d=%6.3f error\n\r",iChannel,faiValue,iChannel,fValue); //Print("Channel %d= %6.3f\n\r",iChannel,fValue); } else { Print ("AI Ch:%d response error. Error code=%d\n\r",iChannel,iRet); //Print("AI has received a response error. Error Code =%d\n\r",iRet); } if (++iChannel>=8) iChannel=0; if (++fValue>=10.0) fValue=0.9; if (Kbhit()) { RestoreCom0(); return; } loop++; } } //===================================================================== //====== Following functions are used to read/write 87K DIO modules === //====== You can copy the functions to your own program. === //===================================================================== int Write87KDIO_DO(int Slot,int TotalChannel,int DOdata) { /* DCON protocol to write DO value Command: @AA(Data) Response: > Slot: 0 ~ 8 TotalChannel: 0 ~ 32 */ int iRet; unsigned char OutBufCom0[20],InBufCom0[20]; switch(TotalChannel) { case 4://less than 4 bits //Generate command string sprintf(OutBufCom0,"@00%01X",DOdata&0xF); break; case 8://8 bit //Generate command string sprintf(OutBufCom0,"@00%02X",DOdata&0xFF); break; case 16://16 bit //Generate command string sprintf(OutBufCom0,"@00%04X",DOdata&0xFFFF); break; } ChangeToSlot(Slot); iRet=SendCmdTo7000(0,OutBufCom0,0); //Com port=0, CheckSum=0 iRet=ReceiveResponseFrom7000_ms(0,InBufCom0,100,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable) if(iRet==NoError) { if(InBufCom0[0] =='>') { return NoError; } else return -1; // Response string error. } else return iRet; } int Read87KDIO_DI(int Slot,int TotalChannel,unsigned int *DIdata) { /* DCON protocol to read DI value Command: @AA Response: >(DO Data)(DI Data) Slot: 0 ~ 8 TotalChannel: 0 ~ 32 */ int iRet; unsigned char InBufCom0[20]; ChangeToSlot(Slot); iRet=ClearCom0(); iRet=SendCmdTo7000(0,"@00",0); //Com port=0, CheckSum=0 (0:disable 1:Enable) iRet=ReceiveResponseFrom7000_ms(0,InBufCom0,100,0); //Com port=0, TimeOut=100, CheckSum=0 (0:disable 1:Enable) if(iRet==NoError) { if(InBufCom0[0] =='>') { switch(TotalChannel) { case 4: // 4 bit *DIdata=ascii_to_hex(InBufCom0[4])&0xF; return NoError; case 8: // 8 bit *DIdata=((ascii_to_hex(InBufCom0[3])<<4)&0xF0) +(ascii_to_hex(InBufCom0[4])&0xF); return NoError; case 16://16 bit *DIdata=(((unsigned)(ascii_to_hex(InBufCom0[1]))<<12)&0xF000) +((ascii_to_hex(InBufCom0[2])<<8)&0xF00) +((ascii_to_hex(InBufCom0[3])<<4)&0xF0) +(ascii_to_hex(InBufCom0[4])&0xF); return NoError; default: return NoError; } } else return -1; // Response string error. } else return iRet; }