0

I've coded a little program to read/send SMS messages using a GSM device. My code worked fine on one kind of device. Today I am using a different device and my program can't receive from it (although it can send to it just fine). The device works just fine with the same configs under puTTY.

    private void Form1_Load(object sender, EventArgs e)
    {
        Variables.sp.PortName = "COM1";
        Variables.sp.BaudRate = 9600;
        Variables.sp.DataBits = 8;
        Variables.sp.Parity = Parity.None;
        Variables.sp.StopBits = StopBits.One;
        Variables.sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        try
        {
            Variables.sp.Open();
            Variables.sp.WriteLine("AT\r");
            //Variables.sp.WriteLine("AT+CMGF=1\r");
        }
        catch
        {
            MessageBox.Show("Can't open COM1. Quit and try again.");
        }
    }

    private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();

        MessageBox.Show(indata);
    }

Any idea why it would work fine on one device and not the other? While I'm typing the same commands under puTTY and they both work the same?

Juicy
  • 11,840
  • 35
  • 123
  • 212
  • The event handler should not be static. This, however, doesn't solve your problems, probably. – Thorsten Dittmar Apr 04 '13 at 11:51
  • Thanks for pointing that out. No my problem is the same. More worrying is that my teacher's code doesn't even work for this device. However I can double confirm that the device works absolutely fine with the standard commands under puTTY, I just sent myself an SMS. – Juicy Apr 04 '13 at 11:56
  • Are you sure the baud rate and other COM port settings are the same? And what about using `AT\r\n` instead of just `\r`? – Thorsten Dittmar Apr 04 '13 at 11:58
  • Yes: COM1, baud=9600, data bits = 8, stop bits = 1, parity = none under puTTY, the same. Tried with \r\n as well, no luck. Makes no sens!!! – Juicy Apr 04 '13 at 12:06
  • I was told I don't need to set Flow Control under C# because it would be XON/XOFF by default, is this true? Can I check what the flow control is set to? – Juicy Apr 04 '13 at 12:07
  • See `Handshake` property. – Thorsten Dittmar Apr 04 '13 at 12:32
  • Maybe send and receive an SMS manually from each device, using Hyperterminal and AT commands, to try and see any differences between the devices? – user1725145 Apr 05 '13 at 13:08

0 Answers0