0

this is my code in c# :

string pdu=SubmitPdu.GetPdu("destNumber","Message","ServiceNumber");
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=" + SubmitPdu.GetLenPdu(pdu);
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

Community
  • 1
  • 1
  • Try not to block thread and make your code asynchronous if possible. You can attach an event handler to the serial port class and read the bytes as they arrive. –  Mar 30 '18 at 21:05
  • So you get the error after `comport.WriteLine(pdu + (char)26 );`? Is that correct? `(char)26 ` is the ASCII end-of-file character. Is that something that the receiving side can handle? In the good-old days, that was an easy way to crash a program by signaling the end of the input stream. – TnTinMn Mar 30 '18 at 21:39
  • Tom,I removed blocks but the problem did not resolve. – mohamadMahmodi Mar 31 '18 at 12:25
  • TnTinMn,I also used this code: .WriteLine(pdu + "\x1A") too and did not make a difference. other side is a cellphone device. – mohamadMahmodi Mar 31 '18 at 12:28

1 Answers1

0

I changed .writeLine with the .write just and remove thread delay and The problem was solved . thanks all