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.
Related
I'm creating a software in C # that must read the serial port and when recognizing a String (String already known sent by the arduino with BaudRate 38400) it must inform the baudRate used in the reading.
My problem is that no matter what value I put in C # BaudRate, it still recognizes the data correctly.
I tried to change the baudRate on the Arduino, but the result is always the same.
For example, the arduino sends the word "Hello" to a baudRate of 38400. The C # should read the serial port and when recognizing the word Hello (which should only appear with the correct BaudRate) it should display the used baudRate and end the function . However, even setting the baud rate to 1200, 2400 ... etc, ... C # always reads the word "Hello".
On the arduino serial monitor, when I change the speed, the characters are scrambled when I select a different speed than the one configured (as it should happen in C #).
I need C # to receive the scrambled serial data when the baudRate is incorrect.
Snippet of C# code:
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;
using System.IO.Ports;
namespace Baud
{
public partial class frmSerial : Form
{
public static System.IO.Ports.SerialPort serialPort1;
//private delegate void LineReceivedEvent(string line);
public frmSerial()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
Int32[] lista_bauds = new Int32[] { 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 };
Boolean connected = false;
System.ComponentModel.IContainer components = new System.ComponentModel.Container();
serialPort1 = new System.IO.Ports.SerialPort(components); // Creating the new object.
for (int i = 0; i < lista_bauds.Length & connected == false; i++)
{
Console.WriteLine(lista_bauds[i]);
string ReceivedData = "";
serialPort1.PortName = "COM" + numCom.Value.ToString(); // Setting what port number.
serialPort1.BaudRate = 1200; // Setting baudrate.
serialPort1.ReadTimeout = 4000;
//serialPort1.DtrEnable = true; // Enable the Data Terminal Ready
serialPort1.Open(); // Open the port for use.
try
{
if (serialPort1.IsOpen == true)
{
ReceivedData = serialPort1.ReadExisting();
Console.WriteLine(ReceivedData);
if (ReceivedData.Equals("Hello") == true)
{
txtDatasend.Text = "Conectado com BaudRate de" + lista_bauds[i]; ;
Console.WriteLine(ReceivedData);
connected = true;
btnConnect.Text = "Conectar";
serialPort1.Close();
}
else
{
serialPort1.Close();
}
}
}
catch (Exception)
{
throw;
}
}
numCom.Enabled = false;
}
private void btnSend_Click(object sender, EventArgs e)
{
// Sends the text as a byte.
serialPort1.Write(new byte[] { Convert.ToByte(txtDatasend.Text) }, 0, 1);
}
}
}
Heres the arduino code:
int LED_Pin = 13; // LED connected to digital pin 13
void setup()
{
Serial.begin(38400); // Configure the baudrate and data format for serial transmission
pinMode(LED_Pin,OUTPUT); // Pin13 as Output
digitalWrite(LED_Pin,LOW); // Pin13 LED OFF
}
void loop()
{
Serial.println("Autenticar"); // Send the String,Use Serial.println() because we have used SerialPort.ReadLine() on C# side
// SerialPort.ReadLine() on C# side will return only after receiving '/n' character
Blink_LED(); // Blink LED to indicate transmission
}
//Function to Blink LED 13
void Blink_LED()
{
digitalWrite(LED_Pin,HIGH);
delay(100);
digitalWrite(LED_Pin,LOW);
delay(100);
}
I currently have a program made using VB6 code that uses the MSCOMM control to pull back data from the serial port. This manages to successfully receive the data from my serial port, in which a Denso BHT-904B device is connected.
I am now trying to move this code over to C# so it fits in with a new piece of software that i am developing. To do this i am using the SerialPort class. However, the issue is that when i open the port up the data received event only fires when the device fails to communicate (which im guessing is due to a timeout). The data then received in the event is '↑↑↑↑↑'.
My SerialPort control settings are the following:
DtrEnable = True
PortName = COM3
ReadBufferSize = 1024
WriteBufferSize = 512
The code that i am using behind my form control is:
namespace BHTTestingDotNet
{
public partial class Form1 : Form
{
private string rxString;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
serialPort.DtrEnable = true;
serialPort.Encoding = Encoding.Default;
serialPort.DataReceived += serialPort_DataReceived;
serialPort.ErrorReceived += serialPort_ErrorReceived;
serialPort.Open();
}
private void serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
MessageBox.Show(e.ToString());
}
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
var serialPort = (SerialPort)sender;
var test = serialPort.BytesToRead;
SerialPort sr = (SerialPort)sender;
rxString = sr.ReadExisting();
this.BeginInvoke(new EventHandler(displayText));
}
private void displayText(object o, EventArgs e)
{
txtBHT.AppendText(rxString);
}
}
}
I have already tried to set both RtsEnable and DtrEnable to true but that didn't make any difference.
UPDATE - I have now changed to protocol settings on the device but i now only receive pipes and then a return symbol, for example like so:
|||||¬
I am using SerialPort class often and for my purposes I have made my own class
public class SerialPortDataSource : SerialPort
where SerialPort.DataReceived handler invoke this method:
private void SerialPortDataSource_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (BytesToRead > 0)
{
var buffor = new byte[BytesToRead];
Read(buffor, 0, buffor.Length);
_receivedBytes = buffor;
//wConsole.WriteLine(ArrayExtension.ToString(buffor));
var dataLogger = DataLogger;
if (dataLogger != null)
{
dataLogger.WriteLine("- DR - {0}", true, BitConverterExtension.ToHexString(buffor));
}
if (OnDataReceived != null)
{
OnDataReceived(this, buffor);
}
}
}
catch (InvalidOperationException)
{
// sometimes DataReceived event is invoked after port is closed which causes InvalidOperationException
}
}
This method is working for me in many applications with variety serial port settings.
I'm experiencing this problem while opening a serial port in C# (which should be dirt simple; or so I thought).
When I try to open a serial port, I am getting the following exception:
The semaphore time-out period has expired.
Here is the method that is doing this.
public static void Open_TheActivePortWeWillUse(String Drone_StringNameFromUser)
{
var TempSerialPort = new SerialPort (
Drone_StringNameFromUser,
(int) SerialPortParameter.TheSerialPortSpeed);
// Now we have a name that anybody can see and use
OurSpecificPorts.TheActivePortWeAreUsing = TempSerialPort;
// We'll do 8-N-1 since almost the whole planet does that
OurSpecificPorts.TheActivePortWeAreUsing.DataBits = 8;
// We'll do 8-N-1
OurSpecificPorts.TheActivePortWeAreUsing.Parity = Parity.None;
// We'll do 8-N-1
OurSpecificPorts.TheActivePortWeAreUsing.StopBits = StopBits.One;
OurSpecificPorts.TheActivePortWeAreUsing.DataReceived +=
OurBackGroundSerialPortReceiver;
// We can now open our active port, which is what this line does
OurSpecificPorts.TheActivePortWeAreUsing.Open();
}
The strangest thing for me is that I get this error inconsistently. Half the time it works okay, and the other half, it does not.
Does anybody see anything obviously wrong with my code? Am I missing something?
Declaring your SerialPort object inside the method will not allow access to it after the method closes. Here is a method that works to open the port:
private void OpenSerialPort(String portName)
{
try
{
serialPort1.Close();
serialPort1.PortName = portName;
serialPort1.BaudRate = 115200;
serialPort1.DataBits = 8;
serialPort1.Handshake = Handshake.None;
serialPort1.Parity = Parity.None;
serialPort1.RtsEnable = false;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
}
catch (Exception ex)
{
MessageBox.Show("Could not open serial port " + portName, "Error");
}
}
The SerialPort object is declared in the class:
namespace Arcadia
{
public partial class Form1 : Form
{
private SerialPort serialPort1;
And the callback is added in the constructor:
public Form1()
{
InitializeComponent();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(this.SerialPortReadCallback);
Sending data is handled in a different method:
private void SerialPortWrite(String writeString)
{
if (serialPort1.IsOpen)
{
serialPort1.WriteLine(writeString);
}
}
And here is the received data callback:
private void SerialPortReadCallback(object sender, SerialDataReceivedEventArgs args)
{
try
{
while (serialPort1.BytesToRead > 0)
{
// Do something with the data
}
}
catch (Exception ex)
{
}
}
i have a serial port that will iterate through the ports with this method:
foreach (string s in SerialPort.GetPortNames())
{
var serialOneOfMany = new SerialPort(s, baudRate, Parity.None, 8, StopBits.One);
if (serialOneOfMany.IsOpen)
{
serialOneOfMany.Close();
}
else
{
try
{
serialOneOfMany.Open();
}
catch
{
var openSerial = new System.Timers.Timer(3100);
openSerial.Elapsed += (o, e) =>
{
serialOneOfMany.Open();
openSerial.Enabled = false;
openSerial.Dispose();
};
openSerial.Enabled = true;
}
}
if (serialOneOfMany.IsOpen)
{
string received;
try
{
lblPortNum.Content = s;
lblPortNum.Refresh();
serialOneOfMany.Write(testMessage);
serialOneOfMany.DataReceived += new SerialDataReceivedEventHandler(testSerialPort_DataReceived);
}
catch (TimeoutException e)
{
serialOneOfMany.Close();
continue;
}
}
}
so, i want to open the port, send it a message, listen for the response, then close it. as everyone knows, every comport found in GetPortNames isn't a valid serial port. so, what i've been doing is setting a timer with a dispatcher timer:
DispatcherTimer time = new DispatcherTimer();
time.Interval = TimeSpan.FromMilliseconds(3000);
time.Tick += new EventHandler(someEventHandler);
time.Start();
here's the other method handled here:
private void someEventHandler(Object sender, EventArgs args)
{
SerialPort serial = (SerialPort)sender;
if (serial.IsOpen)
serial.Close();
serial.Dispose();
//if you want this event handler executed for just once
DispatcherTimer thisTimer = (DispatcherTimer)sender;
thisTimer.Stop();
}
so, it'll open the com port, if it doesn't get a response within 3 seconds, it will close the port. the problem i'm having is that the foreach loop will just barrel through the code and open the comport several times, i'll get a message saying The COM Port is open already and can't be used. so basically it's not pausing in openSerial.
i want it to open a new serial port, and if it's not accessible, wait 3100 milliseconds and try again. how do i do that?
UPDATED CODE:
private void button1_Click(object sender, RoutedEventArgs e)
{
CheckPorts();
}
private void checkPorts()
{
SendMessage("messageToDevice1", 19200);
SendMessage("Message2", 9600);
}
private void SendMessage(string testMessage, int baudRate)
{
int baudRate = 9600;
string testMessage = "test";
txtPortName.Text = "Testing all serial ports";
foreach (string s in SerialPort.GetPortNames())
{
SerialPort newPort = new SerialPort(s, baudRate, Parity.None, 8, StopBits.One);
if (!newPort.IsOpen)
{
try
{
newPort.Open();
}
catch { }
}
if (newPort.IsOpen)
{
openPorts.Add(newPort);
newPort.Write(testMessage);
newPort.DataReceived += new SerialDataReceivedEventHandler(serialOneOfMany_DataReceived);
}
else
{
newPort.Dispose();
}
}
txtPortName.Text = "Waiting for response";
tmrPortTest.Enabled = true;
}
my new problem is that it just blows through the com ports, i need it to stop for each one, take a second to listen, then close it. it just blows through the foreach loop.
now, the reason why i don't just open up the port and keep it open through all the messages is that my devices have different baud rates, and i can't adjust them to all match. so, i need to open the ports, then send messages, listen, if they don't respond to the first round of messages, then open them up at the new baudrate and send a new batch of messages. but the foreachloop doens't pause for me to listen.
I think this more or less agrees with rare's answer. The port where you receive a response (you would probably want to check the response as well) will remain open and all the others should close.
private List<SerialPort> openPorts = new List<SerialPort>();
private void button3_Click(object sender, EventArgs e)
{
int baudRate = 9600;
string testMessage = "test";
txtPortName.Text = "Testing all serial ports";
foreach (string s in SerialPort.GetPortNames())
{
SerialPort newPort = new SerialPort(s, baudRate, Parity.None, 8, StopBits.One);
if (!newPort.IsOpen)
{
try
{
newPort.Open();
}
catch { }
}
if (newPort.IsOpen)
{
openPorts.Add(newPort);
newPort.Write(testMessage);
newPort.DataReceived += new SerialDataReceivedEventHandler(serialOneOfMany_DataReceived);
}
else
{
newPort.Dispose();
}
}
txtPortName.Text = "Waiting for response";
tmrPortTest.Enabled = true;
}
private void serialOneOfMany_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
txtPortName.Text = ((SerialPort)sender).PortName;
}
private void tmrPortTest_Tick(object sender, EventArgs e)
{
tmrPortTest.Enabled = false;
foreach (SerialPort port in openPorts)
{
if (port.PortName != txtPortName.Text)
{
port.Close();
port.Dispose();
}
}
}
Here's how I would do this --
First, try to open all the serial ports. The ones that actually do open are put in a list.
Assign all serial ports in the list to the same DataReceived event handler. The event handler is where you will save the port name (it's in the args) and kill the timer if you rx'd the response
Send your testMessage out all the open ports
Set just one timer for 3.1 seconds
Close the ports once the timer fires or the event handler rx's the response.
HI
im new in c# serial port. im writing a c# program running is winXP and win7 to keep received data from the serial port when the machine was sent data.
using System.IO;
using System.IO.Ports;
using System.Threading;
namespace RS232RVR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SettingRS232();
}
public void SettingRS232 ()
{
try
{
SerialPort mySerialPort = new SerialPort("COM6");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None; //send to hardware flow control.
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);
mySerialPort.Open();
richTextBox1.Text = "on";
mySerialPort.Close();
}
catch (Exception ex)
{
richTextBox1.Text = ex.Message;
}
}
private void DataReceviedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
richTextBox1.Text = indata;
}
}
}
COM6 is active in my pc. but my problem was seem the datareceived event is not fire when it has data coming from the serial port. ( i had checked the sport by using some of the freeware application)
anyone can help?
thanks
mySerialPort.Open();
richTextBox1.Text = "on";
mySerialPort.Close();
That's not going to work, you'll close the serial port a couple of microseconds after opening it. Yes, the DataReceived event handler is not likely to fire. Only close the port when shutting down your program.
mySerialPort.Handshake = Handshake.None
That's a problem too, you'll need to control the handshake signals yourself now. The vast majority of serial port devices won't send anything until they see the machine powered up and ready to receive. Set the DtrEnabled and RtsEnabled properties to true.
Did you copy that code from your application? Is it perhaps just a case that the event handler name is misspelled? E.g. DataReceviedHandler should actually be spelt DataReceivedHandler.
The problem was solved and i would like to share it. i had fine dunning as below:
namespace RS232RVR
{
public partial class Form1 : Form
{
private delegate void SetTextDeleg(string data);
public Form1()
{
InitializeComponent();
SettingRS232();
}
public void SettingRS232 ()
{
try
{
SerialPort mySerialPort = new SerialPort("COM6");
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;
mySerialPort.Open();
//mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.DataReceived += DataReceivedHandler;
textBox1.Text = "Serial Port is Ready.";
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
}
public void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
System.Threading.Thread.Sleep(500);
string indata = sp.ReadExisting();
this.BeginInvoke(new SetTextDeleg(DisplayToUI), new object[] { indata });
//textBox1.Text += indata;
}
private void DisplayToUI(string displayData)
{
textBox1.Text += displayData.Trim();
// textBox1.Text += displayData;
}
}
}
If anyone have comment on the code you are welcome, is my pleasure and wish to code it better.