/* Demo for multi serial I/O I-8144W. with XS_09300.LIB preparation for this demo: 1.iP-8441 or iP-8841 or iP-8411 or iP-8811 2.i-8144W or i-8142W insert at slot 0 and use its RS-485 port 0 3.i-7K or i-87K I/O with Net Address =1 , Baud rate = 115200, Checksum = disable 4.for i-8144W, there are four RS-485 ports, we define port 0 of i-8144W as com port 10, port 1 as com port 11 and so on. 5.we have to declare a callback function and a function pointer for each port of i-8144W. */ #include #include //#include"..\lib\7188e.h" #include"..\lib\8000a.h" #include"..\lib\tcpip32.h" #include"..\lib\MFW.H" // Step 0: // declaration for test conditions int multicom_slot =0 ; // this demo use I-8144 (RS-485) as multi serial port I/O at slot 0 void Com10_CallBack(pCOMPORT com, int mode); // callback function that prepared for COMPORT struct int ReadCom_10(unsigned char *buf,int n); // function pointer for COMPORT struct void DoCom10(int size, unsigned char buf[]); // forward function when receive end char of RS-485 int canSend10 =1; // trigger flag of sending command action // Step 1: // to define the comport struct #define BUF_SIZE 256 char Buf[BUF_SIZE]; // we have to declare one COMPORT struct for each comport COMPORT MyCom10={ 10, //int Port; // 0:COM0, 1:COM1,... 10 Buf, //char *Buf; BUF_SIZE, //unsigned Bufsize; '\r', //char EndChar; 150, //unsigned TriggerLevel; 25, //long Timeout; 0, //long StartTimeTicks; 0, //long LastTimeTicks; 0, //long MasterTimeout; 0, //long MasterStartTimeTicks; 0, //unsigned Size; ReadCom_10, //int (*ReadComn)(unsigned char *buf,int n); Com10_CallBack, //MyCom_CallBack, //void (*CallbackFun)(pCOMPORT, int mode); 0, //pCOMPORT next; }; // Step 2: // assign the function point for comport struct int ReadCom_10(unsigned char *buf,int n) { return ReadCom8000nBytes( multicom_slot, 0,buf, n); // for I-8144W port 0 } /* int ReadCom_11(unsigned char *buf,int n) { return ReadCom8000nBytes( multicom_slot, 1,buf, n); // for I-8144W port 1 } */ void XS_UserInit(int argc,char *argv[]) { void UserLoop(void); char version[20]; InitLib(); InstallCom1(115200, 8,0,1); XS_GetVersion(version); printCom1("[X-Server library]: version=%s ",version); XS_GetLibDate(version); printCom1("date=%s\r\n",version); GetTcpipLibDate(version); printCom1("Tcpip library version:%X, Library Date is %s\r\n",GetTcpipLibVer(),version); /* for xs_09001.lib ~ xs_09003.lib NEED NOT the next 4 lines. */ XS_AddSystemLoopFun(UserLoop); //InstallCom1(115200, 8,0,1); InstallCom8000(multicom_slot); _SetBaudrate(multicom_slot, 0, 115200L); _SetDataFormat(multicom_slot, 0, 8, 0, 1); MyCom10.ReadComn=ReadCom_10; //COM0_DMA XS_AddComPort(&MyCom10); } /************************************************************************/ // to free the comport when exit program void XS_UserEnd(void) { XS_RemoveComPort(&MyCom10); RestoreCom8000(multicom_slot); RestoreCom1(); } /************************************************************************/ void UserLoop(void) { int data; char cmd[32]; if(canSend10==1) { ClearCom8000(multicom_slot, 0); sprintf(cmd,"%s","$00M"); printCom1("Send ==> %s \n",cmd); ToCom8000Str(multicom_slot, 0 , cmd); ToCom8000Str(multicom_slot, 0 , "\r"); canSend10=0; } // when hit any key except 'q' or 'Q' to send command if(IsCom1()) { data=ReadCom1(); if(data=='q' || data=='Q') { QuitMain=1; } else { canSend10 =1; } } } /************************************************************************/ /* XS 0.9.200 or later must add the function main() */ void main(int argc,char *argv[]) { XS_main(argc,argv); /* call the XS library main function. */ } // this callback function will be called when XS frame work begins sending data void Com10_CallBack(pCOMPORT com, int mode) { int i; char OutStr[256]; //printCom_1("COM1 {%d}",mode); switch(mode){ case 0: // does not receive any char after Timeout break; case 1: // receive EndChar com->Size--; com->Buf[com->Size]=0; // remove CR // DoCom10(com->Size,com->Buf); com->Buf[0]=0; break; case 2: // when Buf size reach TriggerLevel case 3: // buffer full break; default: //printCom_1("{?%d}",mode); // break; } com->Size=0; //reset buff size } void DoCom10(int size, unsigned char buf[]) { printCom1("Response ==> [%d]%s\r\n",size,buf); }