/* UDPDemo.c: UDP client Demo Compiler: BC++ 3.1 Compile mode: large Project: UDPDemo.c G4500.lib GSM.lib OS7_COM.lib Hardware: G-4500 [Feb 18, 2011] by Malo */ #include #include #include #include #include #include "../lib/GSM.h" #include "../lib/G4500.h" #include "../lib/OS7_COM.h" void printPacket(GPRSData gprsData) { int i; static int no = 0; no++; Print("----new GPRS Data (%d)----\r\n", no); Print("IP:Port = %s:%d, from link = %d\r\n", gprsData.IP, gprsData.port, gprsData.link); Print("Data length = %d\r\n", gprsData.dataLen); Print("Data = \r\n"); for(i=0; i>8, libVer&0xff, libDate); //-- print GSM lib. version GM_SYS_GetLibDate(libDate); libVer = GM_SYS_GetLibVersion(); Print("GSM LIB vesion %X.%02X, Date:%s\r\n", libVer>>8, libVer&0xff, libDate); //---- init modem strcpy(sysProfile.PINCode, "0000"); //The pin code of SIM card, ex: "0000" sysProfile.modemPort = 4; //modem port number. G-4500 = 4, uP-5000 = 11 sysProfile.hardware = 1; //hardware type. 1: G-4500, 2: uPAC-5000, 3: iP-8000, 0: GTM-201 if( (Result = GM_SYS_InitModem(sysProfile)) == GM_NOERROR) Print("init_modem success!!\r\n"); else{ Print("init_modem fail!! return value is %d\r\n", Result); return 1; } //--check Could the modem service? while(GM_SYS_CheckModemStatus() != GM_NOERROR) { Print("wait modem register...\r\n"); DelayMs(1000); } Print("modem registered!!\r\n"); while(iAction!=0) { iAction=0; quit=0; Print("\r\n"); Print("1) UDP client demo\r\n"); Print("2) check signal quality\r\n"); Print("3) check registered?\r\n"); Print("\r\n"); Print("0) Quits demo program\r\n"); Print("Choose an option and press [Enter]: "); Scanf("%d", &iAction); Print("\r\n"); switch(iAction) { case 1: //UDP client demo Print("UDP client demo start\r\n"); // set Network profile strcpy(netProfile.APN, "INTERNET"); //APN for network provided by your cellular provider strcpy(netProfile.pw, ""); //username for network provided by your cellular provider strcpy(netProfile.user, ""); //password for network provided by your cellular provider //The most basic task of DNS is to translate hostnames such as www.icpdas.com to IP address such as 96.9.41.131. strcpy(netProfile.DnsServerIP, ""); //empty string = system default value GM_NET_SetNet(netProfile); //set ip, port of server Print("please input server IP:(ex: 74.125.227.48)\r\n"); Scanf("%s", serverIP); Print("please input server Port:(ex: 80)\r\n"); Scanf("%d", &serverPort); //--(1) install link[0], GM_NET_InstallLink(0, 0, serverIP, serverPort) for UDP GM_NET_InstallLink(0, 0, serverIP, serverPort); Print("linking...\r\n"); socket_n = 0; //count for the packets while (1) { // press "ESC" to exit if(Kbhit()) { tmp = Getch(); if( tmp == 27 || tmp == 'q') break; } //--(2) check that could the modem service, if it can't, skip operating the modem below if(GM_SYS_CheckModemStatus() != GM_NOERROR) continue; if(GM_NET_GetLinkStatus(0)!=1) continue; else GM_NET_GetIP(myIP); //--(3) send the data to server, and when LinkStatus[1]=1 switch((netSendStatus=GM_SYS_CheckCmdStatus())) { case GM_READY: Print("sending package[%8ld]..., myIP = %s\r\n", socket_n, myIP); gprsData.link = 0; sprintf(gprsData.data, "-<%8ld>-UDP send test!!", socket_n); gprsData.dataLen = strlen(gprsData.data); if(GM_NET_Send(gprsData.link, gprsData.data, gprsData.dataLen)!=GM_NOERROR) Print("can't send package[%8ld]\r\n"); break; case GM_NOERROR: Print("send success!!\r\n"); socket_n++; break; case GM_BUSY: //sending, and waiting reply break; default: Print("send error, and re-send again, error code=%d\r\n", netSendStatus); break; } //--(4) if any new data packet come in, print it if(GM_NET_GetNewPacket(&gprsData) != NULL) { Print("\n== new data packet come in\r\n"); printPacket(gprsData); } } GM_NET_CloseLink(0);//== Close client link[n], 3G:0~9, 2G:0 GM_NET_CloseNet(); break; case 2://check signal quality Result = GM_SYS_CheckSignal(); Print("-----------------------------------\r\n"); Print("signal value = %d\r\n", Result); Print("signal quality:\r\n"); Print("0 -113 dBm or less\r\n"); Print("1 -111 dBm\r\n"); Print("2~30 -109... -53 dBm\r\n"); Print("31 -51 dBm or greater\r\n"); Print("99 not known or not detectable\r\n"); Print("-----------------------------------\r\n"); break; case 3://check register value Result = GM_SYS_CheckReg(); Print("-----------------------------------\r\n"); Print("register value = %d (0:no register, 1:registered, 2:registering)\r\n", Result); Print("register flag:\r\n"); Print(" 0: not registered\r\n"); Print(" 1: registered, home network\r\n"); Print(" 2: not registered, and searching...\r\n"); Print(" 3: registration denied\r\n"); Print(" 4: unknown\r\n"); Print(" 5: registered, roaming\r\n"); Print("-----------------------------------\r\n"); break; case 0: default: quit=1; break; }//end switch() if(!quit) { Print("Press any key to continue...\r\n"); Getch(); } }//end while(1) //must close before program ending to release you resource GM_SYS_CloseModem(0); //== Close the modem, 0:not turn off modem, 1:turn off modem Print("Please press ENTER to exit...\r\n"); Getch(); return 0; }