通过C#中的GSM调制解调器接收短信

时间:2014-08-02 10:15:43

标签: c# sms gsm modem

我希望通过C#中的gsm调制解调器接收消息。我已经编写了一个代码但是在控制台上,每当收到消息时,我都会得到连续的ERROR流。请帮我收到消息。这是我的代码。

     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());

    }
}

1 个答案:

答案 0 :(得分:1)

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);