Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 21st, 2005, 02:33 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default SMS Via USB Serial Port

Hi all,
I am currently creating an app to send sms message to GSM Modem (mobile phone) using System.IO.Ports.SerialPort.
I beleive I have correctly configured the port as I am able to send AT commands to the modem and get replies form the modem which tells me I am connected.
But my problem is using the AT commands themselves, I dont know if they are being sent as expected.
All the commands I am using work with Hyperterminal on the same GSM Modem/Phone.

The problem may be the escape character for the end of the message, in Hyperterminal it is CTRL+Z, which I have read is chr(26).

Or it could be the other AT commands themselves.

Can anyone see my error in this code:

Code:
private void OpenPort()
        {            
            port.PortName = "COM4";
            port.ReceivedBytesThreshold = 1;
            port.BaudRate = 115200;
            port.DtrEnable = true;
            port.DataReceived += new SerialDataReceivedEventHandler(OnCom4Receive); 
            port.Open();
        }
        private void SendMSG()
        {
            port.WriteLine("AT");
            port.WriteLine("AT+CMGF=1");
            port.WriteLine("AT+CMGS=\"0403075???\"");
            //port.WriteLine("> TESTING 1,2,3,4" + char.ConvertFromUtf32(26));
            port.WriteLine("TESTING 1,2,3,4" + Convert.ToChar(26));
        }
        static private void OnCom4Receive(object sender, SerialDataReceivedEventArgs e)
        {
            MessageBox.Show(port.ReadExisting());            
        }
There are not any errors returned at all.
Many thanks for your time in reading this.

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old December 21st, 2005, 05:30 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Fixed the problem, need to explicitly send carriage return and line feed
            port.WriteLine("AT\r");
            port.WriteLine("AT+CMGF=1\r");
            port.WriteLine("AT+CMGS=\"0403075???\"\r");
            //port.WriteLine("> TESTING 1,2,3,4" + char.ConvertFromUtf32(26));
            port.WriteLine("TEST" + '\x001a');
            //port.Write('\x001a'.ToString());


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old February 28th, 2006, 02:02 PM
Registered User
 
Join Date: Feb 2006
Location: , , India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very much happy to see the C# code to send SMS to GSM Modem using System.IO.Ports.SerialPort

We have a requirement to read all the received SMS from GSM Modem, need your help.



 
Old March 8th, 2006, 03:28 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey

i am constantly getting a Security exception when i am trying at Port.Open(). Can you help ?

can u send me this sample application for sending this SMS using AT Command.

Thanks

 
Old March 8th, 2006, 03:39 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Sorry rsika,
I never came across a secutiy exception.
The code in my first entry above with the amendments in the second will consitiute an application if you call the methods on a button click.
Accessing Ports is a security concern so you may need to have admin privilages or the like.
Are you an admin on the machine you are using?


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old March 8th, 2006, 04:40 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Rodam,

This is cool !! i was able to solve the security problem ..but still i am not able to send the SMS

I am able to do it via Hyperterminal though :-(

 
Old March 8th, 2006, 05:47 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Can you post the code you are using to send.

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old March 8th, 2006, 06:14 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

private void OpenPort()
        {

            port.PortName = "COM6";
            port.ReceivedBytesThreshold = 1;

            port.DtrEnable = true;
          // port.DataReceived += new SerialDataReceivedEventHandler(OnCom4Receive);
            if (!port.IsOpen)
                port.Open();
            else
            {
                port.Close();
                port.Open();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenPort();
            port.WriteLine("AT\r");
            port.WriteLine("AT+CMGF=1\r");
            string str = "AT+CMGS=\\" + textBox1.Text + "\\" + "\\r";
            port.WriteLine(str);
            port.WriteLine("TEST" + '\x001a');
        }

These are the code..i even tried hardcoding the number ...

 
Old March 8th, 2006, 06:15 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the textBox1.text is the phone number ....pls help ...

 
Old March 8th, 2006, 06:25 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

In this line you do not want the escape sequences
"AT+CMGS=\\" + textBox1.Text + "\\" + "\\r";
It should look like this
"AT+CMGS=\"" + textBox1.Text + "\"\r");
The double escape sequence actually writes a \ to the string which you dont want.
But you do need a double quote around the number so it requries an escape sequence befor it ie: \"
The rest looks pretty good.


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================




Similar Threads
Thread Thread Starter Forum Replies Last Post
USB Port kanoorani Beginning VB 6 8 August 12th, 2009 04:28 PM
Accessing USB and Serial Port through Java taraprasad Pro JSP 3 April 10th, 2008 06:20 AM
.Net problem with emulated USb to Serial com port Turbovulc General .NET 0 April 5th, 2007 06:18 AM
communicating with usb port Lookie VB How-To 1 March 2nd, 2005 11:53 PM
Usb Port? dedex C# 1 November 9th, 2004 06:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.