Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
comport.WriteLine("AT");
System.Threading.Thread.Sleep(1000);
comport.WriteLine("AT+CMGF=0" + (char)13);
System.Threading.Thread.Sleep(1000);
string s2 = "AT+CMGS=" + messageLenght.ToString();
comport.WriteLine(s2 + (char)13);
System.Threading.Thread.Sleep(1000);
comport.WriteLine(pdu + (char)26 );
System.Threading.Thread.Sleep(5000);
MessageBox.Show(comport.ReadExisting());
comport.Close();


What I have tried:

this is my code in c# :

int messageLenght = 0;
string pdu = SubmitPdu.GetPdu("destinationNumber", "Message", "ServiceCenterNumber", out messageLenght);
SerialPort comport = new SerialPort();
comport.PortName = "com21";
comport.BaudRate = 921600;
comport.Parity = Parity.None;
comport.StopBits = StopBits.One;
comport.DataBits = 8;
comport.ReadBufferSize = 10000;
comport.ReadTimeout = 1000;
comport.WriteBufferSize = 10000;
comport.WriteTimeout = 1000;
comport.RtsEnable = true;
if (!comport.IsOpen)
    comport.Open();
comport.DiscardInBuffer();
comport.DiscardOutBuffer();
comport.WriteLine("AT");
System.Threading.Thread.Sleep(1000);
comport.WriteLine("AT+CMGF=0" + (char)13);
System.Threading.Thread.Sleep(1000);
string s2 = "AT+CMGS=" + messageLenght.ToString();
comport.WriteLine(s2 + (char)13);
System.Threading.Thread.Sleep(1000);
comport.WriteLine(pdu + (char)26 );
System.Threading.Thread.Sleep(5000);
MessageBox.Show(comport.ReadExisting());
comport.Close();

and result is :
AT

OK
AT+CMGF=0

OK
AT+CMGS=15

> 0691891901500011000*****************000A70178->

ERROR


but when use this command in Hyper Terminal every things is okay :
at
OK
at+cmgf=0
OK
at+cmgs=15
> 069189190150001100*****************000A70178->
+CMGS: 139

OK


where is the bug?
thanks all
Posted
Updated 31-Mar-18 2:29am
Comments
[no name] 31-Mar-18 7:42am    
Try to send "AT+CMGS=<number><cr><message><ctrl-z>" as one string. You are using WriteLine which does append a <lf>, but you also append <cr> in your code so finally you send something like this:"AT+CMGS=<number><cr><lf><message><ctrl-z><lf>"

Because you add the <cr> by yourself I would use comport.Write to be on the safe side
mohamadMahmodi 1-Apr-18 13:29pm    
I used the .write instead of the .writeLine and got it right, thank you
[no name] 1-Apr-18 13:31pm    
Maybe you then accept my answer to bring it out of Screen ;)

1 solution

Try to send
"AT+CMGS=<number><cr><message><ctrl-z>"
as one string.
You are using WriteLine which does append a <lf> itself which ends finally in that you send something like this *1):
"AT+CMGS=<number><cr><lf><message><ctrl-z><lf>"

*1)
C#
string s2 = "AT+CMGS=" + messageLenght.ToString();
comport.WriteLine(s2 + (char)13); // => AT+CMGS=<number><cr><lf>
comport.WriteLine(pdu + (char)26 );  // <pdu><ctrl-z><lf>


Notes:
1.) I would also use comport.Write(string text) to be on the safe side.
2.) Check AT Response for "OK" instead of your Sleep(xxxx)

I hope this helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900