Vb.net – Sending And Receiving SMS From GSM modem

gsmmodemserial-portvb.net

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.

Best Answer

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

Related Topic