title: Sending And Receiving SMS From GSM modem
body: <p>I am trying to send message from GSM modem. I can submit AT commands the response is <strong>OK</strong> without any <strong>ERRORS</strong>. But the problem is I can't send message or read message.</p>
  
  <p>I have implemented 3 functions:</p>
  
  <ol>
  <li>Connect to port</li>
  <li>Read SMS</li>
  <li>Send SMS </li>
  <li>Handles</li>
  </ol>
  
  <p><strong>1. Connect To Port:</strong></p>
  
  <pre><code>    Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
              If SerialPort1.IsOpen Then
                  SerialPort1.Close()
                  BtnConnect.Text = "Connect"
              Else
                  Try
                      With SerialPort1
                          .PortName = Trim(Mid(ComboBox1.Text, 1, 5))
                          .BaudRate = 9600
                          .Parity = IO.Ports.Parity.None
                          .DataBits = 8
                          .StopBits = Ports.StopBits.One
                          .Handshake = Ports.Handshake.None
                          .RtsEnable = True
                          .DtrEnable = True
                          .Open()
                          .WriteLine("AT+CNMI=1,2,0,0,0" &amp; vbCrLf) 'send whatever data that it receives to serial port  
                      End With
                      BtnConnect.Text = "Disconnect"
                  Catch ex As Exception
                      BtnConnect.Text = "Connect"
                      MsgBox(ex.Message)
                  End Try
              End If
  
          End Sub
  </code></pre>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <p><strong>2. Read SMS</strong></p>
  
  <pre><code>      Private Sub btn_read_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_read.Click
              Try
                  SerialPort1.WriteLine("AT" &amp; vbCrLf) 'is modem okay?
                  Thread.Sleep(1000)
  
                  SerialPort1.WriteLine("AT+CMGF=1" &amp; vbCrLf) 'To format SMS as a TEXT message
                  Thread.Sleep(1000)
  
                  SerialPort1.WriteLine("AT+CPMS=""SM""" &amp; vbCrLf) ' Select SIM storage
                  Threading.Thread.Sleep(1000)
  
                  SerialPort1.WriteLine("AT+CMGL=""REC UNREAD""" &amp; vbCrLf) 'read unread messages
                  Threading.Thread.Sleep(1000)
  
                  SerialPort1.WriteLine("AT+CMGL=""ALL""" &amp; vbCrLf) 'print all message
                  Threading.Thread.Sleep(1000)
              Catch ex As Exception
                  MsgBox(ex.Message)
              End Try
          End Sub
  </code></pre>
  
  <p><strong>3. Send SMS</strong></p>
  
  <pre><code> Private Sub btn_send_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_send.Click
          Try
              With SerialPort1
                  .WriteLine("AT" &amp; vbCrLf)
                  Threading.Thread.Sleep(1000)
                  .WriteLine("AT+CMGF=1" &amp; vbCrLf) 'Instruct the GSM / GPRS modem to operate in SMS text mode
                  Threading.Thread.Sleep(1000)
                  .WriteLine("AT+CMGS=""9802100355""" &amp; vbCr) 'sender ko no. rakhne ho tyo txtnumber ma 
                  Threading.Thread.Sleep(1000) 'thapeko
                  .WriteLine("This is test message" &amp; vbCrLf &amp; Chr(26)) 'txtmessage automatic huna parchha haina?
  
              End With
  
  
          Catch ex As Exception
              MsgBox(ex.Message)
          End Try
      End Sub
  </code></pre>
  
  <p><strong>4. Handles for Data received in Serial Port</strong></p>
  
  <pre><code>Private Sub serialport1_datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
      'Pause while all data is read
      System.Threading.Thread.Sleep(300)
      rcvdata = SerialPort1.ReadExisting()
      MsgBox(rcvdata, , "Response From AT")
      rcvdata = ""
  
  End Sub
  </code></pre>
  
  <p>Where did I miss anything? While sending SMS I get CMS 500 error. With the software from the modem I am able to read and send sms. But I need to implement my own in my software.</p>