views:

96

answers:

1

I have this if condition,

if (sendSMS(Convert.ToInt32(DLComportNo.SelectedItem.Text), TxtDriMob.Text,
TxtCliDet.Text) && sendSMS(Convert.ToInt32(DLComportNo.SelectedItem.Text), 
TxtCliMob.Text, TxtDriDet.Text))
{
     // I am inserting details to my db
}

and my sendSMS method looks like this,

private bool sendSMS(int portNo, string mobNo, string details)
{
    try
    {
        SerialPort SerialPort1 = new SerialPort();
        SerialPort1.PortName = "COM" + portNo.ToString();
        SerialPort1.BaudRate = 9600;
        SerialPort1.Parity = Parity.None;
        SerialPort1.DataBits = 8;
        SerialPort1.StopBits = StopBits.One;
        SerialPort1.RtsEnable = true;
        SerialPort1.DtrEnable = true;
        SerialPort1.Encoding.GetEncoder();
        SerialPort1.ReceivedBytesThreshold = 1;
        SerialPort1.NewLine = Environment.NewLine;
        SerialPort1.Open();

        SerialPort1.Write("AT" + SerialPort1.NewLine);
        Sleep(500);
        SerialPort1.Write("AT+CMGF=1" + SerialPort1.NewLine);
        Sleep(500);

        SerialPort1.Write("AT+CMGS=" + (char)34 + mobNo + (char)34 +
                               SerialPort1.NewLine); 
        Sleep(1000);
        SerialPort1.Write(details + (char)26);
        Sleep(2000);

        SerialPort1.Close();

    }
    catch
    {

    }
    return true;
}

What happens is when i use break point in my sendSMS i get my output (ie) both the methods get executed and messages are sent properly... But when i removed my breakpoint both the methods in the if statement are executed but message from the first method is sent and not from the second method.... Any suggestion?

+1  A: 

Sure seems like you should be reading back from the serial port after each command to wait for an acknowledgement from the device. Just putting a sleep in is no guarantee that the device has processed your command and is ready for additional input. After sending a command, you should read the response from the device (via the serial port) to confirm that it has processed your command as expected. Generally, devices that accept "AT" commands will return "OK" on a new line once the command has been processed. If it returns an error, you should throw or return false as appropriate to the error condition. Check the specifications for your device or try interacting with it via a terminal to see how it responds.

The MSDN Documentation on the SerialPort class illustrates how to read from the serial port.

Note that another alternative would be to refactor your method to take a collection of messages to send. That would make it easier to reuse the same SerialPort instance rather than opening it, setting it up and closing it for each message you're sending.

Jim Lamb
@Jim can't get your point.. Please explain bit more..
Pandiya Chendur
@Pandiya, I elaborated on my answer a bit. Let me know if you still have questions.
Jim Lamb
@jim could you post a sample code of refactoring the method using the same port....
Pandiya Chendur