/* 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; } void main() { int DOdata,TotalChannel,iRet,iSlot,iRET; unsigned int DIdata; unsigned char cTemp[10]; int quit=0; InitLib(); Print("/*******************/\n\r"); Print("/* 87K_DIO demo */\n\r"); Print("/* */\n\r"); Print("/* [%s] */\n\r",_Demo_Date); Print("/*******************/\n\r"); Print("\n"); //Step 1. Open COM Port with DMA function InstallCom_DMA_0(115200L,8,0,0); 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); for(;;) { Print("Please Input DO Data(Hex) = "); LineInput(cTemp,10); sscanf(cTemp,"%X",&DOdata); //Step 2: Write Digital Output value, @AA(Data) iRet = Write87KDIO_DO(iSlot,TotalChannel,DOdata);//Execute DO. if(iRet==NoError) { Print("DO output OK!\n\r"); } else { Print("DO receive response error. Error Code = %d\n\r",iRet); } Delay(10); //Wait for DO status is changed. //If you don't need to confirm the DO is changed correctly, //remove following codes. //Step 3: Read Digital Input value, @AA iRET=Read87KDIO_DI(iSlot,TotalChannel,&DIdata); if(iRET==NoError) { Print("DI data = %02X\n\r",DIdata); } else { Print("DI receive response error. Error Code = %d\n\r",iRet); } Print("Please input 1 to exit program or the other keys to I/O again:"); LineInput(cTemp,10); sscanf(cTemp,"%d",&quit); //Step 3: Close COM Port if (quit==1) { RestoreCom(0); return; } } } //===================================================================== //====== 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; }