如何在VB.NET中从串口获取消息



我已经有了使用USB宽带套件在VB中发送消息到手机的工作代码。现在我正在用另一种方式从宽带SIM卡获取消息这里是代码:

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

上面的代码不能工作。这一行总是返回一个空值Debug.Print(SerialPort.ReadExisting),即使SIM卡有未读消息。如果你能给我建议最好的做法,可以吗?谢谢谢谢!

读取GSM的方式是初始化或发送命令来要求GSM传输一些消息,消息不能自己传输。我想你漏掉了启动消息流的命令。您可能想要阅读此http://arduino.cc/en/Tutorial/GSMExamplesReceiveSMS—这些都是用C编写的,但主要内容保持不变。

希望对您有所帮助

最新更新