How to Send SMS with 3G Modem in C#
By FoxLearn 7/16/2024 9:16:45 AM 12.71K
AT commands are instructions used to control a modem. Every command line starts with "AT" or "at"
Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SendSMS" and then click OK button.
How to Send SMS Message via GSM Modem in C#
Sending SMS messages via a GSM modem in C# involves interacting with the modem using AT commands through a serial port.
Here's a basic outline of how you can achieve this
Drag and drop the Label, TextBox, Button controls from the Visual Studio toolbox onto your form designer, then you can design a simple UI that allows you to send sms message in c# as shown below.
Add a click event handler to the Send button allows you to send an sms in c#
private void btnSend_Click(object sender, EventArgs e) { try { //Configure serial port, using serial port to send sms message SerialPort sp = new SerialPort(); // Change this to the appropriate COM port for your GSM modem sp.PortName = txtPort.Text; // Open the serial port sp.Open(); // Set up GSM modem for SMS mode, using AT command to send sms sp.WriteLine("AT" + Environment.NewLine); Thread.Sleep(100); sp.WriteLine("AT+CMGF=1" + Environment.NewLine); Thread.Sleep(100); sp.WriteLine("AT+CSCS=\"GSM\"" + Environment.NewLine); Thread.Sleep(100); sp.WriteLine("AT+CMGS=\"" + txtPhoneNumber.Text + "\"" + Environment.NewLine);//Set phone number Thread.Sleep(100); sp.WriteLine(txtMessage.Text + Environment.NewLine);//Set messages Thread.Sleep(100); // Send Ctrl+Z to indicate the end of the message sp.Write(new byte[] { 26 }, 0, 1); // Wait for the modem to send the message Thread.Sleep(100); // Read the response from the modem var response = sp.ReadExisting(); if (response.Contains("ERROR")) MessageBox.Show("Send failed !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show("SMS Sent !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); sp.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
First, We will connect to the GSM modem by opening a serial port connection to communicate with the GSM modem, then send AT commands to the modem to configure it for sending SMS messages.
After successful connection of the GSM/GPRS modem with your PC, you can send sms message from this application
VIDEO TUTORIALS