VB.NET•Send a Message

Listing 3. You must use the correct PDU format when formatting a message that needs to be sent. Use this code to create the "Send SMS" button.

Private Sub cmdSend_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles cmdSend.Click
'---sends SMS message
   Dim prefix1 As String = "06915669489599"
   Dim prefix2 As String = "11E50A91"
   Dim phone As String = _
      FormatPhone(txtPhone.Text)
   Dim prefix3 As String = "2000AD"
   Dim message As String
'---encode message to PDU format
   message = septetsToOctets(txtMessage.Text)
   '---calculate the message length in hexa
   Dim messageLengthInByte As String = _
      Hex((txtMessage.Text.Length))
   If messageLengthInByte.Length = 1 Then
      messageLengthInByte = "0" + _
         messageLengthInByte
   End If
'---format string to send
   Dim stringToSend As String = prefix1 & _
      prefix2 & phone & prefix3 & _
      messageLengthInByte & message
'---calculate length of PDU less prefix1
   Dim length As Integer = (stringToSend.Length _
      - prefix1.Length) / 2
'---AT command for send message
   send("AT+CMGS=" & length)
   receive() ' error message
   '---send the actual message
   send(stringToSend & Chr(26))
   receive() ' error message
   MsgBox("Message sent!")
End Sub