GPRS is the general Packet Radio Service (Universal Packet Wireless Services technology) abbreviation, it is GSM mobile phone users can be a mobile data service. GPRS and the previous continuous channel transmission in different ways, is a packet (Packet)-type transmission. We have developed the YFSoft.Hardware.GPRS library based on the. NET Micro Framework 4.1 platform, through which the user simply invokes the relevant interface and can realize GPRS communication.
Our choice of hardware is based on the sim300_v7.02 module of the Development Board, through the serial port and the. NET MF Development Board to connect (the following figure).
Because the. NET MICRO Framework Development Board provides the serial port as the parent header, so and the GPRS Development Board connection needs to make a special connection line, simple period, I used a single row of needles to do the three-line cross connector (but preferably with a special connector, the following connector is not very close, Prone to communication failures), as shown below.
After the hardware platform is built, we write GPRS TCP Communication program (UDP and TCP are similar, here is no longer an example).
The core code is as follows:
public static void Main ()
{
GPRS = new GPRS ("COM2", 115200, GPRS). GPRSTYPE.SIM300_V7_02);
Gprs. DataReceived + = new Gprsdatareceivedeventhandler (gprs_datareceived);
if (Ret=gprs. Open ())!= 0)
{
Debug.Print ("Open Failed:" + ret.) ToString ());
Gprs. Close ();
Return
}
if (ret = GPRS. SETAPN ("CMNET"))!= 0)
{
Debug.Print ("Config Failed:" + ret.) ToString ());
Gprs. Close ();
Return
}
Debug.Print ("Signal:" + GPRS.) Signal.tostring ());
if (!gprs. Connect (GPRS. Gprsmode.tcp, "221.217.???.???", 8080, 200)
{
Debug.Print ("Connect failed!");
Gprs. Close ();
Return
}
Debug.Print ("IP:" + GPRS.) Localip);
Debug.Print (GPRS. Statestring (GPRS. State));
while (true)
{
Thread.Sleep (1000);
}
Gprs. Disconnect ();
Gprs. Close ();
}
static void Gprs_datareceived (object sender)
{
GPRS GPRS = (GPRS) sender;
if (GPRS). Available > 0)
{
Debug.Print (GPRS. Receive ());
}
}
static void Key_click (Button button, keypad.buttonstate State, DateTime)
{
string keyname = "";
if (state = = KeyPad.ButtonState.Up && GPRS!= NULL)
{
if (!gprs. Send ("Hello MF-" + keyname, 1000))
{
Debug.Print ("Send failed!");
}
}
}
Before starting the test, we need to run a TCP server program (which can be downloaded from the connection provided at the end of the article), as shown in the following figure:
The results of the. NET Micro Framework code running in VS2010 are as follows:
We send data to the server according to the buttons on the Development Board, or send data to the Development Board through the TCPServer program, and the effect is as shown in the two figure.
In order to facilitate users ' in-depth understanding of YFSoft.Hardware.GPRS library, we have written a detailed library function manual, the main contents are as follows: 1 "Properties" 1.1 overtime
Properties: reading and writing
Description: timeout, which refers to the response time of the device when sending an at command, which is used in conjunction with an at instruction that returns OK or error in response. The time unit is milliseconds, and the timeout wait time is likely to be greater than the value when actually used. 1.2 Signal
properties: read only
Description: current cell phone signal strength.
Range:
0-113 DBm or less
1-111 dBm
2...30-109 ... -53 dBm
31-51 DBm or greater
Not known or not detectable 1.3 State
properties: read only
Description: GPRS status.
0-none,
1-ip_initial,
2-ip_start,
3-ip_config,
4-ip_ind,
5-ip_gprsact,
6-ip_status,
7-tcp_connecting,
8-udp_connecting,
9-ip_close,
10-CONNECT_OK, 1.4 localip
properties: read only
Description: local IP address. 1.5 Connected
properties: read only
Description: TCP connection status. 1.6 Available
properties: read only
Description: The length of the data received by the current receive buffer. 2 "Events" 2.1 datareceived
statement: Gprsdatareceivedeventhandler datareceived
parameters:Instance of Object Sender–gprs class
Description: TCP or UDP has received data 2.2 messagereceived
statement: Smsmessagereceivedeventhandler messagereceived
parameters:Instance of Object Sender–gprs class
Note: SMS receive notification 3 "method" 3.1 GPRS
statement: GPRS (string portname, int baudrate,gprstype type)
parameters:portname– serial name, such as COM1
baudrate– baud rate, default 115200
TYPE–GPRS module type, currently supports SIM300_V7_02 and its compatible modules.
back: none
Description: The constructor of the GPRS class. 3.2 Setdebugmode
Disclaimer:void Setdebugmode (bool on)
parameters:on–true turn on debug mode False turns debug mode off
back: none
Note: set debug mode, the content of the at instruction in the communication process will be output through the Debug.Print function. 3.3 Open
declaration:int Open ()
Parameters: None
return: operation return value
0– success
The serial port failed to open
2– device is not online (at)
3– set echo failure (ATE1)
4– Set IP header display failed (at+ciphead=1)
5– settings Return Information format failed (ATV1)
6– failed to set message format (at+cmgf=1)
7– setting output error message mode failure (at+cmee=0)
8– Detect sim Failure (At+cpin?)
9– registration to the mobile operator's network failed (At+creg? )
Description: Open the serial port and make the necessary configuration. 3.4 Close
declaration:Void Close ()
Parameters: None
back: none
Description: close the serial port. 3.5 SETAPN
declaration:int SETAPN (string apn)
parameters:The name of the apn– access point, such as Cmnet
return: operation return value
0– success
Registered GPRS service failed
2– Configuration access Point failed
3– Set Connection mode failed, connection is server or domain 0 is IP address
4– Set Application Mode failed, if 1 the AT command sent is also the data requires a service-side lift, if 0 at instruction will be responsive
Description: Configure GPRS necessary parameters. 3.6 statestring
statement:string statestring (gprsstate state)
parameters:State–gprs State
return: A string form of GPRS status
"IP INITIAL",
"IP START",
"IP CONFIG",
"IP IND",
"IP Gprsact",
"IP STATUS",
"TCP connecting",
"UDP connecting",
"IP Close",
"CONNECT OK"
Note: returns the form of a string of GPRS status. The. NET Micro framework differs from the. NET Framework, and ToString does not return the name of the enum, but the character of the corresponding number. 3.7 Connect
Disclaimer:bool Connect (gprsmode mode,string ip,int port,int Overtime)
parameters:mode–tcp or UDP
ip– Server IP Address
port– Port
overtime– and overtime are different, which is the timeout time to wait for a connection to succeed.
return: connection state BOOL succeeded false failed
Note that the communication status is also successful when the current state is ip_ind.
Description: connect to a TCP or UDP server. 3.8 Disconnect
Disclaimer:void Disconnect ()
Parameters: None
back: none
Description: Disconnect the GPRS connection. 3.9 Send
statement:bool Send (string Buffer,int Overtime)
parameters:buffer– the string to send
overtime– and overtime are different, which is the timeout time to wait for the data to be sent successfully.
return: Send status bool succeeded false failed
Description: sends data to a TCP or UDP server. Only character forwarding is currently supported. 3.10 Receive
declaration:String Receive ()
Parameters: None
returns: returns the data sent back by a TCP or UDP server (all strings that return the current buffer at a maximum of 1024).
Description: data sent by a server that receives TCP or UDP. Only character data reception is currently supported. 3.11 SendMessage
Disclaimer:bool SendMessage (sring phone,string msg,int Overtime)
parameters:phone– phone number
msg– the string to send
overtime– and overtime are different, which is the timeout time to wait for the data to be sent successfully.
return: Send status bool succeeded false failed
Description: send SMS. Only character forwarding is currently supported. 3.12 ReceiveMessage
statement:string[] ReceiveMessage ()
Parameters: None
returns: returns a collection of text strings that are not currently read.
Note: receive SMS. Only character data reception is currently supported. 4 "Example"
Slightly
SOURCE/Document: Http://www.sky-walker.com.cn/MFRelease/library/YFSoft.Hardware.GPRS.rar
TCPServer: