⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendmsg.cs

📁 usb-com for sms 自己开发的类 可以借鉴下
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;



namespace IPP.SMS
{
	/// <summary>
	/// SendMsg 的摘要说明。
	/// </summary>
	public class SendMsg : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox targetNumber;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox CenterNumber;
		private System.Windows.Forms.TextBox smsState;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox smsContent;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ComboBox ConnectPort;
		private System.Windows.Forms.ComboBox ConnectBaudRate;
		private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.Button btnConnect;
		private System.Windows.Forms.Button btnExit;
		static CommPort ss_port=new CommPort();
		PDUdecoding sms=new PDUdecoding();
		private System.Windows.Forms.TextBox ConnectState;

		
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SendMsg()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//

		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.targetNumber = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.CenterNumber = new System.Windows.Forms.TextBox();
			this.smsState = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.smsContent = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.ConnectBaudRate = new System.Windows.Forms.ComboBox();
			this.ConnectPort = new System.Windows.Forms.ComboBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.btnSend = new System.Windows.Forms.Button();
			this.btnConnect = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.ConnectState = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(88, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "对方手机号码";
			// 
			// targetNumber
			// 
			this.targetNumber.Location = new System.Drawing.Point(104, 16);
			this.targetNumber.Name = "targetNumber";
			this.targetNumber.TabIndex = 1;
			this.targetNumber.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(248, 16);
			this.label2.Name = "label2";
			this.label2.TabIndex = 2;
			this.label2.Text = "短信中心号码";
			// 
			// CenterNumber
			// 
			this.CenterNumber.Location = new System.Drawing.Point(368, 16);
			this.CenterNumber.Name = "CenterNumber";
			this.CenterNumber.TabIndex = 3;
			this.CenterNumber.Text = "13800200500";
			// 
			// smsState
			// 
			this.smsState.Location = new System.Drawing.Point(8, 16);
			this.smsState.Multiline = true;
			this.smsState.Name = "smsState";
			this.smsState.ReadOnly = true;
			this.smsState.Size = new System.Drawing.Size(448, 72);
			this.smsState.TabIndex = 4;
			this.smsState.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.smsState);
			this.groupBox1.Location = new System.Drawing.Point(24, 56);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(464, 100);
			this.groupBox1.TabIndex = 5;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "短信发送状态";
			// 
			// smsContent
			// 
			this.smsContent.Location = new System.Drawing.Point(24, 192);
			this.smsContent.Multiline = true;
			this.smsContent.Name = "smsContent";
			this.smsContent.Size = new System.Drawing.Size(184, 104);
			this.smsContent.TabIndex = 5;
			this.smsContent.Text = "";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.ConnectBaudRate);
			this.groupBox2.Controls.Add(this.ConnectPort);
			this.groupBox2.Controls.Add(this.label4);
			this.groupBox2.Controls.Add(this.label3);
			this.groupBox2.Location = new System.Drawing.Point(272, 176);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(224, 88);
			this.groupBox2.TabIndex = 6;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "端口设置";
			// 
			// ConnectBaudRate
			// 
			this.ConnectBaudRate.Items.AddRange(new object[] {
																 "9600",
																 "19200"});
			this.ConnectBaudRate.Location = new System.Drawing.Point(88, 40);
			this.ConnectBaudRate.Name = "ConnectBaudRate";
			this.ConnectBaudRate.Size = new System.Drawing.Size(121, 20);
			this.ConnectBaudRate.TabIndex = 3;
			// 
			// ConnectPort
			// 
			this.ConnectPort.Items.AddRange(new object[] {
															 "com1",
															 "com2",
															 "com4",
															 "com5"});
			this.ConnectPort.Location = new System.Drawing.Point(88, 16);
			this.ConnectPort.Name = "ConnectPort";
			this.ConnectPort.Size = new System.Drawing.Size(121, 20);
			this.ConnectPort.TabIndex = 2;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 40);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 15);
			this.label4.TabIndex = 1;
			this.label4.Text = "波特率";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 17);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(61, 15);
			this.label3.TabIndex = 0;
			this.label3.Text = "通信端口";
			// 
			// btnSend
			// 
			this.btnSend.Location = new System.Drawing.Point(248, 272);
			this.btnSend.Name = "btnSend";
			this.btnSend.TabIndex = 4;
			this.btnSend.Text = "发送";
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			// 
			// btnConnect
			// 
			this.btnConnect.Location = new System.Drawing.Point(344, 272);
			this.btnConnect.Name = "btnConnect";
			this.btnConnect.TabIndex = 7;
			this.btnConnect.Text = "连接";
			this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(448, 272);
			this.btnExit.Name = "btnExit";
			this.btnExit.TabIndex = 8;
			this.btnExit.Text = "退出";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// ConnectState
			// 
			this.ConnectState.Location = new System.Drawing.Point(280, 240);
			this.ConnectState.Name = "ConnectState";
			this.ConnectState.ReadOnly = true;
			this.ConnectState.Size = new System.Drawing.Size(200, 21);
			this.ConnectState.TabIndex = 9;
			this.ConnectState.Text = "";
			// 
			// SendMsg
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(584, 333);
			this.Controls.Add(this.ConnectState);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnConnect);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.CenterNumber);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.targetNumber);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.smsContent);
			this.Controls.Add(this.btnSend);
			this.Name = "SendMsg";
			this.Text = "SendMsg";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 初始化串口
		/// </summary>
		public bool InitCom(string m_port, int m_baudrate)
		{
 			ss_port.PortNum = m_port;//串口号
 			ss_port.BaudRate = m_baudrate;//波特率
 			ss_port.ByteSize = 8;//数据位
 			ss_port.Parity = 0;//
 			ss_port.StopBits = 1;//停止位
 			ss_port.ReadTimeout = 1000;//读超时
 			try
 			{
  			if (ss_port.Opened)
  			{
   			ss_port.Close();
   			ss_port.Open();
  			}
  			else
  			{
   			ss_port.Open();//打开串口
  			}
  			return true;
 			}
 			catch(Exception e) 
 			{
  			MessageBox.Show("错误:" + e.Message);
  			return false;
 			}
		}

		private void btnConnect_Click(object sender, System.EventArgs e)
		{
			bool opened = InitCom(ConnectPort.SelectedItem.ToString(),Convert.ToInt32(ConnectBaudRate.SelectedItem.ToString()));//打开并初始化串口
 			bool Connected = false;
 			if (opened)
 			{
  			ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMI\r")); //获取手机品牌
  			string response = Encoding.ASCII.GetString(ss_port.Read(128));
  			if (response.Length> 0)
  			{
   			ConnectState.Text = response.Substring(10,7);
   			Connected = true;
  			}
  			else
  			{
   			ConnectState.Text = "与手机连接不成功";
   			Connected = false;
  			}
  			ss_port.Write(Encoding.ASCII.GetBytes("AT+CGMM\r"));//获取手机型号
  			response = Encoding.ASCII.GetString(ss_port.Read(128));
  			if(response.Length > 0)
  			{
   			ConnectState.Text =ConnectState.Text+ " " + response.Substring(10,5) + " 连接中......";
   			Connected = true;
  			}
  			else
  			{
   			ConnectState.Text = "与手机连接不成功";
   			Connected = false;
  			}
  			ss_port.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r"));//获取手机短信中心号
  			response = Encoding.ASCII.GetString(ss_port.Read(128));
  			if(response.Length > 0)
  			{
   			CenterNumber.Text = response.Substring(20,13);
   			Connected = true;
  			}
  			else
  			{
   			Connected = false;
  			}
  			if (Connected == true)
  			{
   			btnConnect.Enabled = false;
   			btnSend.Enabled = true;
  			}
  			else
  			{
   			btnConnect.Enabled = true;
   			btnSend.Enabled = false;
  			}
 			}

		}

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			string decodedSMS = sms.smsDecodedsms(CenterNumber.Text,targetNumber.Text,smsContent.Text);
 			byte[] buf =Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}\r",sms.nLength));
 			ss_port.Write(buf);
 			string response = Encoding.ASCII.GetString(ss_port.Read(128));
 			string SendState = "";
 			if( response.Length > 0 && response.EndsWith("> "))
 			{
  			ss_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}\x01a",decodedSMS)));
  			SendState = "发送成功!";
 			}
 			else
 			{
  			SendState = "发送失败";
 			}

 			string Result = String.Format("{0},{1},{2},\n\r",targetNumber.Text,smsContent.Text,SendState);
 			smsState.Text += Result;

		}

	
		private void btnExit_Click(object sender, System.EventArgs e)
		{
            	
			this.Dispose();

		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -