/* 87022_26.c :This demo program is for 87022 and 87026 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 configuration ($AA2) Step 3: Write configuration ($AANNTTCCFF) Step 4: Read Analog Input value (#AAN) Step 5: Close COM Port Compiler: BC++ 3.1, Turbo C ++ 1.01(3.01) MSC 6.0, MSVC 1.52. Compile mode: large Project: 87022_26.c ..\Lib\(8000.Lib, 8000E.lib) Hardware: 8000 [21 Jan,2014] by Hans */ #include #include #include"..\..\lib\8000E.h" int Write87022_26_AO(int Slot,int iChannel,float fValue); int Set87022_26_DataFormat(int iSlot,int iDataFormat); int Set87022_26_Config(int iSlot,int iChannel,int iRangeCode,int iSlewRate); int Read87022_26_DataFormat(int iSlot,int *iDefaultDataFormat); int Read87022_26_Config(int iSlot,int iChannel,int *iRangeCode_Original,int *iSlewRate_Original); void OpenCom0(void); void CloseCom0(void); char *_Demo_Date=__DATE__; void main() { int iRet,iSlot,iChannel; int iSlewRate,iSlewRate_Original; int iRangeCode,iRangeCode_Original; int iDataFormat,iDataFormat_Original; unsigned char cTemp[10]; int quit=0; float fValue; InitLib(); Print("/*******************/\n\r"); Print("/* 87022,87026 */\n\r"); Print("/* demo */\n\r"); Print("/* [%s] */\n\r",_Demo_Date); Print("/*******************/\n\r"); Print("\n\r"); //Step 1. Open COM Port OpenCom0(); Print("Please Input Slot Number(0 to 7) ="); LineInput(cTemp,10); sscanf(cTemp,"%d",&iSlot); Print("\n"); Print("******** Type Code*****\n\r"); Print("* (0) 0 ~ 20 mA *\n\r"); Print("* (1) 4 ~ 20 mA *\n\r"); Print("* (2) 0 ~ 10 V *\n\r"); Print("***********************\n\r"); Print("Please input Type Code(0,1,2) = "); LineInput(cTemp,10); sscanf(cTemp,"%d",&iRangeCode); //Slew Rate Control Code: 0x0 ~ 0xA //For Detailed specifications,refer to //CD:Napdos\DCON\IO_Module\hw_dcon_on_87kUnit iSlewRate=0x0; //Dataforamt: 0x0 Engineer Unit format // 0x1 Percent of Span format // 0x2 Hexadecimal format iDataFormat=0x0; //Step 2: Read configuration ($AA2) //To prevent reading EEPROM again. iRet=Read87022_26_DataFormat(iSlot,&iDataFormat_Original); if(iRet==NoError) { if(iDataFormat_Original==iDataFormat) { //The present configuration is the same as the input configuration. //So there is no need to write into the EEPROM. } else { //Step 3: Write configuration ($AANNTTCCFF) Set87022_26_DataFormat(iSlot,iDataFormat);//Default set. } } else Print("Read DataFormat Error!\n\r"); for(;;) { Print("Please Input Channel Number(0 or 1) = "); LineInput(cTemp,10); sscanf(cTemp,"%d",&iChannel); Print("Please Input Value(float) = "); LineInput(cTemp,10); sscanf(cTemp,"%f",&fValue); //Step 4: Write Analog Ibnput value #AAN(data) iRet=Read87022_26_Config(iSlot,iChannel,&iRangeCode_Original,&iSlewRate_Original); if(iRet==NoError) { if(iRangeCode_Original==iRangeCode && iSlewRate_Original==iSlewRate) { //The present configuration is the same as the input configuration. //So there is no need to write into the EEPROM. } else Set87022_26_Config(iSlot,iChannel,iRangeCode,iSlewRate); } else Print("Read Configuration Error!\n\r"); iRet=Write87022_26_AO(iSlot,iChannel,fValue); if(iRet==NoError) { Print("Output success!!\n\r"); } else { Print("AO 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 5: 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; } //======================================================================== //====== The following functions are used to read 87022,87026 modules. === //====== You can copy the following functions to your own program. === //======================================================================== int Read87022_26_DataFormat(int iSlot,int *iDataFormat_Original) { /* DCON protocol to read the AO Configuration Command: $AA2 Response: !AATTCCFF Slot: 0 ~ 7 DataFormat: 00 = Engineer Unit Format 01 = % of FSR Format 10 = 2's Complement Hexdecimal Format 11 = Ohms in Engineer Unit Format For I-87013 Only */ unsigned char InBufCom0[20]; int iRet; ChangeToSlot(iSlot); iRet=SendCmdTo87K("$002"); iRet=ReceiveResponseFrom87K(InBufCom0,100); if(iRet==NoError) { if(InBufCom0[0]== '!') //Valid response first character: ! { *iDataFormat_Original=(ascii_to_hex(InBufCom0[8]))&0x3; return NoError; } else return -1; //Response string error. } else return iRet; } int Set87022_26_DataFormat(int iSlot,int iDataFormat) { /* DCON protocol to write the AO Configuration Command: %AANNTTCCFF Response: !AA Slot: 0 ~ 7 DataFormat: 00 = Engineer Unit Format 01 = % of FSR Format 10 = 2's Complement Hexdecimal Format 11 = Ohms in Engineer Unit Format For I-87013 Only */ int iRet; unsigned char InBufCom0[20],OutBufCom0[20]; OutBufCom0[0]=0x25; sprintf(OutBufCom0+1,"00013F0A0%01X",iDataFormat); ChangeToSlot(iSlot); iRet=SendCmdTo87K(OutBufCom0); iRet=ReceiveResponseFrom87K(InBufCom0,100); if(iRet==NoError) { if(InBufCom0[0]== '!') //Valid response first character: ! { return NoError; } else return -1; //Response string error. } else return iRet; } int Read87022_26_Config(int iSlot,int iChannel,int *iRangeCode_Original,int *iSlewRate_Original) { /* DCON protocol to write the AO Configuration Command: $AA9N Response: !AATS Slot: 0 ~ 7 RangeCode: For detailed specifications, refer to CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit CD:8000\Napdos\7000\Manual SlewRate: For detailed specifications, refer to CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit CD:8000\Napdos\7000\Manual */ unsigned char OutBufCom0[20],InBufCom0[20]; int iRet; sprintf(OutBufCom0,"$009%01d",iChannel); ChangeToSlot(iSlot); iRet=SendCmdTo87K(OutBufCom0); iRet=ReceiveResponseFrom87K(InBufCom0,100); if(iRet==NoError) { if(InBufCom0[0]== '!') //Valid response first character: ! { *iRangeCode_Original=(ascii_to_hex(InBufCom0[3])&0x3); *iSlewRate_Original=(ascii_to_hex(InBufCom0[4])&0xF); return NoError; } else return -1; //Response string error. } else return iRet; } int Set87022_26_Config(int iSlot,int iChannel,int iRangeCode,int iSlewRate) { /* DCON protocol to write the AO Configuration Command: #AA9NTS Response: !AA Slot: 0 ~ 7 RangeCode: For detailed specifications, refer to CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit CD:8000\Napdos\7000\Manual SlewRate: For detailed specifications, refer to CD:8000\Napdos\DCON\IO_Module\hw_dcon_on_87kUnit CD:8000\Napdos\7000\Manual */ int iRet; unsigned char OutBufCom0[20]; unsigned char InBufCom0[20]; sprintf(OutBufCom0,"$009%01d%01d%01X",iChannel,iRangeCode,iSlewRate);//$009NT0 ChangeToSlot(iSlot); iRet=SendCmdTo87K(OutBufCom0); iRet=ReceiveResponseFrom87K(InBufCom0,100); if(iRet==NoError) { if(InBufCom0[0]== '!') //Valid response first character: ! { return NoError; } else return -1; //Response string error. } else return iRet; } int Write87022_26_AO(int iSlot,int iChannel,float fValue) { /* DCON protocol to write the AO value Command: #AAN(data) Response: > Slot: 0 ~ 7 Channel: 0 ~ 1 */ int iRet; unsigned char OutBufCom0[20],InBufCom0[20]; sprintf(OutBufCom0,"#00%01d%06.3f",iChannel,fValue); ChangeToSlot(iSlot);//Change to specified slot iRet=SendCmdTo87K(OutBufCom0); iRet=ReceiveResponseFrom87K(InBufCom0,100); if(iRet==NoError) { if(InBufCom0[0] =='>') //Valid response first character: > { return NoError; } else return -1; //Response string error. } else return iRet; }