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

📄 new_fxin.cs

📁 源码..C#开发的手机短信发送接收程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
            this.ConnectBaudRate.TabIndex = 12;
            this.ConnectBaudRate.Text = "19200";
            // 
            // ConnectPort
            // 
            this.ConnectPort.Items.AddRange(new object[] {
            "COM1",
            "COM2",
            "COM3",
            "COM4"});
            this.ConnectPort.Location = new System.Drawing.Point(120, 29);
            this.ConnectPort.Name = "ConnectPort";
            this.ConnectPort.Size = new System.Drawing.Size(80, 20);
            this.ConnectPort.TabIndex = 11;
            this.ConnectPort.Text = "COM1";
            // 
            // btnSend
            // 
            this.btnSend.Enabled = false;
            this.btnSend.Location = new System.Drawing.Point(364, 149);
            this.btnSend.Name = "btnSend";
            this.btnSend.Size = new System.Drawing.Size(48, 32);
            this.btnSend.TabIndex = 0;
            this.btnSend.Text = "发送";
            this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
            // 
            // btnConnect
            // 
            this.btnConnect.Location = new System.Drawing.Point(292, 149);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(48, 32);
            this.btnConnect.TabIndex = 1;
            this.btnConnect.Text = "连接";
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            // 
            // btnExit
            // 
            this.btnExit.Location = new System.Drawing.Point(438, 149);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(48, 32);
            this.btnExit.TabIndex = 3;
            this.btnExit.Text = "退出";
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.BackColor = System.Drawing.SystemColors.Control;
            this.ClientSize = new System.Drawing.Size(521, 370);
            this.Controls.Add(this.CenterNumber);
            this.Controls.Add(this.targetNumber);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnConnect);
            this.Controls.Add(this.btnSend);
            this.Name = "Form1";
            this.Text = "发新短信";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// 应用程序的主入口点。
		[STAThread]
		static void Main() 
		{
            Application.Run(new mainform());
		}

		/// 初始化串口
		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;
			}
		}

		/// 初始化代码,并获取手机相关信息
		/// <param name="sender"></param>
		/// <param name="e"></param>
		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;
				}
			}
		}

		/// 发送短信
		/// <param name="sender"></param>
		/// <param name="e"></param>
		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;
		}

		/// 关闭串口,退出程序
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			ss_port.Close();
		   //Application.Exit();
          // Form1 f4 = new Form1();
           // f4.ShowDialog();
            this.Dispose();
            this.Close();
            // f4.Dispose();
            
		}

        private void Form1_Load(object sender, EventArgs e)
        {

        }   

 }
}

⌨️ 快捷键说明

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