วันอังคารที่ 4 มิถุนายน พ.ศ. 2556

Mysql concat multiple row to one with delimited

mysql> SELECT * FROM blah;
+----+-----+-----------+
| K  | grp | name      |
+----+-----+-----------+
|  1 |   1 | foo       |
|  2 |   1 | bar       |
|  3 |   2 | hydrogen  |
|  4 |   4 | dasher    |
|  5 |   2 | helium    |
|  6 |   2 | lithium   |
|  7 |   4 | dancer    |
|  8 |   3 | winken    |
|  9 |   4 | prancer   |
| 10 |   2 | beryllium |
| 11 |   1 | baz       |
| 12 |   3 | blinken   |
| 13 |   4 | vixen     |
| 14 |   1 | quux      |
| 15 |   4 | comet     |
| 16 |   2 | boron     |
| 17 |   4 | cupid     |
| 18 |   4 | donner    |
| 19 |   4 | blitzen   |
| 20 |   3 | nod       |
| 21 |   4 | rudolph   |
+----+-----+-----------+
21 rows in set (0.00 sec)

mysql> SELECT grp, GROUP_CONCAT(name ORDER BY K) FROM blah GROUP BY grp;
+-----+----------------------------------------------------------------+
| grp | GROUP_CONCAT(name ORDER BY K)                                  |
+-----+----------------------------------------------------------------+
|   1 | foo,bar,baz,quux                                               |
|   2 | hydrogen,helium,lithium,beryllium,boron                        |
|   3 | winken,blinken,nod                                             |
|   4 | dasher,dancer,prancer,vixen,comet,cupid,donner,blitzen,rudolph |
+-----+----------------------------------------------------------------+
4 rows in set (0.00 sec)
http://stackoverflow.com/questions/397708/is-it-possible-to-concatenate-strings-from-multiple-rows-and-tables-into-one-res

วันพุธที่ 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