Form1.cs
上传用户:ls4004
上传日期:2007-02-06
资源大小:438k
文件大小:14k
源码类别:

手机短信编程

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Threading;
  8. using System.Text;
  9. using JustinIO;
  10. using SMS;
  11. using System.IO;
  12. namespace smsForCsharp
  13. {
  14. /// <summary>
  15. /// Form1 的摘要说明。
  16. /// </summary>
  17. public class Form1 : System.Windows.Forms.Form
  18. {
  19. static  JustinIO.CommPort ss_port = new JustinIO.CommPort();
  20. PDUdecoding sms = new SMS.PDUdecoding();
  21. private System.Windows.Forms.Label label1;
  22. private System.Windows.Forms.Label label2;
  23. private System.Windows.Forms.GroupBox groupBox1;
  24. private System.Windows.Forms.GroupBox groupBox2;
  25. private System.Windows.Forms.TextBox targetNumber;
  26. private System.Windows.Forms.TextBox CenterNumber;
  27. private System.Windows.Forms.Label label3;
  28. private System.Windows.Forms.Label label4;
  29. private System.Windows.Forms.Label ConnectState;
  30. private System.Windows.Forms.GroupBox groupBox3;
  31. private System.Windows.Forms.Button btnSend;
  32. private System.Windows.Forms.Button btnConnect;
  33. private System.Windows.Forms.Button btnExit;
  34. private System.Windows.Forms.TextBox smsState;
  35. private System.Windows.Forms.TextBox smsContent;
  36. private System.Windows.Forms.ComboBox ConnectPort;
  37. private System.Windows.Forms.ComboBox ConnectBaudRate;
  38. private System.Windows.Forms.Button about;
  39. /// <summary>
  40. /// 必需的设计器变量。
  41. /// </summary>
  42. private System.ComponentModel.Container components = null;
  43. public Form1()
  44. {
  45. //
  46. // Windows 窗体设计器支持所必需的
  47. //
  48. InitializeComponent();
  49. //
  50. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  51. //
  52. }
  53. /// <summary>
  54. /// 清理所有正在使用的资源。
  55. /// </summary>
  56. protected override void Dispose( bool disposing )
  57. {
  58. if( disposing )
  59. {
  60. if (components != null) 
  61. {
  62. components.Dispose();
  63. }
  64. }
  65. base.Dispose( disposing );
  66. }
  67. #region Windows Form Designer generated code
  68. /// <summary>
  69. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  70. /// 此方法的内容。
  71. /// </summary>
  72. private void InitializeComponent()
  73. {
  74. this.label1 = new System.Windows.Forms.Label();
  75. this.label2 = new System.Windows.Forms.Label();
  76. this.groupBox1 = new System.Windows.Forms.GroupBox();
  77. this.smsState = new System.Windows.Forms.TextBox();
  78. this.groupBox2 = new System.Windows.Forms.GroupBox();
  79. this.smsContent = new System.Windows.Forms.TextBox();
  80. this.targetNumber = new System.Windows.Forms.TextBox();
  81. this.CenterNumber = new System.Windows.Forms.TextBox();
  82. this.label3 = new System.Windows.Forms.Label();
  83. this.label4 = new System.Windows.Forms.Label();
  84. this.ConnectState = new System.Windows.Forms.Label();
  85. this.groupBox3 = new System.Windows.Forms.GroupBox();
  86. this.ConnectBaudRate = new System.Windows.Forms.ComboBox();
  87. this.ConnectPort = new System.Windows.Forms.ComboBox();
  88. this.btnSend = new System.Windows.Forms.Button();
  89. this.btnConnect = new System.Windows.Forms.Button();
  90. this.btnExit = new System.Windows.Forms.Button();
  91. this.about = new System.Windows.Forms.Button();
  92. this.groupBox1.SuspendLayout();
  93. this.groupBox2.SuspendLayout();
  94. this.groupBox3.SuspendLayout();
  95. this.SuspendLayout();
  96. // 
  97. // label1
  98. // 
  99. this.label1.Location = new System.Drawing.Point(0, 8);
  100. this.label1.Name = "label1";
  101. this.label1.Size = new System.Drawing.Size(72, 16);
  102. this.label1.TabIndex = 0;
  103. this.label1.Text = "对方手机号:";
  104. // 
  105. // label2
  106. // 
  107. this.label2.Location = new System.Drawing.Point(272, 8);
  108. this.label2.Name = "label2";
  109. this.label2.Size = new System.Drawing.Size(72, 16);
  110. this.label2.TabIndex = 1;
  111. this.label2.Text = "短信中心号:";
  112. // 
  113. // groupBox1
  114. // 
  115. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  116. this.smsState});
  117. this.groupBox1.Location = new System.Drawing.Point(0, 32);
  118. this.groupBox1.Name = "groupBox1";
  119. this.groupBox1.Size = new System.Drawing.Size(496, 136);
  120. this.groupBox1.TabIndex = 3;
  121. this.groupBox1.TabStop = false;
  122. this.groupBox1.Text = "短信发送状态";
  123. // 
  124. // smsState
  125. // 
  126. this.smsState.Location = new System.Drawing.Point(8, 16);
  127. this.smsState.Multiline = true;
  128. this.smsState.Name = "smsState";
  129. this.smsState.ReadOnly = true;
  130. this.smsState.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
  131. this.smsState.Size = new System.Drawing.Size(480, 112);
  132. this.smsState.TabIndex = 0;
  133. this.smsState.Text = "";
  134. // 
  135. // groupBox2
  136. // 
  137. this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
  138. this.smsContent});
  139. this.groupBox2.Location = new System.Drawing.Point(0, 176);
  140. this.groupBox2.Name = "groupBox2";
  141. this.groupBox2.Size = new System.Drawing.Size(248, 152);
  142. this.groupBox2.TabIndex = 4;
  143. this.groupBox2.TabStop = false;
  144. this.groupBox2.Text = "短信内容";
  145. // 
  146. // smsContent
  147. // 
  148. this.smsContent.Location = new System.Drawing.Point(8, 17);
  149. this.smsContent.Multiline = true;
  150. this.smsContent.Name = "smsContent";
  151. this.smsContent.Size = new System.Drawing.Size(229, 127);
  152. this.smsContent.TabIndex = 0;
  153. this.smsContent.Text = "";
  154. // 
  155. // targetNumber
  156. // 
  157. this.targetNumber.Location = new System.Drawing.Point(72, 8);
  158. this.targetNumber.Name = "targetNumber";
  159. this.targetNumber.TabIndex = 5;
  160. this.targetNumber.Text = "";
  161. // 
  162. // CenterNumber
  163. // 
  164. this.CenterNumber.Location = new System.Drawing.Point(344, 8);
  165. this.CenterNumber.Name = "CenterNumber";
  166. this.CenterNumber.TabIndex = 6;
  167. this.CenterNumber.Text = "";
  168. // 
  169. // label3
  170. // 
  171. this.label3.AutoSize = true;
  172. this.label3.Location = new System.Drawing.Point(24, 24);
  173. this.label3.Name = "label3";
  174. this.label3.Size = new System.Drawing.Size(66, 14);
  175. this.label3.TabIndex = 7;
  176. this.label3.Text = "通信端口:";
  177. // 
  178. // label4
  179. // 
  180. this.label4.AutoSize = true;
  181. this.label4.Location = new System.Drawing.Point(24, 56);
  182. this.label4.Name = "label4";
  183. this.label4.Size = new System.Drawing.Size(66, 14);
  184. this.label4.TabIndex = 8;
  185. this.label4.Text = "波 特 率:";
  186. // 
  187. // ConnectState
  188. // 
  189. this.ConnectState.BackColor = System.Drawing.SystemColors.Control;
  190. this.ConnectState.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  191. this.ConnectState.ForeColor = System.Drawing.Color.Red;
  192. this.ConnectState.Location = new System.Drawing.Point(8, 80);
  193. this.ConnectState.Name = "ConnectState";
  194. this.ConnectState.Size = new System.Drawing.Size(224, 16);
  195. this.ConnectState.TabIndex = 10;
  196. this.ConnectState.Text = "尚未与任何设备连接";
  197. // 
  198. // groupBox3
  199. // 
  200. this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
  201. this.ConnectBaudRate,
  202. this.ConnectPort,
  203. this.ConnectState,
  204. this.label3,
  205. this.label4});
  206. this.groupBox3.Location = new System.Drawing.Point(248, 176);
  207. this.groupBox3.Name = "groupBox3";
  208. this.groupBox3.Size = new System.Drawing.Size(240, 104);
  209. this.groupBox3.TabIndex = 0;
  210. this.groupBox3.TabStop = false;
  211. this.groupBox3.Text = "端口设置";
  212. // 
  213. // ConnectBaudRate
  214. // 
  215. this.ConnectBaudRate.Items.AddRange(new object[] {
  216.  "9600",
  217.  "14400",
  218.  "19200",
  219.  "57600"});
  220. this.ConnectBaudRate.Location = new System.Drawing.Point(120, 56);
  221. this.ConnectBaudRate.Name = "ConnectBaudRate";
  222. this.ConnectBaudRate.Size = new System.Drawing.Size(80, 20);
  223. this.ConnectBaudRate.TabIndex = 12;
  224. this.ConnectBaudRate.Text = "19200";
  225. // 
  226. // ConnectPort
  227. // 
  228. this.ConnectPort.Items.AddRange(new object[] {
  229.  "COM1",
  230.  "COM2",
  231.  "COM3",
  232.  "COM4"});
  233. this.ConnectPort.Location = new System.Drawing.Point(120, 24);
  234. this.ConnectPort.Name = "ConnectPort";
  235. this.ConnectPort.Size = new System.Drawing.Size(80, 20);
  236. this.ConnectPort.TabIndex = 11;
  237. this.ConnectPort.Text = "COM1";
  238. // 
  239. // btnSend
  240. // 
  241. this.btnSend.Enabled = false;
  242. this.btnSend.Location = new System.Drawing.Point(256, 288);
  243. this.btnSend.Name = "btnSend";
  244. this.btnSend.Size = new System.Drawing.Size(48, 32);
  245. this.btnSend.TabIndex = 0;
  246. this.btnSend.Text = "发送";
  247. this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
  248. // 
  249. // btnConnect
  250. // 
  251. this.btnConnect.Location = new System.Drawing.Point(312, 288);
  252. this.btnConnect.Name = "btnConnect";
  253. this.btnConnect.Size = new System.Drawing.Size(48, 32);
  254. this.btnConnect.TabIndex = 1;
  255. this.btnConnect.Text = "连接";
  256. this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
  257. // 
  258. // btnExit
  259. // 
  260. this.btnExit.Location = new System.Drawing.Point(438, 288);
  261. this.btnExit.Name = "btnExit";
  262. this.btnExit.Size = new System.Drawing.Size(48, 32);
  263. this.btnExit.TabIndex = 3;
  264. this.btnExit.Text = "退出";
  265. this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
  266. // 
  267. // about
  268. // 
  269. this.about.Location = new System.Drawing.Point(376, 288);
  270. this.about.Name = "about";
  271. this.about.Size = new System.Drawing.Size(48, 32);
  272. this.about.TabIndex = 7;
  273. this.about.Text = "说明";
  274. this.about.Click += new System.EventHandler(this.about_Click);
  275. // 
  276. // Form1
  277. // 
  278. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  279. this.ClientSize = new System.Drawing.Size(504, 333);
  280. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  281.   this.about,
  282.   this.CenterNumber,
  283.   this.targetNumber,
  284.   this.groupBox2,
  285.   this.groupBox1,
  286.   this.label2,
  287.   this.label1,
  288.   this.groupBox3,
  289.   this.btnExit,
  290.   this.btnConnect,
  291.   this.btnSend});
  292. this.Name = "Form1";
  293. this.Text = "终端短信C#版 ---- 天极网";
  294. this.groupBox1.ResumeLayout(false);
  295. this.groupBox2.ResumeLayout(false);
  296. this.groupBox3.ResumeLayout(false);
  297. this.ResumeLayout(false);
  298. }
  299. #endregion
  300. /// <summary>
  301. /// 应用程序的主入口点。
  302. /// </summary>
  303. [STAThread]
  304. static void Main() 
  305. {
  306. Application.Run(new Form1());
  307. }
  308. /// <summary>
  309. /// 初始化串口
  310. /// </summary>
  311. public bool InitCom(string m_port, int m_baudrate)
  312. {
  313. ss_port.PortNum = m_port;
  314. ss_port.BaudRate = m_baudrate;
  315. ss_port.ByteSize = 8;
  316. ss_port.Parity = 0;
  317. ss_port.StopBits = 1;
  318. ss_port.ReadTimeout = 1000;
  319. try
  320. {
  321. if (ss_port.Opened)
  322. {
  323. ss_port.Close();
  324. ss_port.Open();
  325. }
  326. else
  327. {
  328. ss_port.Open();//打开串口
  329. }
  330. return true;
  331. }
  332. catch
  333. (Exception e) 
  334. {
  335. MessageBox.Show("错误:" + e.Message);
  336. return false;
  337. }
  338. }
  339. /// <summary>
  340. /// 初始化代码,并获取手机相关信息
  341. /// </summary>
  342. /// <param name="sender"></param>
  343. /// <param name="e"></param>
  344. private void btnConnect_Click(object sender, System.EventArgs e)
  345. {
  346. bool opened = InitCom(ConnectPort.SelectedItem.ToString(),Convert.ToInt32(ConnectBaudRate.SelectedItem.ToString()));
  347. bool Connected = false;
  348. if (opened)
  349. {
  350. ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMIr")); //获取手机品牌
  351. string response = Encoding.ASCII.GetString(ss_port.Read(128));
  352. if (response.Length > 0)
  353. {
  354. ConnectState.Text = response.Substring(10,7);
  355. Connected = true;
  356. }
  357. else
  358.  {
  359. ConnectState.Text = "与手机连接不成功";
  360. Connected = false;
  361.  }
  362. ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMMr"));//获取手机型号
  363. response = Encoding.ASCII.GetString(ss_port.Read(128));
  364. if(response.Length > 0)
  365. {
  366. ConnectState.Text =ConnectState.Text+ " " + response.Substring(10,5) + "  连接中......";
  367. Connected = true;
  368. }
  369. else
  370. {
  371. ConnectState.Text = "与手机连接不成功";
  372. Connected = false;
  373. }
  374. ss_port.Write(Encoding.ASCII.GetBytes("AT+CSCA?r"));//获取手机短信中心号
  375.                 response = Encoding.ASCII.GetString(ss_port.Read(128));
  376. if(response.Length > 0)
  377. {
  378. CenterNumber.Text = response.Substring(20,13);
  379. Connected = true;
  380. }
  381. else
  382. {
  383. Connected = false;
  384. }
  385. if (Connected == true)
  386. {
  387. btnConnect.Enabled = false;
  388. btnSend.Enabled = true;
  389. }
  390. else
  391. {
  392. btnConnect.Enabled = true;
  393. btnSend.Enabled = false;
  394. }
  395. }
  396. }
  397. /// <summary>
  398. /// 发送短信
  399. /// </summary>
  400. /// <param name="sender"></param>
  401. /// <param name="e"></param>
  402. private void btnSend_Click(object sender, System.EventArgs e)
  403. {
  404. string decodedSMS = sms.smsDecodedsms(CenterNumber.Text,targetNumber.Text,smsContent.Text);
  405. byte[] buf =Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}r",sms.nLength));
  406. ss_port.Write(buf);
  407. string response = Encoding.ASCII.GetString(ss_port.Read(128));
  408. string SendState = "";
  409. if( response.Length > 0 && response.EndsWith("> "))
  410. {
  411. ss_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}x01a",decodedSMS)));
  412. SendState = "发送成功!";
  413. }
  414. else
  415. {
  416. SendState = "发送失败";
  417. }
  418. string Result = String.Format("{0},{1},{2}。nr",targetNumber.Text,smsContent.Text,SendState);
  419. smsState.Text += Result;
  420. }
  421. /// <summary>
  422. /// 关闭串口,退出程序
  423. /// </summary>
  424. /// <param name="sender"></param>
  425. /// <param name="e"></param>
  426. private void btnExit_Click(object sender, System.EventArgs e)
  427. {
  428. ss_port.Close();
  429. Application.Exit();
  430. }
  431. private void about_Click(object sender, System.EventArgs e)
  432. {
  433. Form about = new about();
  434. about.ShowDialog();
  435. }
  436. }
  437. }