making call from gsm modem in C# - c#

I want to make a call from a GSM modem using C#. I have written the following code. but I am unable to make the call. Please tell what the mistake is. Also let me know how to handle the response in the code from the modem so that I can display a message like "call connecting" or "cannot connect".
private void button1_Click(object sender, EventArgs e)
{
SerialPort po = new SerialPort();
po.PortName = "COM3";
po.BaudRate = int.Parse( "9600");
po.DataBits = Convert.ToInt32("8");
po.Parity = Parity.None;
po.StopBits = StopBits.One;
po.ReadTimeout = int.Parse("300");
po.WriteTimeout = int.Parse("300");
po.Encoding = Encoding.GetEncoding("iso-8859-1");
po.Open();
po.DtrEnable = true;
po.RtsEnable = true;
po.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
po.Write("ATD9030665834;");
}
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (e.EventType == SerialData.Chars)
{
//what to write here to display the response??
}
}

Use port.WriteLine("ATD"+phno+";");
This will definitely solve your problem..
And to handle the response use port.ReadExisting() and compare with your requirement. As easy as that :)
Good luck..

Make Sure whether you are configuring po same as Hyper-terminal as it is working with Hyperterminal.
Hyper Terminal settings are usually like:
If it has Flow Control as NONE then You don't need:
po.DtrEnable = true;
po.RtsEnable = true;
I don't find use of Setting encoding.
Most important thing You are forgetting is Add "\r" at the end of Any AT Command! Seems you haven't read AT Command list!

private void button1_Click(object sender, EventArgs e)
{
SerialPort po = new SerialPort();
po.PortName = "COM10";
po.BaudRate = int.Parse("9600");
po.DataBits = Convert.ToInt32("8");
po.Parity = Parity.None;
po.StopBits = StopBits.One;
po.ReadTimeout = int.Parse("300");
po.WriteTimeout = int.Parse("300");
po.Encoding = Encoding.GetEncoding("iso-8859-1");
po.Open();
po.DtrEnable = true;
po.RtsEnable = true;
//po.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
// po.Write("ATD01814201013;");
po.WriteLine("ATD01"+textBoxPhoneNumber.Text+";"+Environment.NewLine);
}

Related

How do I access COM-Port with C#?

Here the serial communication port shows error it cannot be accessed .....But the serial communication port works perfect in arduino, so it cant be the port problem, its not the driver problem either, the driver is updated and works well, so the problem can be in the code .....i am a newbie to C#.
public partial class Form1 : Form
{
private SerialPort myport;
private string in_data;
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
myport = new SerialPort();
myport.BaudRate = 19200;
myport.PortName = pn.Text;
myport.Parity = Parity.None;
myport.DataBits = 8;
myport.StopBits = StopBits.One;
myport.DataReceived += myport_DataReceived;
try
{
myport.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!!");
}
}
void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
*in_data = myport.ReadLine();***
this.Invoke(new EventHandler(displaydata_event));
}
private void displaydata_event(object sender, EventArgs e)
{
string[] newData = in_data.Split(',');
bv.Text = newData[0];
bi.Text = newData[1];
pv.Text = newData[2];
pi.Text = newData[3];
t.Text = newData[4];
}
}
it cannot be accessed
This may indicates that the port doesnt exists or is already in use.
Maybe another application is already listening on this port. (arduino?)
Comport.Open() Exceptions are descibed here:
MSDN SerialPort.Open Method

Continual Read Serial Data To Textbox

I have the following C# program with a button called GetForceButton and a multiline textbox called ForceTextbox. Here is the code I have at the moment:
public Form1()
{
InitializeComponent();
System.ComponentModel.IContainer components = new System.ComponentModel.Container();
serialPort1 = new System.IO.Ports.SerialPort(components);
serialPort1.PortName = "COM7";
serialPort1.BaudRate = 9600;
serialPort1.DtrEnable = true;
serialPort1.Open();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
bool buttonpressed = false;
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadLine();
if (buttonpressed == true)
{
ForceTextbox.Text = indata + "\n";
}
else
{
ForceTextbox.Text = "No data received";
}
}
private void GetForceButton_Click(object sender, EventArgs e)
{
buttonpressed = true;
}
When I step through the code, indata is getting the value from the serialPort of "0.00\r" (including the speech brackets).
After stepping to the ForceTextbox.Text = indata + "\n"; line, an exception is being thrown up saying:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll. Additional information: Cross-thread operation not valid: Control 'ForceTextbox' accessed from a thread other than the thread it was created on.
What does that mean, or what am I doing wrong please?
You need to read this link.
The long and short of it is you need to make sure that you update GUI components on the same thread that started them. Mostly, this is done by the GUI thread.
You'll be using InvokeRequired as shown in that link.
You have to do this in C# all over the place unfortunately.
One other tutorial from Microsoft.

Serial Port Programming Help in C#

Recently i want to connect and read data from Serial Port Using SerialPort Class in C#. I have a time attendance machine named RTA600 from Hundura. it is connected to my PC via serial-to-USB convertor. the code i have tested as follows
SerialPort serialPort1 = new SerialPort();
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.Parity = System.IO.Ports.Parity.None;
serialPort1.DataBits = 8;
serialPort1.StopBits = System.IO.Ports.StopBits.One;
serialPort1.Handshake = System.IO.Ports.Handshake.None;
serialPort1.RtsEnable = true;
// Open the Serial Port
serialPort1.Open();
// Read Data from Serial port
string RXstring = serialPort1.ReadLine();
I use SerialPort Class of System.Io.Ports namespace. But it code hangs when serialPort1.ReadLine() executes. Can Some give me a idea how can i read the data from by time machine through serial-to-usb convertor
You can use ReadExisting() to read the whole data at a time.
You need to handle DataReceived Event of SerialPort
serialPort1.ReadExisting();
Sample:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
String myData=serialPort1.ReadExisting();
}
Example Code: Here i would like to show you the code to Read Data(RFID Tag Code which is basically of length 12)
String macid = "";
private void DoWork()
{
Invoke(
new SetTextDeleg(machineExe ),
new object[] { macid });
macid = "";
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string str1;
macid += serialPort1.ReadExisting();
if (macid.Length == 12)
{
macid = macid.Substring(0, 10);
Thread t = new Thread(new ThreadStart(DoWork));
t.Start();
}
}
public void machineExe(string text)
{
TextBox1.Text=text;
}

Receive SMS through GSM modem

I saw that similar topics were posted on this forum, but I simply don't understand how to send AT commands and receive a response. (I started to program in C# several months ago. I'm still an n00b, but I'm working hard to learn it...).
I need to create application which would only receive SMS message through GSM USB dongle. So far I managed to create app that will recognize and connect modem through COM ports that is available. Now I need to push AT commands for receiving messages and displaying them into a textBox. I was wondering if anyone can spare few minutes to explain the process to me, and modify my code with comments so I can finally learn and understand how to use serialPort for communication. What I need to know, when SMS is sent, does this message is received and stored by GSM modem (and it is stored until I send some requests to read them or do I need to send some event that would trigger GSM modem to collect message from ISP)? how to push AT commands and receive their response (I only know that is done by using serialPort object, but doesn't have clue how to do it...)
This is my method for receiving (which I'm stuck BTW... :))
private void receiveMessage()
{
//commclass is only a class for getting COM port, baud rate and timeout
CommClass cc = new CommClass();
cc.setParameters();
serialPort1.PortName = cc.getPort();
serialPort1.BaudRate = cc.getBaud();
serialPort1.ReadTimeout = cc.getTimeout();
serialPort1.Open();
if (!serialPort1.IsOpen)
{
//MessageBox is written in Croatian language, it is only an alert to check the configuration because port is not opened...
MessageBox.Show("Modem nije spojen, molimo provjerite konfiguraciju...!");
//timer1.Stop();
}
else
{
//this.label2.Text = serialPort1.PortName;
//this.label2.Visible = true;
//this.label3.Visible = true;
//this is where I need to place a code for receiving all SMS messages
this.serialPort1.Write("AT+CMGL=\"REC UNREAD\"");
}
serialPort1.Close();
}
If anyone willing to help, I would appreciate that, if not I would have to deal with it by my self (probably spent few hours/days until I figure it out...)
In both cases, thank you anyway... Cheers.
Sorry for waiting for my reply, been busy lately.
In short this is my code for getting message from GSM USB dongle. I hope that it will be useful to someone...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace SMSget
{
public partial class SMSLogPanel : UserControl
{
SerialPort sp;
int datab = 0;
bool dtr = false;
bool encod;
Handshake h;
Parity p;
int wtimeout = 0;
StopBits s;
#region default constructor
public SMSLogPanel()
{
InitializeComponent();
this.sp = serialPort1 = new SerialPort();
this.datab = serialPort1.DataBits = 8;
this.dtr = serialPort1.DtrEnable = true;
this.encod = serialPort1.Encoding.Equals("iso-8859-1");
this.h = serialPort1.Handshake = Handshake.RequestToSend;
this.p = serialPort1.Parity = Parity.None;
this.wtimeout = serialPort1.WriteTimeout = 300;
this.s = serialPort1.StopBits = StopBits.One;
checkLink();
}
#endregion
#region checking communication and setting user controls...
private void checkLink()
{
GetValues value = new GetValues();
string com = value.getPort();
int baud = value.getBaud();
int timeot = value.getTimeout();
serialPort1.PortName = com;
serialPort1.BaudRate = baud;
serialPort1.ReadTimeout = timeot;
serialPort1.Open();
if (serialPort1.IsOpen)
{
label1.Visible = true;
}
else
{
MessageBox.Show("Komunikacija sa modemom se ne može uspostaviti, molimo postavite novu konfiguraciju...!");
this.Controls.Clear();
SMSConfigPanel cfg = new SMSConfigPanel();
cfg.Show();
this.Controls.Add(cfg);
}
serialPort1.Close();
}
#endregion
#region panel load method
private void SMSLogPanel_Load(object sender, EventArgs e)
{
setGSM();
}
#endregion
#region execute serialport handler
public void getMessage()
{
if (serialPort1.IsOpen)
{
serialPort1.DataReceived += new SerialDataReceivedEventHandler(getResponse);
}
else
{
MessageBox.Show("Nije moguće zaprimiti poruku, komunikacijski port nije otvoren...1");
return;
}
}
#endregion
#region get response from modem
public void getResponse(object sender, SerialDataReceivedEventArgs e)
{
SerialPort serPort = (SerialPort)sender;
string input = serPort.ReadExisting();
if (input.Contains("+CMT:"))
{
if (input.Contains("AT+CMGF=1"))
{
string[] message = input.Split(Environment.NewLine.ToCharArray()).Skip(7).ToArray();
textBox1.Text = string.Join(Environment.NewLine, message);
}
this.Invoke((MethodInvoker)delegate
{
textBox1.Text = input;
});
}
else
{
return;
}
}
#endregion
#region initialize GSM
private void setGSM()
{
serialPort1.Open();
if (!serialPort1.IsOpen)
{
MessageBox.Show("Problem u komunikaciji sa modemom, port nije otvoren...!");
}
serialPort1.Write("AT+CMGF=1" + (char)(13));
serialPort1.Write("AT+CNMI=1,2,0,0,0" + (char)(13));
}
#endregion
#region setiranje timer-a...
private void timer1_Tick_1(object sender, EventArgs e)
{
timer1.Stop();
getMessage();
timer1.Start();
}
#endregion
}
}
This was only code for testing so it works but there is lot to fix and improve. Basically it will be a nice start for all that are searching something like this...
cheers.

trying to read data from serial port on button click event in C# winform?

I have a problem on reading data from serial-port in my winform application...here is my code ..
private void rtrvBtn_Click(object sender, EventArgs e)
{
btnid = 2;
mySerialPort = new SerialPort(port);
mySerialPort.Open();
compacket(btnid);
if (combuffer[0] != 0)
{
mySerialPort.Write(combuffer, 0, 4);
System.Threading.Thread.Sleep(500);
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(this.port_rec);
}
else
{
lblmsg.Text = "FL";
}
mySerialPort.Close();
rtrvBtn.Enabled = false;
conBtn.Enabled = true;
}
public void port_rec(object sender, SerialDataReceivedEventArgs e)
{
string s = Convert.ToString(mySerialPort.ReadExisting());
MessageBox.Show(s);
}
so here i am not getting any data and i am not also entering the port_rec event also.. can any one help ... and my serial-port settings are like this ....
public void SettingRS232(string port)
{
try
{
mySerialPort = new SerialPort(port);
mySerialPort.PortName = port;
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.ReadTimeout = 2000;
mySerialPort.WriteTimeout = 500;
mySerialPort.DtrEnable = true;
mySerialPort.RtsEnable = true;
}
catch (Exception ex)
{
lblmsg.Text = ex.Message;
}
}
help me guys
I would guess that these two methods are in the same class with a mySerialPort field. You shouldn't be making a new serial port object in the click event, you should be using the one you already have.

Categories

Resources