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?