Reirou
Reirou

Reputation: 1

Sending SMS using AT-Commands on VB.NET

I'm trying to send SMS messaging through VB.NET using AT-Commands. This is my code below:

Imports System.IO.Ports
Imports System.Runtime.CompilerServices

Public Class Form1
    Dim SerialPort1 As New System.IO.Ports.SerialPort\
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If

        With SerialPort1
            .PortName = "COM4"
            .BaudRate = 9600
            .DataBits = 8
            .Parity = Parity.None
            .StopBits = StopBits.One
            .Handshake = Handshake.RequestToSend
            .DtrEnable = True
            .RtsEnable = True
            .NewLine = vbCrLf
        End With


        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(SerialPort1.PortName & " is not available!")
        End Try

        If SerialPort1.IsOpen Then
            With SerialPort1
                .Write("AT" & vbCrLf)
                .Write("AT+CMGF=1" & vbCrLf)
                .Write("AT+CMGS=" & Chr(34) & txtRecepient.Text & Chr(34) & vbCrLf) 'Error begins here
                .Write(txtMessage.Text & Chr(26))
            End With
        Else
            MsgBox(SerialPort1.PortName & " is not available!")
        End If
    End Sub
End Class

The error begins when AT+CMGS gets executed; the whole code hangs. When I try to use If-Statements for each AT-command, the error begins at the first command .Write("AT" & vbCrLf).

I debugged with PuTTY and the commands were working as expected; it results in OK messages and PuTTY actually sends SMS messages to my phone.

However, when I try to integrate AT-Commands to my VB.NET Code, it breaks down.

Upvotes: 0

Views: 42

Answers (0)

Related Questions