'Sending And Receiving SMS From GSM modem

I am trying to send message from GSM modem. I can submit AT commands the response is OK without any ERRORS. But the problem is I can't send message or read message.

I have implemented 3 functions:

  1. Connect to port
  2. Read SMS
  3. Send SMS
  4. Handles

1. Connect To Port:

    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" & 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

2. Read SMS

      Private Sub btn_read_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_read.Click
            Try
                SerialPort1.WriteLine("AT" & vbCrLf) 'is modem okay?
                Thread.Sleep(1000)

                SerialPort1.WriteLine("AT+CMGF=1" & vbCrLf) 'To format SMS as a TEXT message
                Thread.Sleep(1000)

                SerialPort1.WriteLine("AT+CPMS=""SM""" & vbCrLf) ' Select SIM storage
                Threading.Thread.Sleep(1000)

                SerialPort1.WriteLine("AT+CMGL=""REC UNREAD""" & vbCrLf) 'read unread messages
                Threading.Thread.Sleep(1000)

                SerialPort1.WriteLine("AT+CMGL=""ALL""" & vbCrLf) 'print all message
                Threading.Thread.Sleep(1000)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

3. Send SMS

 Private Sub btn_send_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_send.Click
        Try
            With SerialPort1
                .WriteLine("AT" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .WriteLine("AT+CMGF=1" & vbCrLf) 'Instruct the GSM / GPRS modem to operate in SMS text mode
                Threading.Thread.Sleep(1000)
                .WriteLine("AT+CMGS=""9802100355""" & vbCr) 'sender ko no. rakhne ho tyo txtnumber ma 
                Threading.Thread.Sleep(1000) 'thapeko
                .WriteLine("This is test message" & vbCrLf & Chr(26)) 'txtmessage automatic huna parchha haina?

            End With


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

4. Handles for Data received in Serial Port

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

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.



Solution 1:[1]

There may several cause of this error. First check your network. Second set Message service center number using AT commands and save this setting. Hope this will help you

Solution 2:[2]

In your second function, you can try declaring a string variable to receive the data, like this:

With serialport1
    rcvdata=""
    .Write(All AT commands)
    Threading.Thread.Sleep(1000)
    Msgbox(rcvdata.Tostring)
End With

You can add a handler to datareceived to read all the bytes:

Dim entrada As String = " "
Dim numeros As Integer = SerialPort1.BytesToRead
For i As Integer = 1 To numeros
    entrada&= Chr(SerialPort1.ReadChar)
Next
chama(entrada)
Private Sub chama(ByVal dados As String)
    rcvdata &= dados
End Sub

Solution 3:[3]

AT+CMGS=""9802100355"

Your Phone number is wrong thats why you getting error 500, you need to enter full phone number including 0 at the front.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ashok singh
Solution 2
Solution 3 Unheilig