Many communication modules made based on sim300 are used in engineering. The following summarizes the AT Instruction Set Application Methods for network communication.
1. preparation steps: test whether the GPRS module and service are easy to use
At // test whether the network connection is complete
At + CCID // check whether a SIM card is installed
At + cgmr // check software version. GPRS function support is available only for Versions later than 5.0.
At + cops? // Carrier Detection:
Response + cops: 0
OK No network found
Response + cops: 0, 0, "Unicom" // China Unicom
OK
Response + cops: 0, 0, "China Mobile" // mobile
OK
At + CSQ // check the signal quality and determine whether to log on to the network. If the signal numbers between 10-31 and 0 are returned, continue,
// If the signal is or 99, the module should be constantly searched for the network.
2. network initialization
At + cgclass = "B"
A-> WCDMA: default factory settings
B-> GSM/GPRS: The at command does not support setting this mode?
CG-> GPRS only: At + CREG? The returned result is not registered, but pppd dialing is supported.
CC-> GSM only: At + CREG? Check the registration, but pppd cannot dial the Internet
At + cgdcont = 1, "ip", "cmnet" // first define the PDP mobile scenario
At + cgatt = 1 // activate PDP. If OK is returned, continue
At + cipcsgp = 1, "cmnet" // set the module connection mode to GPRS, And the access point to "cmnet"
At + cdnsorip = 0 // 0: access through IP Address
// 1: access through Domain Name
At + cdnscfg = "211.136.17.107" // initialize the local DNS. In this example
3. Establish a connection
At + cipstart = "TCP/UDP", 221.216.163.44, 2020
TCP/UDP indicates whether the connection is a TCP connection or a UDP connection.
221.216.163.44 is the port of server IP address 2020
After the connection is successful, the module returns:
OK
Connect OK
4. send information
At + cipsen
After the command is sent, a ">" symbol appears, so that you can enter the information to be sent in the Super Terminal, and then press "CTL + z" to send the message, sends the data to the server of the specified IP address.
5. Close the connection
At + cipclose // OK is returned only when TCP/UDP is in the connect OK status; otherwise, error is returned.
At + cipshut // disable mobile scenarios
6. Application Example:
Transmit data to remote server over TCP
The server is a PC connected to the Internet, and a receiving software can be run on the PC. This is only applicable to the test module. In fact, you need to develop a socket receiving program at the end of development, note that the IP address of the PC must be a public IP address, or the client cannot find it.
1) access to shuimu Tsinghua BBS:
At + cipstart = "TCP", "166.111.8.238", "23" // telnet server of Tsinghua University
2) Build a TCP Server:
Run the following server program on a computer with a public IP Address:
// ================================================ ==========================================/// File Name: tcp_echo_srv.c // Function Description: TCP echo server // maintenance record: 2011-8-15 V1.0 // ======================================== ======================================#include <stdio. h> # include <stdlib. h> # include <string. h> // bzero # include <unistd. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> // inet_ntop // ====================================== ============ ============================/// Syntax format: void main (void) // implementation function: main function, create a TCP echo server // entry parameter: NONE // exit parameter: none ====================================== int main (INT argc, char * argv []) {char recvbuf [2048]; // receipt buffer int sockfd; // socket struct sockaddr_in servaddr; // The server address structure unsigned short Port = 8000; // listener port if (argc> 1) // The parameter receiving port {Port = atoi (argv [1]);} printf ("TCP server started at Port % d! \ N ", Port); // setp 1: Create TCP socket sockfd = socket (af_inet, sock_stream, 0); If (sockfd <0) {perror ("invalid socket"); exit (1) ;}// setp 2: bind the Socket socket to the port number and the local IP address to bzero (& servaddr, sizeof (servaddr )); // initialize the server address servaddr. sin_family = af_inet; // use the IPv4 protocol servaddr. sin_port = htons (port); // specify the port number servaddr. sin_addr.s_addr = htonl (inaddr_any); // bind the local IP address printf ("binding server to port % d \ n", Port); printf ("servaddr. sin _ ADDR. s_addr = % d \ n ", servaddr. sin_addr.s_addr); If (BIND (sockfd, (struct sockaddr *) & servaddr, sizeof (struct sockaddr ))! = 0) {close (sockfd); perror ("binding Err! "); Exit (1);} // setp3 sets the socket to the listening mode if (Listen (sockfd, 1 )! = 0) {close (sockfd); perror ("Listen Err! "); Exit (1);} printf (" Waiting client... \ n "); While (1) {char cliip [inet_addrstrlen]; // used to save the Client IP address size_t recvlen; struct sockaddr_in cliaddr; // Save the client address size_t cliaddrlen = sizeof (cliaddr); // Initialization is required !!! // Setp4: blocks the wait client connection int connfd = accept (sockfd, (struct sockaddr *) & cliaddr, & cliaddrlen ); // obtain a established connection if (connfd <0) {close (sockfd); perror ("Accept Err! "); Exit (1);} inet_ntop (af_inet, & cliaddr. sin_addr.s_addr, cliip, inet_addrstrlen); printf ("Client IP = % s \ n", cliip); // setp5: read the data sent by the client and send it back to the client while (recvlen = read (connfd, recvbuf, 2048)> 0) {write (connfd, recvbuf, recvlen );} close (connfd); printf ("client closed! \ N ");} Close (sockfd); Return 0 ;}
Use the following method to connect:
At + cipstart = "TCP", "124. *. *. 139", "8000"
Run the following command to send data:
At + cipsend
The server will send the data back to you.