using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.Threading; namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); loadPorts(); } private void Form2_Load(object sender, EventArgs e) { } private void loadPorts() { string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { cboPorts.Items.Add(port); } } private void cboPorts_SelectedIndexChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { SmsClass sm = new SmsClass(cboPorts.Text); sm.Opens(); sm.sendSms(txtPhone.Text, txtMessage.Text); sm.Closes(); MessageBox.Show("Message Sent!"); } private void AddMobNo_Click(object sender, EventArgs e) { ListBoxNumbers.Items.Add(tbGMobNo.Text); tbGMobNo.Text = ""; } private void btnSendGrouped_Click(object sender, EventArgs e) { SmsClass sm = new SmsClass(cboPorts.Text); for (int a = 0; a <= ListBoxNumbers.Items.Count - 1; a++) { //SmsClass sm = new SmsClass(cboPorts.Text); //sm.Opens(); //sm.sendSms(ListBoxNumbers.Items[a].ToString(), GtxtMessage.Text); //sm.Closes(); //MessageBox.Show("Message Sent Successfully"); sm.Opens(); sm.sendSms(ListBoxNumbers.Items[a].ToString(), GtxtMessage.Text); sm.Closes(); Thread.Sleep(10000); MessageBox.Show("Message Sent Successfully"); } sm = null; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO.Ports; using System.Windows.Forms; namespace WindowsFormsApplication1 { class SmsClass { SerialPort serialPort; public SmsClass(string comPort) { this.serialPort = new SerialPort(); this.serialPort.PortName = comPort; this.serialPort.BaudRate = 9600; this.serialPort.Parity = Parity.None; this.serialPort.DataBits = 8;// Databits are the number of bits in the stream which can be 8 for binary transfer or 7 for text transfer this.serialPort.StopBits = StopBits.One; this.serialPort.Handshake = Handshake.RequestToSend;// Handshake is a protocol between the sender and receiver for determining the data transfer correctness this.serialPort.DtrEnable = true;//Gets or sets a value that enables the Data Terminal Ready this.serialPort.RtsEnable = true;//Gets or sets a value indicating whether the Request to Send this.serialPort.NewLine = System.Environment.NewLine; } public bool sendSms(string cellNo, string sms) { string messages = null; messages = sms; if (this.serialPort.IsOpen == true) { try { this.serialPort.WriteLine("AT" + (char)(13)); Thread.Sleep(4); this.serialPort.WriteLine("AT+CMGF=1" + (char)(13)); Thread.Sleep(5); this.serialPort.WriteLine("AT+CMGS=\"" + cellNo + "\""); Thread.Sleep(10); this.serialPort.WriteLine(">" + messages + (char)(26)); } catch (Exception ex) { MessageBox.Show(ex.Source); } return true; } else return false; } public void Opens() { if (this.serialPort.IsOpen == false) { this.serialPort.Open(); } } public void Closes() { if (this.serialPort.IsOpen == true) { this.serialPort.Close(); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)