VB.NET•Receive a Message

Listing 4. You must convert a message received in the PDU format back into plain text. This code creates the "Get SMS" button.

Private Sub cmdGet_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles cmdGet.Click
'---set storage to Mobile Equipment
   send("AT+CPMS=""ME""")
   receive() ' error message
   Dim i As Integer = 1
   '---prepare the ListView control
   ListView1.Clear()
   ListView1.Columns.Add("Phone", -1, _
      HorizontalAlignment.Center)
   ListView1.Columns.Add("Message", 150, _
      HorizontalAlignment.Left)
   While True ' read_message loop
'---AT command to read message
      send("AT+CMGR=" & i)
      Dim response As String
'---receive message
      response = receive()
      If (response.IndexOf("CMS ERROR")) >= 0 _
         Then 'error
' message index not found, exit read_message loop
         Exit While
      ElseIf (response.IndexOf("+CMGR: 1")) >= _
         0 Or (response.IndexOf("+CMGR: 0")) _
         >= 0 Then
' 1 is received message, 0 is unread message
         Dim phone, message As String
'---phone number of sender
         phone = Mid(response, _
            response.IndexOf("0691") + 21, 10)
'---message from sender
         message = Mid(response, _
            response.IndexOf("0691") + 51, _
            response.Length - response.IndexOf("0691") + 50)
'---add message to ListView
         Dim ls As New _
            ListViewItem(FormatPhone(phone))
'---add message
   ls.SubItems.Add((OctetsToSeptets(message)))
         ListView1.Items.Add(ls)
'------------------------------------
      Else
' might be sent messages, etc/
      End If
      i += 1
   End While
   MsgBox("Messages received complete!")
End Sub