Link to home
Start Free TrialLog in
Avatar of jaykhatri
jaykhatriFlag for India

asked on

Error Sending SMS using PDU mode

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
Avatar of BigRat
BigRat
Flag of France image

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.
Avatar of jaykhatri

ASKER

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

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!)
       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.

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

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
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
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.
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
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?
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.
I suspect there is problem in chr(26). Should it be done exactly what i am doing. I am in doubt.
>>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?
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.
And yes since everything was not working I used pduspy software to get the pdu string.
please let me know if you need more details.
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i tried replacing writeline with write bu still error is coming. Do I need to replace anything else also.
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