/* 7k87k_do.c :Demo program for 7K and 87K DI connected to COM port of 7188/8000. User must use DCON Utility to set Address, Baudrate,CheckSum before running the demo. Compiler: BC++ 3.1, Turbo C ++ 1.01(3.01) (free from http://community.borland.com/museum) MSC 6.0, MSVC 1.52. Compile mode: large Project: 7k87k_do.c ..\Lib\(8000E.Lib,7188XAL.Lib,7188XBL.Lib,7188XL.Lib or 7188EL.Lib) Hardware: 7188/8000 [24 May,2005] by Bill */ #include #include"..\..\lib\7188Xc.h" int Write7K87K_DO(int iComPort,int iAddress,int iTotalChannel,int iCheckSum,int iTimeout,unsigned long iValue); int Read7K87K_DO(int iComPort,int iAddress,int iTotalChannel,int iCheckSum,int iTimeout,unsigned long *Value); void main() { int iRet,iAddress,iTotalChannel,iComPort,iCheckSum,iTimeout; int iValue; unsigned char C_Address[10],C_TotalChannel[10],C_ComPort[10],C_CheckSum[10]; unsigned char C_DOdata[10],C_Timeout[10],C_Baudrate[20]; unsigned long Rb_value; unsigned long lBaudrate; unsigned long DOdata; InitLib(); Print("/*******************/\n\r"); Print("/* 7K87K_DO demo */\n\r"); Print("/* for Com Port */\n\r"); Print("/* [11 Mar,2005] */\n\r"); Print("/*******************/\n\r"); Print("\n\r"); Print("Please Input Comport Number:"); LineInput(C_ComPort,10); sscanf(C_ComPort,"%d",&iComPort); Print("Please Input Comport'S Baudrate:"); LineInput(C_Baudrate,10); sscanf(C_Baudrate,"%ld",&lBaudrate); Print("Please Select Checksum(0:Disable or 1:Enable):"); LineInput(C_CheckSum,10); sscanf(C_CheckSum,"%d",&iCheckSum); Print("Please Input module's address(0~256) ="); LineInput(C_Address,10); sscanf(C_Address,"%d",&iAddress); Print("Please Input Total DO Channel Number ="); LineInput(C_TotalChannel,10); sscanf(C_TotalChannel,"%d",&iTotalChannel); Print("Please Input Timeout:"); LineInput(C_Timeout,10); sscanf(C_Timeout,"%d",&iTimeout); InstallCom(iComPort,lBaudrate,8,0,1); //Begin to write DO. for(;;) { Print("Please Input DO Data(Hex) = "); LineInput(C_DOdata,10); sscanf(C_DOdata,"%08lX",&DOdata); iRet=Write7K87K_DO(iComPort,iAddress,iTotalChannel,iCheckSum,iTimeout,DOdata); if(iRet==NoError) { Delay(10); /* Wait for DO status is changed. If you don't need to confirm the DO is changed correctly, remove following codes. */ iRet=Read7K87K_DO(iComPort,iAddress,iTotalChannel,iCheckSum,iTimeout,&Rb_value); if(iRet==NoError) { Print("DO output success.The output value is %-8lX \n\r",Rb_value); } else { Print("Reading output value is error\n\r "); RestoreCom(iComPort); } } else Print("DO receive response error. Error Code = %d\n\r",iRet); } RestoreCom(iComPort); } //===================================================================== //====== Following functions are for 87K DO modules === //====== You can copy the functions to your own program. === //===================================================================== int Write7K87K_DO(int iComPort,int iAddress,int iTotalChannel,int iCheckSum,int iTimeout,unsigned long lValue) { /* DCON protocol to write DO value Send command: @AA(Data) valid Response: > iComPort: 1~8 iAddress: 0 ~ 255 iCheckSum: 0=Disable or 1=Enable iTimeout: unit=ms iTotalChannel: 1~32 */ int iRet,i; unsigned char OutBuf[20],InBuf[20]; unsigned long iMask=0,x; //Creat DO mask. For example: if DO channel= 5, the mask is 00011111. for(i=0;i 8) { sprintf(OutBuf,"@%02X%04X",iAddress,(lValue & iMask)); } else { sprintf(OutBuf,"@%02X%08lX",iAddress,(lValue & iMask)); } iRet=SendCmdTo7000(iComPort,OutBuf,iCheckSum); iRet=ReceiveResponseFrom7000_ms(iComPort,InBuf,iTimeout,iCheckSum); if(iRet==NoError) { if(InBuf[0] =='>') //Valid response first character: > { return NoError; } else return -1; //Response string error. } else return iRet; } int Read7K87K_DO(int iComPort,int iAddress,int iTotalChannel,int iCheckSum,int iTimeout,unsigned long *Value) { /* DCON protocol to read DI value Send command: @AA valid Response: >(DI/DO Data) ComPort: 1~8 Address: 0 ~ 255 CheckSum= 0:Disable or 1:Enable TotalChannel: 1~32 */ int iRet; unsigned long ReadBack_Data; unsigned char InBuf[20],OutBuf[10]; sprintf(OutBuf,"@%02X",iAddress); iRet=SendCmdTo7000(iComPort,OutBuf,iCheckSum); iRet=ReceiveResponseFrom7000_ms(iComPort,InBuf,iTimeout,iCheckSum); if(iRet==NoError) { if(InBuf[0] == '>') //Valid response first character: > { if(iTotalChannel<=8) { sscanf(InBuf+1,"%lX",&ReadBack_Data); *Value=ReadBack_Data >> 8; } else { sscanf(InBuf+1,"%lX",&ReadBack_Data); *Value=ReadBack_Data; } return NoError; } else { return -1; //Response string error. } } else return iRet; }