/* XW511.c: Demo program for XW511i Compiler: BC++ 3.1 Turbo C++ 1.01 (3.01) Compile mode: large Project: XW511.c ..\..\lib\upac5000.lib Hardware: uPAC-5000 + XW511i [Sep 11, 2017] */ #include #include #include "..\..\lib\uPAC5000.h" void main(void) { char buf[128]; unsigned long mytimeticks; int i, iOption, iQuit=0; char getChar; InitLib(); // install the driver for the COM port for COM3 to COM6 in the interrupt vector, // which means that all COM ports use the interrupt mechanism to read and write data InstallCom(3, 115200l, 8, 0, 1); InstallCom(4, 115200l, 8, 0, 1); InstallCom(5, 115200l, 8, 0, 1); InstallCom(6, 115200l, 8, 0, 1); Puts("\n"); Puts("*******************************************\n"); Puts("| Demo program for uPAC-5000 + XW511i |\n"); Puts("| |\n"); Puts("| [Sep 11, 2017] |\n"); Puts("*******************************************\n"); while(!iQuit) { Puts("\n"); Puts("1) COM3 <-> COM4\n"); Puts("2) COM5 <-> COM6\n"); Puts("\n"); Puts("0) Quit demo program\n\n"); Puts("Choose an option and press [Enter]: "); Scanf("%d", &iOption); Puts("\n"); switch(iOption) { case 1: ClearCom(4); // clear all data in input buffer of COM4 ToComBufn(3, "hello world", 11); // send n bytes of data via COM3 memset(buf, 0, sizeof(buf)); mytimeticks = GetTimeTicks(); for(i=0; i < 11; ) { if(IsCom(4)) { // check whether there is any data in the input buffer of COM4 buf[i++] = ReadCom(4); // read one byte of data from the input buffer of COM4 } if(GetTimeTicks() - mytimeticks > 20) { i=0; break; } } if(i==0) { Print("timeout\n"); } else { buf[i] = '\0'; // add a null character at the end of a string Print("Received \"%s\" via COM4\n", buf); } break; case 2: ClearCom(6); // clear all data in input buffer of COM6 ToComStr(5, "hello world\r"); // send a string (ending with '\0') via COM5 memset(buf, 0, sizeof(buf)); i=0; getChar='\0'; mytimeticks = GetTimeTicks(); do { if(IsCom(6)) { getChar = ReadCom(6); buf[i++] = getChar; } if(GetTimeTicks() - mytimeticks > 20) { i=0; break; } } while(getChar != '\r'); if(i==0) { Print("timeout\n"); } else { buf[i - 1] = '\0'; // replace the character '\r' with '\0' Print("Received \"%s\" via COM6\n", buf); } break; case 0: iQuit=1; // If the program calls InstallCom, // the RestoreCom must be called to restore the COM Port setting before exiting the program RestoreCom(3); RestoreCom(4); RestoreCom(5); RestoreCom(6); break; default: break; } if(!iQuit) { Puts("\r\nPress any key to continue...\n"); Getch(); } } }