Create a project, add the reference browsing tab, select MODEM. DLL to open, and then add a namespaceUsing gsmmodem;Then we can use the class we implemented in the previous article.
For details about this C # class library, refer:
Implementation of sms cat Software (C #) <6> sending and receiving sms c # implementation (API)
1:PrivateGsmmodemGM =NewGsmmodem(); // Instantiate the device class
Scan the computer serial port and add the combox1 control:
1:
2:Foreach(StringSInSerialPort. Getportnames ())
3:{
4:Combobox1.items. Add (s );
5:}
- Read SMS:
1:Read key text message Codes
2:Try
3: {
4:GM. readmsgbyindex (Convert. Toint32 (textbox3.text ),OutMsgcenter,OutPhone,OutMSG,OutTime); // call the method of the device object to read the SMS by serial number
5:Textbox4.text ="SMS center :"+ Msgcenter +"\ R \ n"+"Mobile Phone number :"+ Phone +"\ R \ n"+
6:"Text message content :"+ MSG +"\ R \ n"+"Sent :"+ Time. substring (0, 4) +"-"+
7:Time. substring (4, 2) +"-"+ Time. substring (6, 2) +""+ Time. substring (8, 2) +
8:":"+ Time. substring (10, 2) +":"+ Time. substring (12, 2 );
9:Label9.text ="Read successful";
10:Label9.forecolor =Color. Green;
11:}
12:Catch
13: {
14:Label9.text ="Reading failed";
15:Label9.forecolor =Color. Red;
16:Return;
17:}
- Delete SMS:
1:Delete text message key code
2:Try
3: {
4:GM. deletemsgbyindex (Convert. Toint32 (textbox3.text); call the device method to delete
5:}
6:Catch
7: {
8:Label9.text ="Deletion failed";
9:Label9.forecolor =Color. Red;
10:Return;
11:}
-
- New SMS prompt:
This part is troublesome. For more information about delegation, callback functions, and events, see msdn.
1:
2:// Delegate the callback function delegate that receives the text message
3: Delegate voidUpdatadelegate();// There can be parameters, which are not required here
4: UpdatadelegateUpdatehandle =Null;
5:
6:Private voidForm1_load (ObjectSender,EventargsE)
7:{......
8:GM. onrecieved + =NewGsmmodem.Onrecievedhandler(Gm_onrecieved );
9:
10:Updatehandle =NewUpdatadelegate(Updatelabel8 );// Instantiate the delegate
11:
12: }
13:
GM. onrecieved + =NewGsmmodem.Onrecievedhandler(Gm_onrecieved );After receiving the text message, the system automatically runs the gm_onrecieved function.
1:VoidGm_onrecieved (ObjectSender,EventargsE)
2:{
3:Invoke (updatehandle,Null);
4:}
There is only one sentence in the function: Call the function pointed to by the callback function handle updatehandle.
Updatehandle =NewUpdatadelegate(Updatelabel8 );// Instantiate the delegate
Indicates that the name of the function to be called is updatelabel8.
1:VoidUpdatelabel8 ()
2:{
3:Label8.text ="New Message available";
4:Label8.forecolor =Color. Green;
5:}
The function body changes the text property of the control, prompting you to receive a new message. You can also directly read the text message content here.
Read new messages:
1:GM. readmsgbyindex (GM. newmsgindex,OutMsgcenter,OutPhone,OutMSG,OutTime );
Call the serial number to read the text message: the serial number is provided by newmsgindex.