Solved

Error Sending SMS using PDU mode

Posted on 2011-02-17
20
1,409 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
ID: 34927843
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
ID: 34928238
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
ID: 34928404
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
ID: 34928738
       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
ID: 34931272
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
ID: 34934086
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
ID: 34935808
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
ID: 34935818
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
ID: 34948751
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
ID: 34948881
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 27

Expert Comment

by:BigRat
ID: 34952445
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
ID: 34952504
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
ID: 34952555
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
ID: 34953419
>>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
ID: 34953652
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
ID: 34953786
And yes since everything was not working I used pduspy software to get the pdu string.
0
 

Author Comment

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

Accepted Solution

by:
BigRat earned 500 total points
ID: 34959829
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
ID: 34959919
i tried replacing writeline with write bu still error is coming. Do I need to replace anything else also.
0
 

Author Comment

by:jaykhatri
ID: 34960051
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we have discussed about the OS X EI Capitan and how to fix Wi-Fi issue in OS X El Capitan. We have explained how to delete system level preferences and create a new Wi-Fi location to resolve Wi-Fi issue.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

939 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

2 Experts available now in Live!

Get 1:1 Help Now