Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error Sending SMS using PDU mode

Posted on 2011-02-17
20
Medium Priority
?
1,429 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This program is used to assist in finding and resolving common problems with wireless connections.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

963 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