【问题标题】:Why is it error when I use the "at command "in the C#, but does the command work properly in the hyper terminal?为什么我在C#中使用“at命令”会出错,但是在超级终端中该命令是否正常工作?
【发布时间】:2018-03-30 20:16:46
【问题描述】:

这是我在 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();

结果是:

好的

AT+CMGF=0

好的

AT+CMGS=15

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

错误

但在超级终端中使用此命令时一切正常:

好的

at+cmgf=0

好的

at+cmgs=15

> 069189190150001100**********************000A70178->

+CMGS:139

好的

错误在哪里?

谢谢大家

【问题讨论】:

  • 尽量不要阻塞线程,尽可能让你的代码异步。您可以将事件处理程序附加到串行端口类并在字节到达时读取它们。
  • 所以你在comport.WriteLine(pdu + (char)26 );之后得到错误?那是对的吗? (char)26 是 ASCII 文件结束符。这是接收方可以处理的事情吗?在过去,这是一种通过发出输入流结束信号来使程序崩溃的简单方法。
  • Tom,我删除了块,但问题没有解决。
  • TnTinMn,我也使用了这个代码:.WriteLine(pdu + "\x1A") 也没有什么不同。另一边是手机设备。

标签: c# sms at-command pdu


【解决方案1】:

我用 .write 更改了 .writeLine 并删除了线程延迟,问题就解决了。 谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-28
    • 2021-04-08
    • 1970-01-01
    • 2020-07-28
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多