今年三月份,用VB,C#各写一份用手机+电脑+数据线发送短信的程式!感觉还行。 以下的功能单一,只能单发,或群发。只是初始的版本。朋友们可以看看其中的 一些技巧,如怎么将进值转换,中文转换,字符移位处理。用MSCOMM怎么发送和接收 数据,怎么延时等! using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Threading; namespace SendSMS { /// /// Form1 的摘要说明。 /// public class FrmMain : System.Windows.Forms.Form { #region 控件的初始化===================================================== private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.ComboBox cboBox2; private System.Windows.Forms.ComboBox cboBox1; private System.Windows.Forms.Button btnCnn; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label lbl; private System.Windows.Forms.Label lbls; private System.Windows.Forms.Button btnClose; private System.Windows.Forms.Label lblcnnInfo; private AxMSCommLib.AxMSComm MSCOMM; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Label lblCenter; private System.Windows.Forms.TextBox txtCenter; private System.Windows.Forms.Label lblVirtualDate; private System.Windows.Forms.Label lblMobile; private System.Windows.Forms.TextBox txtMobile; private System.Windows.Forms.Label label1; private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.StatusBarPanel statusBarPanel1; private System.Windows.Forms.StatusBarPanel statusBarPanel2; private System.Windows.Forms.StatusBarPanel statusBarPanel3; private System.Windows.Forms.Button btnSendSMS; private System.Windows.Forms.TextBox txtSMS; private System.Windows.Forms.ComboBox cboBox3; private System.Windows.Forms.Label lblSMS; private System.Windows.Forms.TextBox txtInput; /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; public FrmMain() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmMain)); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.MSCOMM = new AxMSCommLib.AxMSComm(); this.lblcnnInfo = new System.Windows.Forms.Label(); this.btnClose = new System.Windows.Forms.Button(); this.cboBox2 = new System.Windows.Forms.ComboBox(); this.cboBox1 = new System.Windows.Forms.ComboBox(); this.btnCnn = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.lbl = new System.Windows.Forms.Label(); this.lbls = new System.Windows.Forms.Label(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.txtInput = new System.Windows.Forms.TextBox(); this.lblSMS = new System.Windows.Forms.Label(); this.cboBox3 = new System.Windows.Forms.ComboBox(); this.btnSendSMS = new System.Windows.Forms.Button(); this.txtSMS = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.txtMobile = new System.Windows.Forms.TextBox(); this.lblMobile = new System.Windows.Forms.Label(); this.lblVirtualDate = new System.Windows.Forms.Label(); this.txtCenter = new System.Windows.Forms.TextBox(); this.lblCenter = new System.Windows.Forms.Label(); this.statusBar1 = new System.Windows.Forms.StatusBar(); this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel(); this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel(); this.statusBarPanel3 = new System.Windows.Forms.StatusBarPanel(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.MSCOMM)).BeginInit(); this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.MSCOMM); this.groupBox1.Controls.Add(this.lblcnnInfo); this.groupBox1.Controls.Add(this.btnClose); this.groupBox1.Controls.Add(this.cboBox2); this.groupBox1.Controls.Add(this.cboBox1); this.groupBox1.Controls.Add(this.btnCnn); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.lbl); this.groupBox1.Controls.Add(this.lbls); this.groupBox1.Location = new System.Drawing.Point(8, 8); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(192, 328); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "通讯设置"; // // MSCOMM // this.MSCOMM.ContainingControl = this; this.MSCOMM.Enabled = true; this.MSCOMM.Location = new System.Drawing.Point(120, 264); this.MSCOMM.Name = "MSCOMM"; this.MSCOMM.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("MSCOMM.OcxState"))); this.MSCOMM.Size = new System.Drawing.Size(38, 38); this.MSCOMM.TabIndex = 9; this.MSCOMM.OnComm += new System.EventHandler(this.MSCOMM_OnComm); // // lblcnnInfo // this.lblcnnInfo.AutoSize = true; this.lblcnnInfo.Location = new System.Drawing.Point(8, 208); this.lblcnnInfo.Name = "lblcnnInfo"; this.lblcnnInfo.Size = new System.Drawing.Size(0, 17); this.lblcnnInfo.TabIndex = 8; // // btnClose // this.btnClose.Enabled = false; this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnClose.ForeColor = System.Drawing.SystemColors.ControlText; this.btnClose.Location = new System.Drawing.Point(96, 152); this.btnClose.Name = "btnClose"; this.btnClose.TabIndex = 6; this.btnClose.Text = "断开(&D)"; this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // cboBox2 // this.cboBox2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224))); this.cboBox2.Cursor = System.Windows.Forms.Cursors.PanSouth; this.cboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboBox2.Items.AddRange(new object[] { "2400", "4800", "9600", "19200", "38400", "57600", "115200"}); this.cboBox2.Location = new System.Drawing.Point(78, 96); this.cboBox2.Name = "cboBox2"; this.cboBox2.Size = new System.Drawing.Size(90, 20); this.cboBox2.TabIndex = 5; // // cboBox1 // this.cboBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224))); this.cboBox1.Cursor = System.Windows.Forms.Cursors.PanSouth; this.cboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboBox1.Items.AddRange(new object[] { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6"}); this.cboBox1.Location = new System.Drawing.Point(78, 64); this.cboBox1.Name = "cboBox1"; this.cboBox1.Size = new System.Drawing.Size(90, 20); this.cboBox1.TabIndex = 4; // // btnCnn // this.btnCnn.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnCnn.Location = new System.Drawing.Point(8, 152); this.btnCnn.Name = "btnCnn"; this.btnCnn.TabIndex = 3; this.btnCnn.Text = "连接(&C)"; this.btnCnn.Click += new System.EventHandler(this.btnCnn_Click); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(14, 96); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(66, 17); this.label2.TabIndex = 2; this.label2.Text = "通讯速率:"; // // lbl // this.lbl.AutoSize = true; this.lbl.Location = new System.Drawing.Point(14, 72); this.lbl.Name = "lbl"; this.lbl.Size = new System.Drawing.Size(66, 17); this.lbl.TabIndex = 1; this.lbl.Text = "通讯端口:"; // // lbls // this.lbls.ForeColor = System.Drawing.Color.Red; this.lbls.Location = new System.Drawing.Point(14, 23); this.lbls.Name = "lbls"; this.lbls.Size = new System.Drawing.Size(144, 32); this.lbls.TabIndex = 0; this.lbls.Text = "请选择正确的通讯端口,通讯速率一般为19200。"; // // groupBox2 // this.groupBox2.Controls.Add(this.txtInput); this.groupBox2.Controls.Add(this.lblSMS); this.groupBox2.Controls.Add(this.cboBox3); this.groupBox2.Controls.Add(this.btnSendSMS); this.groupBox2.Controls.Add(this.txtSMS); this.groupBox2.Controls.Add(this.label1); this.groupBox2.Controls.Add(this.txtMobile); this.groupBox2.Controls.Add(this.lblMobile); this.groupBox2.Controls.Add(this.lblVirtualDate); this.groupBox2.Controls.Add(this.txtCenter); this.groupBox2.Controls.Add(this.lblCenter); this.groupBox2.Location = new System.Drawing.Point(200, 8); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(312, 328); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "短信"; // // txtInput // this.txtInput.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224))); this.txtInput.Enabled = false; this.txtInput.Location = new System.Drawing.Point(200, 40); this.txtInput.Multiline = true; this.txtInput.Name = "txtInput"; this.txtInput.Size = new System.Drawing.Size(96, 120); this.txtInput.TabIndex = 11; this.txtInput.Text = ""; // // lblSMS // this.lblSMS.AutoSize = true; this.lblSMS.ForeColor = System.Drawing.Color.Red; this.lblSMS.Location = new System.Drawing.Point(16, 304); this.lblSMS.Name = "lblSMS"; this.lblSMS.Size = new System.Drawing.Size(0, 17); this.lblSMS.TabIndex = 10; // // cboBox3 // this.cboBox3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224))); this.cboBox3.Cursor = System.Windows.Forms.Cursors.PanSouth; this.cboBox3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboBox3.Items.AddRange(new object[] { "00 - 没有 ", "47 - 6小时", "90 - 12小时", "A7 - 1天", "A8 - 2天", "A9 - 3天", "AA - 4天", "AB - 5天", "AC - 6天", "AD - 7天"}); this.cboBox3.Location = new System.Drawing.Point(16, 88); this.cboBox3.Name = "cboBox3"; this.cboBox3.Size = new System.Drawing.Size(176, 20); this.cboBox3.TabIndex = 9; // // btnSendSMS // this.btnSendSMS.Enabled = false; this.btnSendSMS.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnSendSMS.ForeColor = System.Drawing.SystemColors.ControlText; this.btnSendSMS.Location = new System.Drawing.Point(184, 296); this.btnSendSMS.Name = "btnSendSMS"; this.btnSendSMS.TabIndex = 8; this.btnSendSMS.Text = "发送(&S)"; this.btnSendSMS.Click += new System.EventHandler(this.btnSendSMS_Click); // // txtSMS // this.txtSMS.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224))); this.txtSMS.Location = new System.Drawing.Point(16, 184); this.txtSMS.Multiline = true; this.txtSMS.Name = "txtSMS"; this.txtSMS.Size = new System.Drawing.Size(288, 104); this.txtSMS.TabIndex = 7; this.txtSMS.Text = ""; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(16, 168); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(208, 17); this.label1.TabIndex = 6; this.label1.Text = "短信内容:(内容不能超过104个字符)"; // // txtMobile // this.txtMobile.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224))); this.txtMobile.Location = new System.Drawing.Point(16, 136); this.txtMobile.MaxLength = 11; this.txtMobile.Name = "txtMobile"; this.txtMobile.Size = new System.Drawing.Size(176, 21); this.txtMobile.TabIndex = 5; this.txtMobile.Text = ""; this.txtMobile.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtMobile_KeyPress); // // lblMobile // this.lblMobile.AutoSize = true; this.lblMobile.Location = new System.Drawing.Point(16, 120); this.lblMobile.Name = "lblMobile"; this.lblMobile.Size = new System.Drawing.Size(178, 17); this.lblMobile.TabIndex = 4; this.lblMobile.Text = "发送手机号:(例:13121011900)"; // // lblVirtualDate // this.lblVirtualDate.AutoSize = true; this.lblVirtualDate.Location = new System.Drawing.Point(16, 72); this.lblVirtualDate.Name = "lblVirtualDate"; this.lblVirtualDate.Size = new System.Drawing.Size(79, 17); this.lblVirtualDate.TabIndex = 2; this.lblVirtualDate.Text = "短信有效期:"; // // txtCenter // this.txtCenter.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224))); this.txtCenter.ForeColor = System.Drawing.SystemColors.ControlText; this.txtCenter.Location = new System.Drawing.Point(16, 40); this.txtCenter.MaxLength = 14; this.txtCenter.Name = "txtCenter"; this.txtCenter.Size = new System.Drawing.Size(176, 21); this.txtCenter.TabIndex = 1; this.txtCenter.Text = "+8613010112500"; this.txtCenter.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtCenter_KeyPress); // // lblCenter // this.lblCenter.AutoSize = true; this.lblCenter.Location = new System.Drawing.Point(16, 24); this.lblCenter.Name = "lblCenter"; this.lblCenter.Size = new System.Drawing.Size(208, 17); this.lblCenter.TabIndex = 0; this.lblCenter.Text = "短信中心号码:(例:+8613010112500)"; // // statusBar1 // this.statusBar1.Cursor = System.Windows.Forms.Cursors.Hand; this.statusBar1.Location = new System.Drawing.Point(0, 339); this.statusBar1.Name = "statusBar1"; this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.statusBarPanel1, this.statusBarPanel2, this.statusBarPanel3}); this.statusBar1.ShowPanels = true; this.statusBar1.Size = new System.Drawing.Size(518, 24); this.statusBar1.TabIndex = 3; // // statusBarPanel1 // this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents; this.statusBarPanel1.Icon = ((System.Drawing.Icon)(resources.GetObject("statusBarPanel1.Icon"))); this.statusBarPanel1.Width = 31; // // statusBarPanel2 // this.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents; this.statusBarPanel2.Width = 10; // // statusBarPanel3 // this.statusBarPanel3.Width = 300; // // FrmMain // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(518, 363); this.Controls.Add(this.statusBar1); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.MaximizeBox = false; this.Name = "FrmMain"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "短信群发系统.."; this.Load += new System.EventHandler(this.FrmMain_Load); this.groupBox1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.MSCOMM)).EndInit(); this.groupBox2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel3)).EndInit(); this.ResumeLayout(false); } #endregion /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new FrmMain()); } private void FrmMain_Load(object sender, System.EventArgs e) { this.cboBox1.Text="COM1"; this.cboBox2.Text="19200"; this.cboBox3.Text="A7 - 1天"; StatusBar(); } #endregion #region 手机与电脑连接和断开事件========================================= private void btnCnn_Click(object sender, System.EventArgs e) { lblcnnInfo.Text=""; btnClose.Enabled =false; if (MSCOMM.PortOpen) { MessageBox.Show("端口已打开!","提示信息"); } else { MSCOMM.PortOpen =true; lblcnnInfo.Text="COM1已打开!"; } MSCOMM.InBufferCount = 0; string strTemp="AT\r"; int T = Environment.TickCount; int L; string Buffer = ""; MSCOMM.Output = strTemp; do { System.Windows.Forms.Application.DoEvents(); Console.WriteLine(Environment.TickCount - T ); lblcnnInfo.Text = "COM1 已打开," +"\r"+ "正在连接手机 ..."; Buffer += MSCOMM.Input; L=Buffer.IndexOf("OK\r\n"); } while(L == -1 && Environment.TickCount - T <=2500); if (L!=-1) { btnCnn.Enabled=false; btnClose.Enabled =true; btnSendSMS.Enabled =true; lblcnnInfo.Text = "COM1 已打开," + (char)13 + "连接手机成功!"; } else { btnCnn.Enabled =true; btnClose.Enabled =false; MSCOMM.PortOpen =false; btnSendSMS.Enabled =false; lblcnnInfo.Text = "COM1 已打开," + (char)13 + "连接手机失败!"; } } private void btnClose_Click(object sender, System.EventArgs e) { if (MSCOMM.PortOpen) { MSCOMM.PortOpen =false; } btnCnn.Enabled =true; btnClose.Enabled =false; btnSendSMS.Enabled =false; lblcnnInfo.Text = "COM1关闭, 已断开手机的连接!"; } #endregion #region 发送短信========================================================= private void btnSendSMS_Click(object sender, System.EventArgs e) { lblSMS.Text=""; if(this.txtCenter.Text.Trim()=="") { MessageBox.Show("提示:请输入短信中心服务号码!","短信发送"); this.txtCenter.Focus(); return; } else if(this.txtMobile.Text.Trim()=="") { MessageBox.Show("提示:请输入要发送的手机号!","短信发送"); this.txtMobile.Focus(); return; } else if(this.txtSMS.Text.Trim()=="") { MessageBox.Show("提示:请输入要发送的短信内容!","短信发送"); this.txtSMS.Focus(); return; } //将没字转换成16进制 string strInput=txtSMS.Text; string txt=""; string strTemp=""; for (int i = 0; i < strInput.Length; i++) { string str = "0000" + ((int)strInput).ToString("X"); txt += str.Substring(str.Length - 4); } //有效期 string cbo =this.cboBox3.Text; cbo=cbo.Substring(0,2); strInput =txt; strTemp = strTemp + "08"; //SMSC地址信息的长度(91683110301405F0共八位字节) strTemp = strTemp + "91"; //SMSC地址格式(TON/NPI) strTemp = strTemp + strTOstr(txtCenter.Text.Substring(1)); //SMSC地址(8613010341500南京的短信服务中心号码) strTemp = strTemp + "11" ; //基本参数(TP-MTI/VFP)( 发送,接收为84) strTemp = strTemp + "00" ; //MR Message Reference strTemp = strTemp + "0D" ; //目标SIM卡号码长度(683159717456F4按阿拉伯数字个数计) strTemp = strTemp + "91" ; //同上 strTemp = strTemp + "68"; strTemp = strTemp + strTOstr(txtMobile.Text); //要发送的手机号 strTemp = strTemp + "00"; //普通GSM类型,点到点方式 strTemp = strTemp + "08"; //UCS2编码 strTemp = strTemp + cbo; //有效期 string SMSLength = (txt.Length / 2).ToString("X"); strTemp=strTemp + SMSLength; strTemp=strTemp + txt; strTemp = strTemp + (char)26; // MSCOMM.Output = "AT+CMGF=0" + vbCr '以Pdu模式发送短信 MSCOMM.Output = "AT+CMGS=" + (15 + txtSMS.Text.Length * 2) + (char)13; Thread.Sleep(2000); MSCOMM.Output = strTemp; lblSMS.Text="正在发送手机短信..."; int T = Environment.TickCount; int L; string Buffer = ""; do { System.Windows.Forms.Application.DoEvents(); Console.WriteLine(Environment.TickCount - T ); Buffer += MSCOMM.Input; L=Buffer.IndexOf("OK\r\n"); } while(L == -1 && Environment.TickCount - T <=5500); if (L!=-1) { lblSMS.Text = "短信发送成功!"; } else { lblSMS.Text = "短信发送失败!"; } } #endregion #region 输入校验处理===================================================== private void txtCenter_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if("+0123456789".IndexOf(e.KeyChar) == -1 && e.KeyChar !=(char)8 ) { e.Handled = true ; } } private void txtMobile_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if("0123456789".IndexOf(e.KeyChar) == -1 && e.KeyChar !=(char)8) { e.Handled = true ; } } #endregion #region 手机号移位处理=================================================== string strTOstr(string txt) { txt=txt+"F"; string txt2=""; for (int i = 1; i < txt.Length; i=i+2) { txt2 = txt2 + txt.Substring(i,1); txt2 = txt2 + txt.Substring(i-1,1); } return txt2; } #endregion #region MSCOMM 返回的信息放入文本框 txtInput 中========================== private void MSCOMM_OnComm(object sender, System.EventArgs e) { txtInput.Text=(string)MSCOMM.Input; } #endregion #region 状态栏处理======================================================= private void StatusBar() { this.statusBarPanel1.Text ="版权所有 2004-2008"; this.statusBarPanel2.Text = "设计:Super "; this.statusBarPanel3.Text = "网址: http://www.wsoft.cn"; } #endregion } }
关键字词: