title: Sending SMS gives CMS error 304
body: <p>I'm working on a program in C# that can send a SMS from my computer using my GSM modem, and I got most of my program to work.</p>
  
  <p>I can send most Hayes AT commands like: "AT" and "AT+CGMI;+CGMM", which returns the message "OK", so I know I can communicate with the modem.</p>
  
  <p>However I DO have problems sending a standard 7-bit encoded message from the modem to my cell phone.</p>
  
  <p>With the help of PDUspy I have a somewhat confident that I'm encoding my message right.</p>
  
  <p>However the following code fails miserably:</p>
  
  <pre><code>public string SendEncodedSms(string reciever, string message)
  {
    string response = GetResponse("AT+CMGF=0");
    if (isFine(response))
    {
      string encodedBody = "000100" + EncodedReceiver(reciever) + "0000";
      encodedBody += EncodeToSeptet(message);
  
      int cmgs_header = encodedBody.Length / 2;
  
      port.WriteLine("AT+CMGS=" + cmgs_header.ToString() + "\r\n");
      port.WriteLine(encodedBody + (char)26);
  
      return ReadResponse(300);
    }
    else throw new ApplicationException("Cant go into SMS PDU mode");
  }
  </code></pre>
  
  <p>There five function calls inside:</p>
  
  <ul>
  <li><p>GetResponse() is function that works as a wrapper when communicating with the modem - always making sure each command ends with a "\r", and returns OK or Error depending on the response from the modem.</p></li>
  <li><p>isFine() is a simple function that check whether or not the response from GetResponse() contained a "OK" message.</p></li>
  <li><p>ReadResponse() returns any response from the modem whether that is OK or any error messages and whatever there also might be embedded between the AT command and the status message.</p></li>
  <li><p>EncodeReceiver() encode the receivers phone number in reverse nibble notation. E.g. 12345678 becomes 21436587.</p></li>
  <li><p>EncodeToSeptet() Encodes the message from 8bit notation to 7bit notation.</p></li>
  </ul>
  
  <p>All functions have been confirmed to work fine using PDU spy and responses sent to debug window.</p>
  
  <p>Calling <code>SendEncodedSms("&lt;my phone number&gt;", "test")</code> returns:</p>
  
  <pre><code>AT+CMGS=17
  
  0001000A91xxxxxxxxxx000004F4F29C0E
  
  +CMS ERROR: 304
  </code></pre>
  
  <p>I replaced my encoded phone number for privacy issues... ;-)</p>
  
  <ul>
  <li>Is there anybody who can give me a hint what I am failing at?</li>
  </ul>
  
  <p>UPDATED:</p>
  
  <p>Link hinted I was calculating cmgs_header wrong.</p>
  
  <p>After a bit of cleaning code I got:</p>
  
  <pre><code>public string SendEncodedSms(string receiver, string message)
  {
    if(isFine("AT+CMGF=0"))
    {
       string encodedBody = EncodedReceiver(receiver) + "0000";
       encodedBody += EncodeToSeptet(message);
  
       /* The +2 in calculation is a hack. Its probleby two of the octets 
        * in 000100 that should be a part of length calculation. But need 
        * to verify in against severeal sites.
        */
       int cmgs_header = (encodedBody.Length / 2) + 2;
  
       encodedBody = "000100" + encodedBody;
  
       // Rest is as before.
  
      ...
  
    }
  
    else ...
  }
  </code></pre>