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

📄 sms.cs

📁 串口发送短信
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// lblState
			// 
			this.lblState.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblState.Location = new System.Drawing.Point(40, 328);
			this.lblState.Name = "lblState";
			this.lblState.Size = new System.Drawing.Size(496, 16);
			this.lblState.TabIndex = 16;
			this.lblState.Text = "尚未与任何设备连接";
			// 
			// txtState
			// 
			this.txtState.Location = new System.Drawing.Point(40, 344);
			this.txtState.MaxLength = 160;
			this.txtState.Multiline = true;
			this.txtState.Name = "txtState";
			this.txtState.Size = new System.Drawing.Size(496, 48);
			this.txtState.TabIndex = 17;
			this.txtState.Text = "";
			// 
			// FormSMS
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(560, 421);
			this.Controls.Add(this.txtState);
			this.Controls.Add(this.lblState);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnOpen);
			this.Controls.Add(this.txtCenter);
			this.Controls.Add(this.lblCenter);
			this.Controls.Add(this.btnCan);
			this.Controls.Add(this.btnSend);
			this.Controls.Add(this.txtMessage);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.lblMessage);
			this.Name = "FormSMS";
			this.Text = "发送SMS";
			this.Load += new System.EventHandler(this.FormSMS_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.customerData)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FormSMS());
		}
		//添加用户
		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			if (ValidateField.IsNullField(this.txtName.Text))
			{
				MessageBox.Show(null,"姓名不能为空","Error");
				this.txtName.Focus();
				return;
			}	
				
			if (ValidateField.IsNullField(this.txtPhone.Text))
			{
				MessageBox.Show(null,"手机号码不能为空","Error");
				this.txtPhone.Focus();
				return;
			}
			if (ValidateField.Validate(this.txtPhone.Text))
			{
				customerData.Tables[CustomerDataSet.CUSTOMERS_TABLE].Rows.Add(new object[]{this.txtName.Text.Trim(),this.txtPhone.Text.Trim()});
				this.business.Insert(customerData);
				MessageBox.Show(null,this.txtName.Text + " 添加成功","Success");
				customerData.AcceptChanges();
				this.btnCancel_Click(sender,e);
			}
			else
			{
				MessageBox.Show(null,"号码必须为11位数字","Error");
				this.txtPhone.Text = "";
				this.txtPhone.Focus();
			}
							
		}

		//重置添加用户
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.txtName.Text = "";
			this.txtPhone.Text = "";
			this.txtName.Focus();
		}

		//根据用户名查询号码
		private void btnQuery_Click(object sender, System.EventArgs e)
		{
			if (ValidateField.IsNullField(this.textBoxName.Text))
			{
				MessageBox.Show(null,"姓名不能为空","Error");
				this.textBoxName.Focus();
				return;
			}	
			try
			{
				this.textBoxPhone.Text = this.business.GetCustomerByName(this.textBoxName.Text.Trim()).Tables[CustomerDataSet.CUSTOMERS_TABLE].Rows[0]["Phone"].ToString();

			}
			catch
			{
				MessageBox.Show(null,"没有这个人的手机号码","None");
				this.textBoxName.Text = "";
				this.textBoxName.Focus();
			}
		}

		//清除用户名
		private void btnClear_Click(object sender, System.EventArgs e)
		{
			this.textBoxName.Text = "";
			this.textBoxPhone.Text = "";
		}

		
		private void FormSMS_Load(object sender, System.EventArgs e)
		{
			this.btnSend.Enabled = false;
			this.textBoxName.Focus();
			this.btnCan.Enabled = false;
		}

		//读取 
		//在读之前如果有其他的操作 必须要清除缓冲区 才可以,否则不能够获得想要的数据 how to ?
		//用myComm.read(num)来清除??????????
		
		private void btnCan_Click(object sender, System.EventArgs e)
		{
////			this.txtMessage.Text = "";
////			this.txtMessage.Focus();

			if (myComm.Opened)
			{
				
//				string All = "all";
				myComm.Write(Encoding.ASCII.GetBytes("AT+CMGF=1\r"));
				Thread.Sleep(1000);
				myComm.Write(Encoding.ASCII.GetBytes("AT+CMGR=1\r"));
				string response = Encoding.ASCII.GetString(myComm.Read(200));
				MessageBox.Show("Total:"+response);
//				MessageBox.Show("Test12:"+response.Substring(12));
//				MessageBox.Show("Test13:"+response.Substring(13));
//				MessageBox.Show("Test14:"+response.Substring(14));
//				MessageBox.Show("Test15:"+response.Substring(15));
//				MessageBox.Show("Test16:"+response.Substring(16));
	
				MessageBox.Show("Test28:"+response.Substring(28));
				string phone = response.Substring(50).Substring(0,11);
				MessageBox.Show("Phone:"+phone);
				string date = response.Substring(65).Substring(0,17);
				MessageBox.Show("DateTime:"+date);
				string temp = response.Substring(88);
				MessageBox.Show("Temp:"+temp);
				temp = temp.Substring(0,temp.Length-8);
				this.txtState.Text = this.encoder.Decode(temp.Trim());
			}
			else
			{
				MessageBox.Show(null,"串口未打开","Error");
			}
			

		}

		//发送
		private void btnSend_Click(object sender, System.EventArgs e)
		{

			if (ValidateField.IsNullField(this.textBoxPhone.Text.Trim())) 
			{
				MessageBox.Show(null,"发送目的号码不能为空","Error");
				this.textBoxName.Focus();
				return;
			}
			
			if (ValidateField.IsNullField(this.txtMessage.Text.Trim()))
			{
				MessageBox.Show(null,"发送内容不能为空","Error");
				this.txtMessage.Focus();
				return;
			}

			if (!ValidateField.Validate(this.textBoxPhone.Text.Trim()))
			{
				MessageBox.Show(null,"目的号码必须为11位数字","Error");
				this.textBoxPhone.Focus();
				return;
			}

			
			//短信中心号码,目的号码,发送内容,Text3.Text接受到的内容,Text5.Text端口号?

			string sms = encoder.GetSMSAfterEncoded(this.txtCenter.Text.Trim(),this.textBoxPhone.Text.Trim(),this.txtMessage.Text.Trim());

			byte [] buff = Encoding.ASCII.GetBytes(string.Format("AT+CMGS={0}\r",encoder.Length));
            			
			myComm.Write(buff);
			string response = Encoding.ASCII.GetString(myComm.Read(128));
			if( response.Length > 0 && response.EndsWith("> "))
			{
				myComm.Write(Encoding.ASCII.GetBytes(string.Format("{0}\x01a",sms)));
				MessageBox.Show(null,"发送成功","Success");
			}
			else
			{
				MessageBox.Show(null,"发送失败","Failure");
			}

		}

		//打开端口
		private void btnOpen_Click(object sender, System.EventArgs e)
		{
			
			try
			{
				bool opened = this.InitCom("COM1",9600);
			
			
				bool connected = false;

				if (opened)
				{
					myComm.Write(Encoding.ASCII.GetBytes("AT+CGMI\r")); //获取手机品牌
					string response = Encoding.ASCII.GetString(myComm.Read(128));
					if (response.Length > 0)
					{
						this.lblState.Text = response.Substring(10,7);
						connected = true;
					}
					else
					{
						this.lblState.Text = "与手机连接不成功";
						connected = false;
					}

					myComm.Write(Encoding.ASCII.GetBytes("AT+CGMM\r")); //获取手机型号
					response = Encoding.ASCII.GetString(myComm.Read(128));
					if (response.Length > 0)
					{
						this.lblState.Text = this.lblState.Text +" "+ response.Substring(10,5) + " 连接中......";
						connected = true;
					}
					else
					{
						this.lblState.Text = "与手机连接不成功";
						connected = false;
					}

					myComm.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r")); //获取短信中心号码
					response = Encoding.ASCII.GetString(myComm.Read(128));
					if (response.Length > 0)
					{
						this.txtCenter.Text = response.Substring(20,13);
						connected = true;
					}
					else
					{
						connected = false;
					}
					
					if (connected == true)
					{
						this.btnOpen.Enabled = false;
						this.btnSend.Enabled = true;					
						this.btnCan.Enabled = true;
					}
					
				}
			}
			catch(Exception ex)			
			{
				MessageBox.Show(null,ex.Message + "\r\r" + ex.StackTrace,"Error");
			}
			
		}

		//关闭窗口
		private void btnClose_Click(object sender, System.EventArgs e)
		{
			myComm.Close();
			MessageBox.Show(null,"端口已关闭","关闭端口");
			this.lblState.Text = "尚未与任何设备连接";
			this.btnOpen.Enabled = true;
		}	

		//测试
		private void button1_Click(object sender, System.EventArgs e)
		{
			if (ValidateField.IsNullField(this.textBoxPhone.Text.Trim())) 
			{
				this.textBoxName.Focus();
				return;
			}
			if (!ValidateField.Validate(this.textBoxPhone.Text.Trim()))
			{
				MessageBox.Show(null,"目的号码必须为11位数字","Error");
				this.textBoxPhone.Focus();
				return;
			}
			if ((this.txtMessage.Text.Trim() == string.Empty )||(this.txtMessage.Text.Trim() == null))
			{
				MessageBox.Show(null,"发送内容不能为空","Error");
				return;
			}
			
			MessageBox.Show(encoder.GetSMSAfterEncoded(this.txtCenter.Text.Trim(),this.textBoxPhone.Text.Trim(),this.txtMessage.Text.Trim()));


		}

		//退出
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			myComm.Close();
			Application.Exit();
		}

		public bool InitCom(string m_port, int m_baudrate)
		{
			myComm.PortNum = m_port;
			myComm.BaudRate = m_baudrate;
			myComm.ByteSize = 8;
			myComm.Parity = 0;
			myComm.StopBits = 1;
			myComm.ReadTimeout = 1000;
			try
			{
				if (myComm.Opened)
				{
					myComm.Close();
					myComm.Open();
				}
				else
				{
					myComm.Open();//打开串口
				}
				return true;
			}
			catch
				(Exception e) 
			{
				MessageBox.Show(null,e.Message,"Error");
				return false;
			}
		}

		
	}
}

⌨️ 快捷键说明

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