none
Receiving SMS from GSM RRS feed

  • Question

  • hi ,

      I am writing a windows service which receives the sms through serial port using GSM modem and insert into the table. For this i used serial port's data received event.

     The following is my code

            void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                try
                {
                    if (e.EventType == SerialData.Chars)
                    {
                        string buffer = string.Empty;
                        Thread.Sleep(500);
                        do
                        {
                                string t = port.ReadExisting();
                                buffer += t;
                        }
                        while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

    My sms ends with 'OK' so i can get the full sms using this. But my confusion is if the data is loss how i terminate the loop? Any better approach for my scenario?
    Tuesday, September 4, 2012 7:37 AM

Answers

  • Hi akhilrajau,

    You can try enabling handshaking, using the Handshake property of the SerialPort object. You'll have to set it on both the sender or receiver


    Regards, http://www.shwetalodha.blogspot.in/

    Friday, September 7, 2012 3:26 AM
  • To answer your questions:

    A stopwatch is found in System.Diagnostic. You can start the stopwatch before your loop, wait for data with ReadExisting(), then on the exit condition for the loop, add a check if the stopwatch has not exceeded a specified period of time.

    e.g. (not tested)

    int c = 0;
    do 
    {
      s
    tring t = port.ReadExisting();
      buffer
    += t;
      c++;
      Thread.Sleep(100);

    }
    while ((!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n")) && c < 5);

    SMS handling for GSM mobiles is tricky. Not all phones behave the same (some are buggy), so I hope you have an "embedded" modem that stays fixed.

    Usually you want to get an event when an SMS arrives. +CMTI (iirc) is the response. Then you can list the SMS and get the contents.

    The length of the SMS is part of the response to getting the SMS. The ETSI specs go into some detail and a bit of trial and error you can get it reliably working. You'll want the AT command spec, as well as the other for SMS itself.

    When sending an SMS, don't forget to send the Ctrl-Z.

    Some phones send OK once the SMS is sent, others send OK only when it has been submitted to the network. I worked in Telematics for a few years and there are some other strange conditions, which you can only find out by significant testing.

    • Proposed as answer by Jason Curl Sunday, September 9, 2012 6:24 PM
    • Marked as answer by Mike FengModerator Monday, September 24, 2012 2:41 PM
    Wednesday, September 5, 2012 8:18 AM
  • Hi akhilrajau,

    Alternatively you can refer this:

    http://go4answers.webhost4life.com/Example/serial-communication-slow-27146.aspx


    Regards, http://www.shwetalodha.blogspot.in/

    Friday, September 7, 2012 3:28 AM

All replies

  • Hi,

    As a simple solution, why don't you put a small delay in the loop (like a Thread.Sleep(100)) and maintain a counter? If the counter is exceeded, then abort the loop. Other variants could be using a StopWatch to see if the total time has exceeded.

    Regards,
    Jason.

    Tuesday, September 4, 2012 5:15 PM
  • I see you are approaching and getting knowledge implementing your solution, its a good thing if you have the time.

    If you in a hurry, you can use this library for Gsm comunication, including sms handling for .Net:

    http://www.scampers.org/steve/sms/libraries.htm#gsmcomm_download

    Regards


    -

    Tuesday, September 4, 2012 7:25 PM
  • Thanks for the response. As i understood correctly, Jason's suggestion is i add one counter in the do--while loop and also add the condition in while loop if the counter  reaches a specified count. Am i right? Can you tell me about stop watch? Which one is better?

    Dear Enrique, I heard about that library. The situation is handled there? The windows service going to create is a part of my product. So i am confusing about any licensing issue and support for this. So only i use general serial port program using AT commands. Can you guide me which is better? In my situation sms may came from from different mobiles at a time? So how this is handled?

    Update: Dear Enrique, I checked the library and it is useful in my scenario as well. But can you clarify me some of my doubts regarding that?

    1. How much time the received event is wait to get the full SMS. When i connect i can give one timeout. Is this used for receiving also?

    2. How the SMS is identified as full. Acutually when i check new sms indication (direct message output mode ) using AT+CNMI command i didnt got any OK response after message received so only asked?

    • Edited by akhilrajau Wednesday, September 5, 2012 7:08 AM Update for GSMComm
    Wednesday, September 5, 2012 4:13 AM
  • To answer your questions:

    A stopwatch is found in System.Diagnostic. You can start the stopwatch before your loop, wait for data with ReadExisting(), then on the exit condition for the loop, add a check if the stopwatch has not exceeded a specified period of time.

    e.g. (not tested)

    int c = 0;
    do 
    {
      s
    tring t = port.ReadExisting();
      buffer
    += t;
      c++;
      Thread.Sleep(100);

    }
    while ((!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n")) && c < 5);

    SMS handling for GSM mobiles is tricky. Not all phones behave the same (some are buggy), so I hope you have an "embedded" modem that stays fixed.

    Usually you want to get an event when an SMS arrives. +CMTI (iirc) is the response. Then you can list the SMS and get the contents.

    The length of the SMS is part of the response to getting the SMS. The ETSI specs go into some detail and a bit of trial and error you can get it reliably working. You'll want the AT command spec, as well as the other for SMS itself.

    When sending an SMS, don't forget to send the Ctrl-Z.

    Some phones send OK once the SMS is sent, others send OK only when it has been submitted to the network. I worked in Telematics for a few years and there are some other strange conditions, which you can only find out by significant testing.

    • Proposed as answer by Jason Curl Sunday, September 9, 2012 6:24 PM
    • Marked as answer by Mike FengModerator Monday, September 24, 2012 2:41 PM
    Wednesday, September 5, 2012 8:18 AM
  • Thanks for the support. Sending not in the current scope. But i already created one service for this and running fine now. 

    Now i need to receive the sms and the confusion is how i identified the sms is fully received.I got your point in the timout model.

    Now i am checking GSMComm library provided by Enrique. ?In that also i am confused how much time there waiting to timeout. In the starting of the application the demo ask time interval but not specific to the received event

    Wednesday, September 5, 2012 8:26 AM
  • How about 500ms?

    We always get a message without any interruption. If you havn't recieve any data over 500ms, we can think it is over.

    Have a nice day.


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Friday, September 7, 2012 1:36 AM
  • Hi akhilrajau,

    You can try enabling handshaking, using the Handshake property of the SerialPort object. You'll have to set it on both the sender or receiver


    Regards, http://www.shwetalodha.blogspot.in/

    Friday, September 7, 2012 3:26 AM
  • Hi akhilrajau,

    Alternatively you can refer this:

    http://go4answers.webhost4life.com/Example/serial-communication-slow-27146.aspx


    Regards, http://www.shwetalodha.blogspot.in/

    Friday, September 7, 2012 3:28 AM
  • thanks friends. 500 ms is i just put as default to wait for sms. I can accept that GSM data will receive with in this. Now i just give 300 ms as default.I implemented using the GSMComm library.But now i have confused that when i send sms continuously, more than 1 sms may receive as one . I don't the know is this  possible in GSM receiving ? Can anyone help this also

    Saturday, September 8, 2012 11:29 AM