How to get messages from Serial Port in VB.NET

943 Views Asked by At

I already have the working codes using USB broadband kit to send a message in VB to mobile phone. Now I am doing the other way which is getting the messages from the Broadband SIM here's the code:

Imports System
Imports System.IO.Ports

Public Class ReadSMS

Dim SerialPort As New System.IO.Ports.SerialPort()

Private Sub ReadNow_Click(sender As Object, e As EventArgs) Handles ReadNow.Click

    If SerialPort.IsOpen Then
        SerialPort.Close()
    End If

    SerialPort.PortName = "COM4"
    SerialPort.BaudRate = 9600
    SerialPort.Parity = Parity.None
    SerialPort.StopBits = StopBits.One
    SerialPort.DataBits = 8
    SerialPort.Handshake = Handshake.None
    SerialPort.DtrEnable = True
    SerialPort.RtsEnable = True
    SerialPort.NewLine = vbCrLf
    SerialPort.ReadTimeout = 10000
    SerialPort.Open()

    If SerialPort.IsOpen() Then
        Try
            Debug.Print("START")
            Debug.Print(SerialPort.ReadExisting)
            Debug.Print("END")
        Catch ex As Exception
            MsgBox("read " & ex.Message)
        End Try
    Else
        MsgBox("Port not available")
    End If
End Sub
End Class

The code above is not working. This line always return an empty value Debug.Print(SerialPort.ReadExisting) even if the SIM card has unread messages. Will that be okay if you will suggest me the best thing to do? Thanks Thanks!

1

There are 1 best solutions below

0
WindyHen On

The way you can read GSM is to initialize or send command to ask GSM to stream you some messages, the messages cannot stream by itself. I think you are missing the command to initate the stream of message. You might wana read this http://arduino.cc/en/Tutorial/GSMExamplesReceiveSMS -- these are in C, but the principal remains the same.

Hope this help