Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using MSCommLib;
6
7 namespace ConsoleApplication4
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 WaveComSMS ws = new WaveComSMS(1, 9600);
14 string cellphonenum = "13554103564";
15 string text = "hello,world!您好\r\n哈哈 我是马劲的短信猫~~";
16 if (ws.Send(cellphonenum, text))
17 {
18 Console.WriteLine("发送成功!");
19 Console.WriteLine("手机号:" + cellphonenum);
20 Console.WriteLine("信息内容:\r\n" + text);
21 }
22 else
23 {
24 Console.WriteLine("发送失败!");
25 }
26 }
27 }
28 /// <summary>
29 /// WaveComSMS短信发送类
30 /// </summary>
31 public class WaveComSMS
32 {
33 /// <summary>
34 /// COM口号码
35 /// </summary>
36 private int comPort=0;
37 /// <summary>
38 /// COM口速度
39 /// </summary>
40 private int comSpeed = 2400;
41 public WaveComSMS(int comPort, int comSpeed)
42 {
43 this.comPort = comPort;
44 this.comSpeed = comSpeed;
45 }
46 /// <summary>
47 /// 手机号转PDU
48 /// </summary>
49 /// <param name="str"></param>
50 /// <returns></returns>
51 private static string ConverPDU(string str)
52 {
53 int tl, ti;
54 string ltem, rtem, ttem;
55 ttem = "";
56 tl = str.Length;
57 if ((tl != 11) & (tl != 13))
58 return "";
59 if (tl == 11)
60 {
61 tl = tl + 2;
62 str = "86" + str;
63 }
64 ti = 0;
65 while (ti < tl)
66 {
67 ltem = str.Substring(ti, 1);
68 if ((ti + 1) == tl)
69 rtem = "F";
70 else rtem = str.Substring(ti + 1, 1);
71 ttem = ttem + rtem + ltem;
72 ti = ti + 2;
73 }
74
75 return ttem;
76 }
77 /// <summary>
78 /// 短信内容转PDU
79 /// </summary>
80 /// <param name="str"></param>
81 /// <returns></returns>
82 private static string ConverUniCode(string str)
83 {
84 Encoding encodingUTF = System.Text.Encoding.BigEndianUnicode;
85 string s = null;
86 byte[] encodedBytes = encodingUTF.GetBytes(str);
87 for (int i = 0; i < encodedBytes.Length; i++)
88 {
89 s += BitConverter.ToString(encodedBytes, i, 1);
90 }
91 return s;
92 }
93 /// <summary>
94 /// 发送短信
95 /// </summary>
96 /// <param name="mobilePhoneNumber">手机号码</param>
97 /// <param name="smsText">信息文本</param>
98 /// <returns></returns>
99 public bool Send(string mobilePhoneNumber, string smsText)
100 {
101 MSComm SMSComm = new MSCommLib.MSCommClass(); //微软的MSCOMM控件声明
102 string prex;
103 string pdu, pnum, pmsg, leng;
104
105 string strIn = "";
106 byte[] bytin;
107 object objin;
108
109 string outstr;
110
111
112 int len;
113 prex = "0011000DA1";
114 pnum = ConverPDU(mobilePhoneNumber);
115 pmsg = ConverUniCode(smsText);
116 len = pmsg.Length;
117 len = len / 2;
118 leng = len.ToString("X");
119 if (len < 16)
120 {
121 leng = "0" + leng;
122 }
123 pdu = prex + pnum + "0008A7" + leng + pmsg;
124
125 len = (pdu.Length / 2) - 1;
126 try
127 {
128 SMSComm.CommPort = Convert.ToInt16(this.comPort);
129 if (SMSComm.PortOpen == true)
130 SMSComm.PortOpen = false;
131
132 SMSComm.Settings = this.comSpeed.ToString();
133 SMSComm.Handshaking = MSCommLib.HandshakeConstants.comNone;
134 SMSComm.InputLen = 0;
135 SMSComm.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;
136
137
138 SMSComm.PortOpen = true;
139 SMSComm.Output = "AT" + "\r\n";
140 System.Threading.Thread.Sleep(1000);
141 objin = SMSComm.Input;
142 bytin = (byte[])objin;
143 strIn = Encoding.ASCII.GetString(bytin);
144
145 SMSComm.Output = "AT" + "\r\n";
146 System.Threading.Thread.Sleep(1000);
147 objin = SMSComm.Input;
148 bytin = (byte[])objin;
149 strIn = Encoding.ASCII.GetString(bytin);
150
151 SMSComm.Output = "AT+CMGF=0" + "\r\n";
152 System.Threading.Thread.Sleep(1000);
153 objin = SMSComm.Input;
154 bytin = (byte[])objin;
155 strIn = Encoding.ASCII.GetString(bytin);
156
157 SMSComm.Output = "AT+CNMI=2,1,0,1" + "\r\n";
158 System.Threading.Thread.Sleep(1000);
159 objin = SMSComm.Input;
160 bytin = (byte[])objin;
161 strIn = Encoding.ASCII.GetString(bytin);
162
163 outstr = string.Format("AT+CMGS={0}\r", len);
164 SMSComm.Output = outstr;
165 System.Threading.Thread.Sleep(1000);
166 objin = SMSComm.Input;
167 bytin = (byte[])objin;
168 strIn = Encoding.ASCII.GetString(bytin);
169 SMSComm.InBufferCount = 0;
170 SMSComm.OutBufferCount = 0;
171
172
173 if ((strIn.Length > 0) && strIn.EndsWith("> "))
174 {
175 outstr = pdu.Trim() + "\x01a";
176 SMSComm.Output = outstr.Trim();
177 System.Threading.Thread.Sleep(5000);
178 objin = SMSComm.Input;
179 bytin = (byte[])objin;
180 strIn = Encoding.ASCII.GetString(bytin);
181 SMSComm.PortOpen = false;
182 return true;
183 }
184 SMSComm.PortOpen = false;
185 return false;
186 }
187 catch
188 {
189 SMSComm.PortOpen = false;
190 return false;
191 }
192 }
193 }
194
195 }
196
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using MSCommLib;
6
7 namespace ConsoleApplication4
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 WaveComSMS ws = new WaveComSMS(1, 9600);
14 string cellphonenum = "13554103564";
15 string text = "hello,world!您好\r\n哈哈 我是马劲的短信猫~~";
16 if (ws.Send(cellphonenum, text))
17 {
18 Console.WriteLine("发送成功!");
19 Console.WriteLine("手机号:" + cellphonenum);
20 Console.WriteLine("信息内容:\r\n" + text);
21 }
22 else
23 {
24 Console.WriteLine("发送失败!");
25 }
26 }
27 }
28 /// <summary>
29 /// WaveComSMS短信发送类
30 /// </summary>
31 public class WaveComSMS
32 {
33 /// <summary>
34 /// COM口号码
35 /// </summary>
36 private int comPort=0;
37 /// <summary>
38 /// COM口速度
39 /// </summary>
40 private int comSpeed = 2400;
41 public WaveComSMS(int comPort, int comSpeed)
42 {
43 this.comPort = comPort;
44 this.comSpeed = comSpeed;
45 }
46 /// <summary>
47 /// 手机号转PDU
48 /// </summary>
49 /// <param name="str"></param>
50 /// <returns></returns>
51 private static string ConverPDU(string str)
52 {
53 int tl, ti;
54 string ltem, rtem, ttem;
55 ttem = "";
56 tl = str.Length;
57 if ((tl != 11) & (tl != 13))
58 return "";
59 if (tl == 11)
60 {
61 tl = tl + 2;
62 str = "86" + str;
63 }
64 ti = 0;
65 while (ti < tl)
66 {
67 ltem = str.Substring(ti, 1);
68 if ((ti + 1) == tl)
69 rtem = "F";
70 else rtem = str.Substring(ti + 1, 1);
71 ttem = ttem + rtem + ltem;
72 ti = ti + 2;
73 }
74
75 return ttem;
76 }
77 /// <summary>
78 /// 短信内容转PDU
79 /// </summary>
80 /// <param name="str"></param>
81 /// <returns></returns>
82 private static string ConverUniCode(string str)
83 {
84 Encoding encodingUTF = System.Text.Encoding.BigEndianUnicode;
85 string s = null;
86 byte[] encodedBytes = encodingUTF.GetBytes(str);
87 for (int i = 0; i < encodedBytes.Length; i++)
88 {
89 s += BitConverter.ToString(encodedBytes, i, 1);
90 }
91 return s;
92 }
93 /// <summary>
94 /// 发送短信
95 /// </summary>
96 /// <param name="mobilePhoneNumber">手机号码</param>
97 /// <param name="smsText">信息文本</param>
98 /// <returns></returns>
99 public bool Send(string mobilePhoneNumber, string smsText)
100 {
101 MSComm SMSComm = new MSCommLib.MSCommClass(); //微软的MSCOMM控件声明
102 string prex;
103 string pdu, pnum, pmsg, leng;
104
105 string strIn = "";
106 byte[] bytin;
107 object objin;
108
109 string outstr;
110
111
112 int len;
113 prex = "0011000DA1";
114 pnum = ConverPDU(mobilePhoneNumber);
115 pmsg = ConverUniCode(smsText);
116 len = pmsg.Length;
117 len = len / 2;
118 leng = len.ToString("X");
119 if (len < 16)
120 {
121 leng = "0" + leng;
122 }
123 pdu = prex + pnum + "0008A7" + leng + pmsg;
124
125 len = (pdu.Length / 2) - 1;
126 try
127 {
128 SMSComm.CommPort = Convert.ToInt16(this.comPort);
129 if (SMSComm.PortOpen == true)
130 SMSComm.PortOpen = false;
131
132 SMSComm.Settings = this.comSpeed.ToString();
133 SMSComm.Handshaking = MSCommLib.HandshakeConstants.comNone;
134 SMSComm.InputLen = 0;
135 SMSComm.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;
136
137
138 SMSComm.PortOpen = true;
139 SMSComm.Output = "AT" + "\r\n";
140 System.Threading.Thread.Sleep(1000);
141 objin = SMSComm.Input;
142 bytin = (byte[])objin;
143 strIn = Encoding.ASCII.GetString(bytin);
144
145 SMSComm.Output = "AT" + "\r\n";
146 System.Threading.Thread.Sleep(1000);
147 objin = SMSComm.Input;
148 bytin = (byte[])objin;
149 strIn = Encoding.ASCII.GetString(bytin);
150
151 SMSComm.Output = "AT+CMGF=0" + "\r\n";
152 System.Threading.Thread.Sleep(1000);
153 objin = SMSComm.Input;
154 bytin = (byte[])objin;
155 strIn = Encoding.ASCII.GetString(bytin);
156
157 SMSComm.Output = "AT+CNMI=2,1,0,1" + "\r\n";
158 System.Threading.Thread.Sleep(1000);
159 objin = SMSComm.Input;
160 bytin = (byte[])objin;
161 strIn = Encoding.ASCII.GetString(bytin);
162
163 outstr = string.Format("AT+CMGS={0}\r", len);
164 SMSComm.Output = outstr;
165 System.Threading.Thread.Sleep(1000);
166 objin = SMSComm.Input;
167 bytin = (byte[])objin;
168 strIn = Encoding.ASCII.GetString(bytin);
169 SMSComm.InBufferCount = 0;
170 SMSComm.OutBufferCount = 0;
171
172
173 if ((strIn.Length > 0) && strIn.EndsWith("> "))
174 {
175 outstr = pdu.Trim() + "\x01a";
176 SMSComm.Output = outstr.Trim();
177 System.Threading.Thread.Sleep(5000);
178 objin = SMSComm.Input;
179 bytin = (byte[])objin;
180 strIn = Encoding.ASCII.GetString(bytin);
181 SMSComm.PortOpen = false;
182 return true;
183 }
184 SMSComm.PortOpen = false;
185 return false;
186 }
187 catch
188 {
189 SMSComm.PortOpen = false;
190 return false;
191 }
192 }
193 }
194
195 }
196