Prakash Ash
Prakash Ash

Reputation: 151

AT Command Works, But no response

I've tried sending AT commands by opening a serial port connection and my goal was to send out an SMS. I was able to achieve this and the message was sent out, which is a sign that the WriteLine() method completes successfully. However, i can't seem to get any response in my receive buffers. I've turned on echo by looking at some of the answers over here on stackoverflow.

But still there is no response. I tried waiting out by using Thread.Sleep() and also by using the handler, p_DataReceived. There is still no response.

One example would be the simple "AT" command which should have a response of "ok". But there are no responses at all (i.e. BytesToRead property size is 0)

I'm trying this with D-Link's dwm222 4G LTE USB adapter

Any pointers would be greatly appreciated !

    private void button_Click(object sender, RoutedEventArgs e)
    {



        mySerialPort = new SerialPort("COM15");
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;

        mySerialPort.Handshake = Handshake.None;
        mySerialPort.NewLine = Environment.NewLine;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
        mySerialPort.Open();
        mySerialPort.DiscardInBuffer();
        mySerialPort.DiscardOutBuffer();


        mySerialPort.WriteLine("ATE1"); //turn on echo


        mySerialPort.WriteLine("AT"); //test response


        Thread.Sleep(3000);
        string responseSTR = mySerialPort.ReadLine();

        if (responseSTR.Contains("OK"))  //empty string
        {

            MessageBox.Show(responseSTR);
         }

        this.SendSms("+XXXXXXXXXXXXXXXXXXX", "Hello From C#");

        mySerialPort.Close();

    }

    public void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string s = (sender as SerialPort).ReadExisting();
        buff += s;
        MessageBox.Show(buff);
    }

   void SendSms(string destination, string text)
    { ........}

Upvotes: 0

Views: 2163

Answers (2)

Prakash Ash
Prakash Ash

Reputation: 151

Enabling DTR and RTS filled up the buffer of the serial port object.

        mySerialPort = new SerialPort("COM17");
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;

        mySerialPort.Handshake = Handshake.None;
        mySerialPort.NewLine = Environment.NewLine;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);

        mySerialPort.Open();
        mySerialPort.DiscardInBuffer();
        mySerialPort.DiscardOutBuffer();
        mySerialPort.DtrEnable = true;    //this solved my problem
        mySerialPort.RtsEnable = true;    //this solved my problem

Upvotes: 1

Zain Ul Abidin
Zain Ul Abidin

Reputation: 2710

i think you should discard buffer each time so you should get a fresh incoming data try this code i have modified in your event handler

public void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string s = mySerialPort.ReadExisting();
        buff += s;
        mySerialPort.DiscardInBuffer();
        mySerialPort.DiscardOutBuffer();
        MessageBox.Show(buff);
    }

Upvotes: 0

Related Questions