/* 87K_DI.c :This demo program is for 87K Digital Input 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: Read Digital Input value, @AA Step 3: Close COM Port Compiler: BC++ 3.1, Turbo C ++ 1.01(3.01) MSC 6.0, MSVC 1.52. Compile mode: large Project: 87K_DI.c ..\Lib\(8000.Lib, 8000E.lib) Hardware: 8000 [21 Jan,2014] by Hans */ #include #include"..\..\lib\8000e.h" int Read87K_DI(int Slot,int TotalChannel,unsigned long *Value); void OpenCom0(void); void CloseCom0(void); char *_Demo_Date=__DATE__; void main() { int iSlot,iRet,TotalChannel; unsigned long iValue; unsigned char cTemp[10]; int quit=0; InitLib(); Print("/*******************/\n\r"); Print("/* 87K_DI demo */\n\r"); Print("/* */\n\r"); Print("/* [%s] */\n\r",_Demo_Date); Print("/*******************/\n\r"); Print("\n"); //Step 1. Open COM Port OpenCom0(); Print("Please Input Slot Number(0~3 or 0~7) ="); LineInput(cTemp,10); sscanf(cTemp,"%d",&iSlot); Print("Please Input Total DI Channel Number(8,16,32) ="); LineInput(cTemp,10); sscanf(cTemp,"%d",&TotalChannel); for(;;) { //Step 2: Read Digital Input value, @AA iRet=Read87K_DI(iSlot,TotalChannel,&iValue); if(iRet==NoError) { Print("DI data = %-08lX\n\r",iValue); } 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) { CloseCom0(); return; } } } // To open COM0 with DMA void OpenCom0(void) { InstallCom_0(115200L, 8, 0,1); //open COM 0, baudrate 115200,N,8,1 OpenCom0UseDMA(); //Open COM 0 with DMA } // To close COM0 with DMA void CloseCom0(void) { CloseCom0UseDMA(); } // Send command to 87k module int SendCmdTo87K(unsigned char *cCmd) { ToComStr_0(cCmd); //Send command string ToCom_0(0x0d); //Send the terminal char 0x0D return NoError; } //------------------------------------------------------------------------------ unsigned char _87K_Response[256]; //------------------------------------------------------------------------------ // Blocked mode to receive data from 87k module int ReceiveResponseFrom87K(unsigned char *cCmd, long lTimeout) { unsigned char c,data; long t; int newcnt,cnt=0; Com0SetInputBuf(_87K_Response,256); //Set a Buffer for receiving data from COM 0 t=GetTimeTicks(); while(1){ newcnt=Com0GetDataSize(); // Get Data size from COM 0 if(newcnt != cnt){ if(_87K_Response[newcnt-1]=='\r'){ //if get the terminal char 0x0D newcnt--; _87K_Response[newcnt]=0; // Add zero data to the end. strcpy(cCmd,_87K_Response); break; } cnt=newcnt; } else { // timeout if time for geteting data is more than lTimeout if(GetTimeTicks()-t > lTimeout) return TimeOut; } } return NoError; } //===================================================================== //====== Following functions are used to read 87K DI modules === //====== You can copy the functions to your own program. === //===================================================================== int Read87K_DI(int Slot,int TotalChannel,unsigned long *Value) { /* 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); SendCmdTo87K("@00"); iRet=ReceiveResponseFrom87K(InBufCom0,100); if(iRet==NoError) { if(InBufCom0[0] == '>') { switch(TotalChannel) { case 8: // 8 bit *Value=((ascii_to_hex(InBufCom0[1])<<4)&0xF0) +(ascii_to_hex(InBufCom0[2])&0xF); return NoError; case 16://16 bit *Value=(((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; case 32: *Value=(((unsigned long)(ascii_to_hex(InBufCom0[1]))<<28)&0xF0000000) +(((unsigned long)(ascii_to_hex(InBufCom0[2]))<<24)&0xF000000) +(((unsigned long)(ascii_to_hex(InBufCom0[3]))<<20)&0xF00000) +(((unsigned long)(ascii_to_hex(InBufCom0[4]))<<16)&0xF0000) +((unsigned long)(ascii_to_hex(InBufCom0[5])<<12)&0xF000) +((unsigned long)(ascii_to_hex(InBufCom0[6])<<8)&0xF00) +((unsigned long)(ascii_to_hex(InBufCom0[7])<<4)&0xF0) +((unsigned long)(ascii_to_hex(InBufCom0[8]))&0xF); return NoError; default: return NoError; } } else { return -1; //Response string error. } } else return iRet; }