在c#中通过GSM调制解调器接收短信

本文关键字:调制解调器 GSM | 更新日期: 2023-09-27 18:16:08

我想用c#通过gsm调制解调器接收消息。我已经写了一个代码,但在控制台上,每当收到消息时,我就会得到连续的错误流。请帮我接收一条信息。这是我的代码。

     static void Main(string[] args)
        {   
              SerialPort serialport = new SerialPort();
              int mSpeed = 1;
              serialport.PortName = "COM16";
              serialport.BaudRate = 9600;
              serialport.Parity = Parity.None;
              serialport.DataBits = 8;
              serialport.StopBits = StopBits.One;
              serialport.Handshake = Handshake.XOnXOff;
              serialport.DtrEnable = true;
              serialport.RtsEnable = true;
              serialport.NewLine = Environment.NewLine;

            serialport.DataReceived += newSerialDataReceivedEventHandler(DataReceivedHandler);
            serialport.Open();
            Console.ReadKey();
    }
    private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        sp.WriteLine("AT+CMTI" + Environment.NewLine);
        Console.WriteLine(sp.ReadLine());
    }
}

在c#中通过GSM调制解调器接收短信

您的serialport.Open()使用这些代码后。
serialport.Write("AT+CMGF=1'r"); // Instruct the GSM modem to operate in SMS text mode serialport.Write("AT+CNMI=1,2,0,0,0'r"); // Receive notification of incoming messages

试试这个. .你的DataReceivedHandler

SerialPort sp = (SerialPort)sender; string input = sp.ReadExisting(); Console.WriteLine(input);