/* 87k_demo.c :This demo program is for 87K General Function 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). Compiler: BC++ 3.1, Turbo C ++ 1.01(3.01) MSC 6.0, MSVC 1.52. Compile mode: large Project: 87K_demo.c ..\Lib\(8000.Lib, 8000E.lib) Hardware: 8000 [21 Jan,2014] by Hans */ #include"..\..\lib\8000e.h" /* === English comments ========================================== This demo program is for all functions of 87K I/O modules . Users can refer the demo to excute every commands list in 87K I/O module's document. For example: $AAM, #AA, @AA, @AA6... etc. If users wants to use AI,AO,DI,DO,DIO, we have prepared several demos. Uers can copy the code from these demos then paste to their own program. === Chinese comments ========================================== 硂絛ㄒ琌皐癸 87K I/O 家舱场 ㄏノ把σ絛ㄒㄓㄏノ– 87K I/O 家舱㏑もい ㄒ $AAM, #AA, @AA, @AA6... 单单 安ㄏノ璶ㄏノ AI, AO, DI, DO, DIO 硂ㄇи竒非称 计瞷Θ絛ㄒ祘Α盢硂ㄇ祘Α絏钡狡籹祘Αいㄓㄏノ */ void OpenCom0(void); void CloseCom0(void); char *_Demo_Date=__DATE__; void main() { unsigned char InBufCom0[60]; Print("/*******************/\n\r"); Print("/* 87K_ demo */\n\r"); Print("/* */\n\r"); Print("/* [%s] */\n\r",_Demo_Date); Print("/*******************/\n\r"); Print("\n"); InitLib(); //Step 1: Open COM 0 OpenCom0(); //Step 2: Change to 87k Slot 0 ChangeToSlot(0); //Step 3: Send command to 87k module SendCmdTo87K("$00M"); //Send command to COM0(Com port,command,Checksum=Disable) //For example: command="$00M" ===> Gets module name. //For detailed command sets,refer to //CD:\DCON\IO_Module\hw_dcon_on_87kUnit //Step 4: Receive data from 87k module ReceiveResponseFrom87K(InBufCom0,100); //InBufCom0: Pointer of input buffer to receive response // from the 87K module, //Timeout=100ms, //Step 5: Print module name string Print("InBufCom0 = %s",InBufCom0); //Step 6: Close COM Port CloseCom0(); } // 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; }