Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace ReceiptGenerationGanapt
{
    public partial class SmsSendApp : Form
    {
        
        private SerialPort serialPort;
        public SmsSendApp()
        {
            InitializeComponent();
        }

        private void SmsSendApp_Load(object sender, EventArgs e)
        {
            this.serialPort = new SerialPort();
            this.serialPort.PortName = "COM5";
            this.serialPort.BaudRate = 9600;
            this.serialPort.Parity = Parity.None;
            this.serialPort.DataBits = 8;
            this.serialPort.StopBits = StopBits.One;
            this.serialPort.Handshake = Handshake.RequestToSend;
            this.serialPort.DtrEnable = true;
            this.serialPort.RtsEnable = true;
            this.serialPort.NewLine = System.Environment.NewLine;
            serialPort.Open();
            MessageBox.Show("Port Connected Successfull");
            send_sms();
            
        }
        public bool send_sms()
        {
            String SMSMessage = "demo msg";
            String CellNumber = "some-number";
            String messageToSend = null;
            if (SMSMessage.Length <= 160)
            {
                messageToSend = SMSMessage;
            }
            else
            {
                messageToSend = SMSMessage.Substring(0, 160);
            }
            if (serialPort.IsOpen)
            {
                this.serialPort.WriteLine(@"AT" + (char)(13));
                Thread.Sleep(200);
                this.serialPort.WriteLine("AT+CMGF=1" + (char)(13));
                Thread.Sleep(200);
                this.serialPort.WriteLine(@"AT+CMGS=""" + CellNumber + @"""" + (char)(13));
                Thread.Sleep(200);
                this.serialPort.WriteLine(SMSMessage + (char)(26));
                return true;
            }
            return false;
        }
    }
}


Edit - CellNumber removed from code.
Posted
Updated 25-Oct-15 5:53am
v3

You get a compiler error because you didn't add a using statement for System.Threading, the namespace which holds the Thread class.

Add this line to your using statements:
C#
using System.Threading;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Oct-15 2:24am    
I voted 5, but...

For an inquirer who doesn't really understand namespaces (and, most likely, assemblies) and who cannot name the types used properly, this won't be enough, because kitchen recipes don't help without understanding. So, I added some explanation in Solution 2, please see.

—SA
Thomas Daniels 26-Oct-15 12:24pm    
Good point; +5.
In addition to Solution 1:

There is no such type as "Thread". Real type name is System.Threading.Thread:
https://msdn.microsoft.com/en-us/library/system.threading.thread%28v=vs.110%29.aspx[^].

Essentially, "using" is nothing but a way to shorted top-level type names, but you should always think of type names as of full type names. And you need to learn namespace, assemblies and their use, and all other basics, before you really can do some programming.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Oct-15 12:36pm    
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900