Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 21st, 2005, 01: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
======================================
Reply With Quote
  #2 (permalink)  
Old December 21st, 2005, 04: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.
======================================
Reply With Quote
  #3 (permalink)  
Old February 28th, 2006, 01: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.



Reply With Quote
  #4 (permalink)  
Old March 8th, 2006, 02: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

Reply With Quote
  #5 (permalink)  
Old March 8th, 2006, 02: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.
======================================
Reply With Quote
  #6 (permalink)  
Old March 8th, 2006, 03: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 :-(

Reply With Quote
  #7 (permalink)  
Old March 8th, 2006, 04: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.
======================================
Reply With Quote
  #8 (permalink)  
Old March 8th, 2006, 05: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 ...

Reply With Quote
  #9 (permalink)  
Old March 8th, 2006, 05: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 ...

Reply With Quote
  #10 (permalink)  
Old March 8th, 2006, 05: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.
======================================
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 10:53 PM
Usb Port? dedex C# 1 November 9th, 2004 05:14 AM



All times are GMT -4. The time now is 07:36 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.