1

I have a Huawei Modem connected to my VB.net project all AT Commands work such as CONNECT, READ and SEND SMS

The only thing that I cannot work with is the DELETE SMS

I have this code:

With SerialPort1
    .Write("AT")
    Threading.Thread.Sleep(1000)
    .Write("AT+CMGF=1")
    Threading.Thread.Sleep(1000)
    .Write("AT+CPMS=""SM""" & vbCrLf)
    Threading.Thread.Sleep(1000)
    .Write("AT+CMGD=1,4")
    Threading.Thread.Sleep(1000)
    MsgBox(at_status.ToString)            
End With

But the at_status throws me "ERROR" always.
AT+CMGD=1,4 as what I've read on the documentation is the right command but it throws me an "ERROR" always.

What do you think is wrong with my code? All other command works, except this.

MatSnow
  • 7,357
  • 3
  • 19
  • 31
Quest Major
  • 61
  • 1
  • 7
  • could u add the exception/error – Raizzen Jan 11 '18 at 14:50
  • tried adding try-catch block, it won't catch the error since the error is inside the AT Command not an ordinary runtime error. – Quest Major Jan 11 '18 at 14:57
  • 1
    No! No! No! You should [never, never, never, never, never, never, never, never, never, never, ever use Threading.Thread.Sleep like that](https://stackoverflow.com/a/46064206/23118). You MUST **read** and **parse** the responses given back by the modem, otherwise you will [abort](https://stackoverflow.com/a/1389034/23118) the commands. – hlovdal Jan 19 '18 at 20:58

2 Answers2

0

I figured it out, I only need to put some linebreaks & vbCrLf after every AT Commands.

 With SerialPort1
                .Write("AT" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write("AT+CMGF=1" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write(TextBox3.Text & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write("AT+CMGD=1,4" & vbCrLf)
                Threading.Thread.Sleep(1000)
                MsgBox(at_status.ToString)
 End With
Quest Major
  • 61
  • 1
  • 7
  • Fetch a copy of the [V.250 standard](http://www.itu.int/rec/T-REC-V.250-200307-I/en) and read at least all of chapter 5. This standard is the bible for AT command handling and will teach you an enormous amount regarding AT command handling. Like for instance that using vbCrLf is wrong; AT command lines should be terminated with `\r` alone and nothing else. – hlovdal Jan 19 '18 at 21:22
  • Communicating with AT Commands using Visual Basic needs "vbCrLf", \r is not a VB Command so VB wont recognize what \r mean. – Quest Major Jan 20 '18 at 14:27
  • Not being able to use `\r` directly is not a valid excuse for continuing to use vbCrLf which is wrong. As far as I know `Chr(13)` should be equivalent. – hlovdal Jan 21 '18 at 00:11
0

Hope this helps.

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Try
        With SerialPort1
            .Write("AT" & vbCrLf)
            Threading.Thread.Sleep(100)
            .Write("AT+CMGF=1" & vbCrLf)
            Threading.Thread.Sleep(100)
            .Write("AT+CPMS=""SM""" & vbCrLf)
            Threading.Thread.Sleep(100)
            .Write("AT+CMGD=" & Label9.Text & "" & vbCrLf)
            Threading.Thread.Sleep(100)
            'MsgBox(ReceivedData.ToString
            If ReceivedData.ToString.Contains("ERROR") Then
                MsgBox("Got some error", MsgBoxStyle.Critical, "Error")
            Else
                MsgBox("Message Deleted", MsgBoxStyle.Information, "Deleted")
                ListView1.Items.Clear() 
                ReceivedData = ""
                Try
                    With SerialPort1
                        .Write("AT" & vbCrLf)
                        Threading.Thread.Sleep(100)
                        .Write("AT+CMGF=1" & vbCrLf)
                        Threading.Thread.Sleep(100)
                        .Write("AT+CPMS=""SM""" & vbCrLf)
                        Threading.Thread.Sleep(100)
                        .Write("AT+CMGL=""ALL""" & vbCrLf)
                        Threading.Thread.Sleep(100)
                        ReadMesssage()
                    End With

                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
                Label11.Text = ListView1.Items.Count
                Label9.Text = ""
            End If
        End With
    Catch ex As Exception

    End Try
End Sub
Pang
  • 9,564
  • 146
  • 81
  • 122
ramber
  • 21
  • 3