วันพุธที่ 29 พฤษภาคม พ.ศ. 2556

How to send SMS through 3G usb modem (aircard) in C#

Require: API DotRas http://dotras.codeplex.com/releases/view/103495
Require: .Net Framework 3.5 or above


Following below code for find Serial port number for sending SMS.

 using System.Management;  
 using DotRas;  
 class Port 
 {  
      public static String getPort(String DeviceName)  
      {  
         String pn = null;  
         ReadOnlyCollection<RasDevice> devices = RasDevice.GetDevices();  
         foreach (DotRas.RasDevice modem in devices)  
         {  
           if (!modem.Name.ToLower().Contains("miniport") && modem.Name.ToLower().Contains(DeviceName))  
           {  
             rd = modem;  
           }  
           else  
           {  
             rd = null;  
           }  
         }  
         if (rd != null)  
         {  
           MessageBox.Show(rd.Name);  
           ManagementObjectSearcher ms = new ManagementObjectSearcher("Select * from Win32_POTSModem");  
           ManagementObjectCollection mc = ms.Get();  
           foreach (ManagementObject mo in mc)  
           {  
             if (mo.GetPropertyValue("Name").ToString() == rd.Name)  
             {  
               pn = mo.GetPropertyValue("AttachedTo").ToString();  
               break;  
             }  
           }  
         }  
         return pn;  
      }  
 }  

Then following below code for Sending SMS.

 using System.IO.Ports;  
 class SmsHandler  
 {  
     static SerialPort _serialPort;  
     public static void SendSMS(String smstxt, String PhoneNumber, String DeviceName)  
     {  
       _serialPort = new SerialPort(getPort("D-Link HSPA"), 19200, Parity.None, 8, StopBits.One);  
       _serialPort.Handshake = Handshake.None;  
       _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);  
       _serialPort.ReadTimeout = 2000;  
       _serialPort.WriteTimeout = 2000;  
       _serialPort.Open();  
       try  
       {  
         if (!_serialPort.IsOpen)  
           _serialPort.Open();  
         _serialPort.Write("AT+CMGF=1\r\n");  
         Thread.Sleep(1000);  
         _serialPort.Write("AT+CMGS=\"" + PhoneNumber + "\"" + Environment.NewLine);  
         _serialPort.Write(smstxt + (char)26 + Environment.NewLine);  
       }  
       catch (Exception ex)  
       {  
         MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");  
       }  
     }  
     static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)  
     {  
       Thread.Sleep(500);  
       string data = _serialPort.ReadLine();  
       Console.Write(data);  
     }  
 }  

When you want to send SMS. Just use below Code.

 SmsHandler.SendSMS("Some message","+66xxxxxxxxx","D-Link HSPA");  

Thank & Credit
http://dotras.codeplex.com/discussions/442346
http://dotras.codeplex.com/discussions/219006
http://dotras.codeplex.com/discussions/81268
https://sites.google.com/site/controlandelectronics/c-serial-port-example
http://www.diafaan.com/sms-tutorials/gsm-modem-tutorial/at-cmgs-text-mode/
http://stackoverflow.com/questions/7192387/c-sharp-send-text-msg-sms-via-usb-hsdpa-modem-unsupported-message-type