Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# sending sms from computer

Tags:

c#

text

sms

i have this code:

private SerialPort port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);

                  Console.WriteLine("Incoming Data:");
                  port.WriteTimeout = 5000;
                  port.ReadTimeout = 5000;
                  // Attach a method to be called when there is data waiting in the port's buffer
                  port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

                  // Begin communications
                  port.Open();
                  #region PhoneSMSSetup
                  port.Write("AT+CMGF=1\r\n");
                  Thread.Sleep(500);
                  port.Write("AT+CNMI=2,2\r\n");
                  Thread.Sleep(500);
                  port.Write("AT+CSCA=\"+4790002100\"\r\n");
                  Thread.Sleep(500);
                  #endregion
                  // Enter an application loop which keeps this thread alive
                  Application.Run();

i got it from here:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22832563.html

i have a new winforms empty application:

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;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

can you please tell me:

  1. where exactly would i paste the code?
  2. how do i get the code to run?

i am sending AT COMMANDS to my cell phone that is attached to the computer

like image 592
Alex Gordon Avatar asked Sep 01 '26 20:09

Alex Gordon


2 Answers

The top code sample seems to indicate that it should be running in a separate thread (which would make sense) so add a start button to your form and add a Click event handler to it.

I just grabbed the code below from here and reformatted it a little.

In that event handler write something like:

ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();

And then create a class like this:

public class ThreadWork 
{
   public static void DoWork()
   {
       // put your top half code in here, the bit that does the actual serial communication
   }
}

And then add the event handler port_DataReceived that your code expects.

like image 158
Hans Olsson Avatar answered Sep 03 '26 11:09

Hans Olsson


Drop a SerialPort from the toolbox onto your form. Set its properties and double-click DataReceived. That takes care of the first 5 lines.

In the Load event handler, put the Open and Write calls, that takes care of the rest of the lines.

Drop a TextBox on the form, set its MultiLine property to True. Write this code in the DataReceived event handler:

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) {
        string response = serialPort1.ReadLine();
        this.BeginInvoke(new MethodInvoker(
            () => textBox1.AppendText(response + "\r\n")
        ));
    }

From there, you can tinker with the form design to make it more useful. Maybe you want to add a Button whose Click event polls the modem again.

like image 34
Hans Passant Avatar answered Sep 03 '26 12:09

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!