0

I'm trying to send and receive data with a GSM modem attached to a COM port. Here is the essential part of my code:

        SerialPort sp = new SerialPort();
        sp.PortName = "COM1";
        sp.BaudRate = 9600;
        sp.DataBits = 8;
        sp.Parity = Parity.None;
        sp.StopBits = StopBits.One;
        sp.ReadTimeout = 5000;

                sp.Open();
                if (sp.IsOpen)
                {
                    Console.WriteLine(sp.PortName + " is open");

                    Console.WriteLine("Trying to write data");
                    sp.Write("AT");
                    Console.WriteLine("Data has been sent");

                    Console.WriteLine("Trying to read data");
                    char[] read = new char[200];
                    sp.Read(read, 0, 20);
                    foreach (char chr in read)
                    {
                        Console.Write(chr);
                    }
                    Console.WriteLine();

                }

I don't get any errors but the GSM device is just sending me back whatever I send him. In this case AT. If I send "BOO" he sends back "BOO" (although sometimes I only get the first letter, I think this is because of the way I'm reading the data, should be in a seperate thread no? I can do this easily later).

I'm expecting him to send me back "OK" when I send him "AT".

What explanation could there be for the device sending me back whatever I'm sending him. I was thinking it could maybe be that the port communication is not set up properly and I'm actually not getting to the device, just sending it back to myself?

Juicy
  • 11,840
  • 35
  • 123
  • 212

1 Answers1

2

Have you communicated to it with PuTTY? You should first try to send and receive data from a terminal application (PuTTY Download Link) to rule out any issues with the modem itself, then move on to writing an application to communicate.

As for the code, you should be using sp.WriteLine instead of sp.Write... The modem needs to see a newline character to know your command is finished. You can set the newline character by using sp.NewLine so that it matches what the modem needs to see. Alternatively, you can just append '\r' to any AT command you are writing.

The GSM modems I have used all have echo on by default, you can disable echo by sending 'ATE0' to it. While echo is on, the modem will first echo anything you send it, then it will send it's response. I suspect you are seeing the echo come back but not the response, due to the way you are reading the data. Try this method for reading the data:

//add this just before opening the port
sp.DataReceived += new 
    SerialDataReceivedEventHandler(port_DataReceived);

Add the following method outside the method you are setting up the port:

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
  Console.WriteLine(port.ReadExisting());
}
Crake
  • 1,661
  • 1
  • 13
  • 30
  • Thanks for the answer but I tried sending ATE0 and then AT. The answer I get back is "ATE0AT". Looks like he's ignoring me and just echoing. – Juicy Mar 28 '13 at 14:19
  • Updated answer to suggest using PuTTY to try commands before writing code to talk to it. – Crake Mar 28 '13 at 14:23
  • Also, make sure you are sending a newline character by using sp.WriteLine instead of sp.Write – Crake Mar 28 '13 at 14:26
  • Really weird, tried PuTTY, sent AT and got OK back. Tried sending with my code, even with WriteLine instead of Write, just getting AT back... – Juicy Mar 28 '13 at 14:38
  • Try the following: sp.Write("AT\r"); – Crake Mar 28 '13 at 14:39