Solved

Error Sending SMS using PDU mode

Posted on 2011-02-17
20
1,406 Views
Last Modified: 2013-12-09
Hi Experts,

I have been trying to send the message this way:

            Dim firstarg As Integer = Convert.ToInt32(pdudata.Substring(0, 2))
            firstarg = firstarg * 2
            tmp = pdudata.Substring(2)
            tmp = tmp.Substring(firstarg)
            firstarg = tmp.Length \ 2
            port.WriteLine("AT+CMGF=0" & vbCrLf)
            Thread.Sleep(500)
            read = New Byte(port.BytesToRead - 1) {}
            port.Read(read, 0, read.Length)
            'txtsms.Text += Encoding.ASCII.GetString(inbuf);
            tmp = port.ReadExisting()
            port.WriteLine("AT+CMGS=" & Convert.ToString(19) & vbCrLf)
            Thread.Sleep(500)
            MsgBox(port.ReadExisting())
            port.WriteLine("0011000A911909101535700000AA05E8329BFD06" & Chr(26) & vbCrLf) 'I used constant value for testing purpose
            MsgBox(port.ReadExisting()) 'Here port shows 'ERROR'
            read = New Byte(port.BytesToRead - 1) {}
            port.Read(read, 0, read.Length)


Can you suggest where I am wrong, SMS is going using Text Mode.

Please provide me the solution at the earliest.

Best Regards

Jaidev Khatri
0
Comment
Question by:jaykhatri
  • 14
  • 5
20 Comments
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
What does MsgBox(port.ReadExisting()) do?

I ask because with Echo on the phone will reply to  port.WriteLine("AT+CMGS=" & Convert.ToString(19) & vbCrLf) withe the command you sent, plus CR/LF plus OK plus CR/LR and these characters are NOT send all at once but bit by bit, so if you are too fast you think you have read all the messages but there's still some there.

The correct way to prgram this is 1) turn echo off and 2) check EVERY response form EVERY command for errors and 3) read data UNTIL you get CR/LF and then, and only then, look at what you've got.

I suspect that ReadExisting just reads what is in the buffer. It probably does NOT read until it finds the CR/FL sequence.
0
 

Author Comment

by:jaykhatri
Comment Utility
MsgBox(port.ReadExisting())  --> show like this > (this is what we see in hyperterminal after we send CMGF)

Ok I did some changes. Hope these are significant to your suggestion

            Dim firstarg As Integer = Convert.ToInt32(pdudata.Substring(0, 2))
            firstarg = firstarg * 2
            tmp = pdudata.Substring(2)
            tmp = tmp.Substring(firstarg)
            firstarg = tmp.Length \ 2
            port.WriteLine("ate0" & vbCrLf)
            port.WriteLine("AT+CMGF=0" & vbCrLf)
            Thread.Sleep(500)
            read = New Byte(port.BytesToRead - 1) {}
            port.Read(read, 0, read.Length)
            'txtsms.Text += Encoding.ASCII.GetString(inbuf);
            tmp = port.ReadExisting()
            port.WriteLine("AT+CMGS=" & Convert.ToString(19) & vbCrLf)
            Thread.Sleep(500)
            port.Write("0011000A911909101535700000AA05E8329BFD06" & Chr(26))
            Thread.Sleep(500)
            port.Write(Chr(26))
            read = New Byte(port.BytesToRead - 1) {}
            port.Read(read, 0, read.Length)

Still message is not going.

Please check and can u suggest me exactly where to put change.

Thanks & Best Regards

Jaidev Khatri

0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
1) Send an ATZ command first.
2) You need a read routine which construct a buffer. You then read bytes into the buffer. You then search for a CR/LF and if so return the buffer up to the CR/LF. You delete that portion plus the CR/LF and the next time you append the data onto the end of that in the buffer. If you don't find CR/LF in the buffer, you wait 500 ms, try again and if these is no data error.
3) Then after each command you check that the read routine returns OK, if not error message and give out what you have read.
4) After an OK you should get the command prompt which is a > and after sending a command I think you also get a CR/LF. My code, which is not in VB, ignores blank or empty lines. Some phones/modems sometimes sends extra blank lines, so that's a handy feature anyway.

PS: I hope you have initialised the Port correctly. I actually use 9600 Baud, because some phones can't take the speed (in fact when using Hyperterminal to test the baud rate is almost zero!)
0
 

Author Comment

by:jaykhatri
Comment Utility
       Dim prt As New System.IO.Ports.SerialPort("com8", 9600, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
 this is how I am configuring the port.

I tried using ATZ to reset modem. But nothing its not working.

0
 

Author Comment

by:jaykhatri
Comment Utility
OK, I tried the same thing simply in hyper terminal.

Thats has given an error.

I tried this

AT
AT+CMGF=0
AT+CMGS=19
>0011000A911909101535700000AA05E8329BFD06 <ctrl z>

ERROR

Doing it programatically is the next milestone, but its still not working even in hyperterminal.
0
 

Author Comment

by:jaykhatri
Comment Utility
Hello I tried using this PDU in hyper terminal and it worked for me.

But when I try the same thing in VB.Net 2005, it is not working, I tried all the combinaton of Thread.Sleep. It is not working.

Please let me know how can I achieve this.

0
 

Author Comment

by:jaykhatri
Comment Utility
The Last working PDU in hyperterminal is this = 0021000C91190910153570000002E834

So I made changes in my code like that

            port.WriteLine("AT+CMGF=0" & vbCrLf)
            MsgBox(port.ReadExisting)
            port.WriteLine("AT+CMGS=15" & vbCrLf)
            MsgBox(port.ReadExisting)
            Thread.Sleep(5000)
            port.WriteLine("0021000C91190910153570000002E834" & Chr(26))
            Thread.Sleep(10000)
            'read = New Byte(port.BytesToRead - 1) {}
            'port.Read(read, 0, read.Length)

Not only this, I tried all the combinations, but still no success. Please help me out. I am sure the problem is in the line  port.WriteLine("0021000C91190910153570000002E834" & Chr(26))

 I even tried to baud rate 9600, even tried to lower it to even 100. But still its shows 'ERROR'.

Can u please figure out the problem and let me have the exact change so that I may send sms from it.

Thanks & Best Regards

Jaidev Khatri
0
 

Author Comment

by:jaykhatri
Comment Utility
Hi BigRat,

As far as I understood by your post is to wait until port send the OK message.

I did the same and there is always a OK message, instead of the last statement that is >PDUcode and ctrl z

I will be thankful if you can send the code in vb.net or c#.net.

Best Regards

Jaidev Khatri
0
 
LVL 13

Expert Comment

by:imarshad
Comment Utility
So you can send the SMS using the following in hyper terminal?
AT+CMGF=0 <Enter>
AT+CMGS=15 <Enter>
>0021000C91190910153570000002E834 <Ctrl-z>

and the modem replies with OK code. and the message is sent?

If yest then you can try to replace vbCrLf with VbCr. in your code.
Also you should first test sending via Text mode.
0
 

Author Comment

by:jaykhatri
Comment Utility
Thank you very much for your reply imarshad,

I tested the same code with text mode and its running very fine. Regarding vbcrlf replacement. I have already tried with all the combinations of it vbcr, vbcrlf, vblf everything. I was just thinking that I am not giving enough time to the connection to send the sms so I did wait upto 10000.

Still I have no clue whats wrong with my code. In my Msgbox its shows ERROR.

No description of Error so that I may trace accordingly.

Best Regards

Jaidev Khatri
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 27

Expert Comment

by:BigRat
Comment Utility
Two things.

First AT or ATZ ought to be sent to reset the device.

Second, when a message is sent one gets a CMGS: reply with the message number. Do you see such a reply on Hyperterminal?
0
 

Author Comment

by:jaykhatri
Comment Utility
Yeah i am resetting the device.

Second as far as I understood your query means : When all is complete and I press ctrl+z. I get message on hyperterminal like '+cms' and some digit.
0
 

Author Comment

by:jaykhatri
Comment Utility
I suspect there is problem in chr(26). Should it be done exactly what i am doing. I am in doubt.
0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
>>I get message on hyperterminal like '+cms' and some digit.

Yes, actually the number counts up, so after each message you next the next number.

>>I suspect there is problem in chr(26)

No 26 is Hex 1A and that is CTRL/Z since Z is hex 5A and the control takes away hex 40.

Would you care to post the ENTIRE code for the message sending?
0
 

Author Comment

by:jaykhatri
Comment Utility
This is the total code:


        Public Sub Senddata(ByVal port As SerialPort, ByVal pdudata As String)
            Dim firstarg As Integer = Convert.ToInt32(pdudata.Substring(0, 2))
            firstarg = firstarg * 2
            tmp = pdudata.Substring(2)
            tmp = tmp.Substring(firstarg)
            firstarg = tmp.Length \ 2
            port.WriteLine("atz" & vbCr)
            port.WriteLine("ate0" & vbCr)
            port.WriteLine("AT+CMGF=0" & vbCr)
            read = New Byte(port.BytesToRead - 1) {}
            port.Read(read, 0, read.Length)
            'txtsms.Text += Encoding.ASCII.GetString(inbuf);
            tmp = port.ReadExisting()
            port.WriteLine("AT+CMGS=15" & vbCr)
            Thread.Sleep(1000)
            port.WriteLine("0021000C91190910153570000002E834" & Chr(26))
            Thread.Sleep(5000)
            port.DiscardOutBuffer()
            'read = New Byte(port.BytesToRead - 1) {}
            'port.Read(read, 0, read.Length)
        End Sub

and the port object is this:

        Dim prt As New System.IO.Ports.SerialPort()
        With prt
            .PortName = "com8"
            .BaudRate = 19200
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .Handshake = Handshake.RequestToSend
            .DtrEnable = True
            .RtsEnable = True
            .NewLine = vbCrLf
        End With
        prt.Open()
        send.Senddata(prt, sendmsg) //here sendmsg is string but I am not using this string in  the message
        prt.Close()

I am using vs 2005 with Dot Net Framework 2.0 and using VB.Net Language.
0
 

Author Comment

by:jaykhatri
Comment Utility
And yes since everything was not working I used pduspy software to get the pdu string.
0
 

Author Comment

by:jaykhatri
Comment Utility
please let me know if you need more details.
0
 
LVL 27

Accepted Solution

by:
BigRat earned 500 total points
Comment Utility
This command :-

port.WriteLine("AT+CMGS=15" & vbCr)


Actually writes AT+CMGS=15 CR/LF CR/LF. This is incorrect, since after the first CR/LF the prompt appears and the phone wants a PDU and NOT a CR/LF.

Replace all calls to wtiteline with write. Where a CR/LF is explicity required we shall append one, otherwise none shall be sent.
0
 

Author Comment

by:jaykhatri
Comment Utility
i tried replacing writeline with write bu still error is coming. Do I need to replace anything else also.
0
 

Author Comment

by:jaykhatri
Comment Utility
Thank you very very much BigRat, I tried something more after replacing with write.

I did
port.WriteLine("AT+CMGS=15" & vbLf)
instead of
port.WriteLine("AT+CMGS=15" & vbCr)

Just send the first sms from this program.

Thank you very very much.

If i will have any query I will return but obviously with new question :)

Jaidev Khatri
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 48
API Soap Calls 4 56
Installed softare without GUID 6 53
advertisement module in core php 4 81
Using in-flight Wi-Fi when you travel? Business travelers beware! In-flight Wi-Fi networks could rip the door right off your digital privacy portal. That’s no joke either, as it might also provide a convenient entrance for bad threat actors.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now