2

当我使用 SerialPort 通过 c# 在 AT COMMAND 中获得 +CDS 时,我遇到了问题,任何时候我都会截断这个 +CDS,例如:

+CDS:25 0002970C91555868047414212181414094882121814140948830

为什么我有这个问题,为什么任何时候都很好?

我正在启动 SerialPort:

    public PortCOM(string porta)
            : base(porta, 115200, Parity.None, 8, StopBits.One)
        {
            this.StatusPort = StatusPorta.Ready;
            this.DiscardNull = true;
            this.ReadTimeout = 21000;
            this.RtsEnable = true;
            this.DtrEnable = true;
            this.ReceivedBytesThreshold = 9;
            this.NewLine = "\r\n";
            this.ReadBufferSize = 1024;
        }

    public static  void TestPort()
        {
            var p = new PortCom("COM12");

            if (!p.IsOpen)
                p.Open();

            p.StatusPort = StatusPorta.Ready;
            p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceivedSample);
            p.PinChanged += new SerialPinChangedEventHandler(p_PinChanged);
            p.ErrorReceived += new SerialErrorReceivedEventHandler(p_ErrorReceived);

            p.Disposed += new EventHandler((obj, porta) =>
            {
                Console.WriteLine(((PortaCOM)obj).ToString());
            });

            if (Console.ReadKey().Key == ConsoleKey.B)
            {
                p.Close();
                p.Dispose();
            }
        }

static void p_DataReceivedSample(object sender, SerialDataReceivedEventArgs e)
        {
            var p = (PortaCOM)sender;
            try
            {
                Console.WriteLine(p.ReadExisting());
                var sb = new StringBuilder();

                sb.Append(p.ReadExisting());
                int y = sb.ToString().IndexOf("\r\n");


                var stop = Stopwatch.StartNew();
                stop.Start();
                while (y == -1)
                {
                    sb.Append(p.ReadExisting());
                    y = sb.ToString().IndexOf("\r\n");
                    if (stop.Elapsed.TotalSeconds > 10)
                        break;
                }
                stop.Stop();

                var _retorno = sb.ToString();
                var cmt = regCMT.Match(_retorno);
                var succ = regSucess.Match(_retorno);
                var report = regStatusReport.Match(_retorno);
                var erro = regError.Match(_retorno);


                #region Resposta
                if (cmt.Success)
                {
                    var smss = new SMS();
                    var source = cmt.Groups[3].Value;
                    SMS.Fetch(smss, ref source);
                    var resposta = new Resposta()
                    {
                        Mensagem = smss.Message,
                        Data = smss.ServiceCenterTimeStamp,
                        Sender = smss.PhoneNumber,
                        Operadora = p.OperadoraName.NomeOperadora.ToString()
                    };

                    GravaResposta().ToAsync(Scheduler.TaskPool).Invoke(p, cmt.Groups[3].Value);
                    p.IsError = false;
                }
                #endregion

                #region StatusReport
                if (report.Success)
                {
                    RecebeReport(p, report.Groups[2].Value.Trim());
                    p.IsError = false;
                }
                #endregion
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
        }

请我真的需要帮助,我很高兴有任何帮助!

4

1 回答 1

1

+cds对传入消息发出警报,消息位置位于 SIM 内存中。

所以这里似乎是 PDU 模式数据。它似乎可能是 Flash 消息内容。

将数据 PDU 模式转换为文本模式以接收消息。

查看此ATSMS 库

于 2013-01-01T16:56:12.270 回答