【问题标题】:SMS Sending Application in VB.netVB.net 中的 SMS 发送应用程序
【发布时间】:2015-05-07 23:59:02
【问题描述】:

我正在尝试使用 vb.net 2008 开发一个应用程序,以便从我的电脑向电话号码发送短信。我已经使用 USB 电缆(与 COM3 端口连接)将诺基亚手机连接到我的电脑。鉴于我编写的波纹管代码应该可以工作,但没有发送消息,并且我的应用程序处于死锁状态:

Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim ports As String() = SerialPort.GetPortNames

            Dim port As String

            For Each port In ports
                ComboBox1.Items.Add(port)

            Next port

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        Try
            With SerialPort1
                .PortName = ComboBox1.Text
                .BaudRate = 9600
                .Parity = Parity.None
                .StopBits = StopBits.One
                .DataBits = 8
                .Handshake = Handshake.RequestToSend
                .DtrEnable = True
                .RtsEnable = True
                .NewLine = vbCrLf
                .Open()
                MsgBox("Connected !", MsgBoxStyle.Information)

            End With

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Try
            If SerialPort1.IsOpen Then
                With SerialPort1
                    .Write("AT" & vbCrLf)
                    .Write("AT+CMGF=1" & vbCrLf)
                    .Write("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf)
                    .Write(RichTextBox1.Text & Chr(26))

                    .Close()
                    MsgBox("Message Sent!", MsgBoxStyle.Information)


                End With

            Else
                MsgBox("Error on port")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

End Class

【问题讨论】:

  • 您能否阐明您遇到的确切问题以及它在代码中出现的位置,以便其他用户能够更好地帮助您?
  • 尝试连接 com3 端口时,一切正常。但是当我点击发送按钮时,没有发送消息。我也遇到了僵局。

标签: vb.net com sms


【解决方案1】:
Public Function sendMsg(ByVal port As SerialPort, ByVal PhoneNo As String, ByVal Message As String) As Boolean
        Dim isSend As Boolean = False

        Try

            Dim recievedData As String = ExecCommand(port,"AT", 300, "No phone connected")
            recievedData = ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.")
            Dim command As String = "AT+CMGS=""" & PhoneNo & """"
            recievedData = ExecCommand(port,command, 300, "Failed to accept phoneNo")
            command = Message & Char.ConvertFromUtf32(26) & vbCr
            recievedData = ExecCommand(port,command, 3000, "Failed to send message") '3 seconds
            If recievedData.EndsWith(vbCrLf & "OK" & vbCrLf) Then
                isSend = True
            ElseIf recievedData.Contains("ERROR") Then
                isSend = False
            End If
            Return isSend
        Catch ex As Exception
            Throw ex
        End Try

    End Function

执行命令

Public Function ExecCommand(ByVal port As SerialPort, ByVal command As String, ByVal responseTimeout As Integer, ByVal errorMessage As String) As String
        Try

            port.DiscardOutBuffer()
            port.DiscardInBuffer()
            receiveNow.Reset()
            port.Write(command & vbCr)

            Dim input As String = ReadResponse(port, responseTimeout)
            If (input.Length = 0) OrElse (((Not input.EndsWith(vbCrLf & "> "))) AndAlso ((Not input.EndsWith(vbCrLf & "OK" & vbCrLf)))) Then
                Throw New ApplicationException("No success message was received.")
            End If
            Return input
        Catch ex As Exception
            Throw ex
        End Try
    End Function

【讨论】:

  • 感谢您的回复。在这里,我没有正确理解它。请解释如何声明 ExecCommand ??
  • @user3005083 现在检查一下
  • 欲了解更多详情,请参阅此代码drive.google.com/file/d/0B1kYVIkNqZNpalpEY0FDM2VDbTA/…
  • 非常感谢。现在我的问题解决了。上面的代码没问题..问题在于连接错误的端口号。
  • 欢迎用户3005083
【解决方案2】:

你也可以试试这个编码..

Imports System
Imports System.Threading
Imports System.ComponentModel
Imports System.IO.Ports

Public Class Form1
    'connect your mobile/GSM modem to PC,
    'then go in device manager and check under ports which COM port has been slected
    'if say com1 is there then put com2 in following statement
    Dim SMSEngine As New SMSCOMMS("COM17")
    Dim i As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SMSEngine.Open() 'open the port
        SMSEngine.SendSMS() 'send the SMS

    End Sub

End Class
Public Class SMSCOMMS
    Private WithEvents SMSPort As SerialPort
    Private SMSThread As Thread
    Private ReadThread As Thread
    Shared _Continue As Boolean = False
    Shared _ContSMS As Boolean = False
    Private _Wait As Boolean = False
    Shared _ReadPort As Boolean = False
    Public Event Sending(ByVal Done As Boolean)
    Public Event DataReceived(ByVal Message As String)

    Public Sub New(ByRef COMMPORT As String)
        'initialize all values
        SMSPort = New SerialPort
        With SMSPort
            .PortName = COMMPORT
            .BaudRate = 19200
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .Handshake = Handshake.RequestToSend
            .DtrEnable = True
            .RtsEnable = True
            .NewLine = vbCrLf
        End With
    End Sub
    Public Function SendSMS() As Boolean
        Application.DoEvents()

        If SMSPort.IsOpen = True Then
            'sending AT commands
            SMSPort.WriteLine("AT")
            Thread.Sleep(500)
            SMSPort.WriteLine("AT+CMGF=1" & vbCrLf) 'set command message format to text mode(1)
            Thread.Sleep(500)

            '    SMSPort.WriteLine("AT+CSCA=""+919822078000""" & vbCrLf) 'set service center address (which varies for service providers (idea, airtel))
            SMSPort.WriteLine("AT+CMGS=" & """" & Form1.TextBox1.Text & """" & vbCrLf) ' enter the mobile number whom you want to send the SMS
            _ContSMS = False
            Thread.Sleep(500)

            SMSPort.WriteLine(Form1.TextBox2.Text & Chr(26)) 'SMS sending(specify the labl and the text box)
            MessageBox.Show(":send")
            SMSPort.Close()
        End If
    End Function

    Public Sub Open()
        If Not (SMSPort.IsOpen = True) Then
            SMSPort.Open()
        End If
    End Sub

    Public Sub Close()
        If SMSPort.IsOpen = True Then
            SMSPort.Close()
        End If
    End Sub
End Class

您必须将手机与 PC 套件或蓝牙或使用 USB 数据线连接。 之后你必须检查端口号并根据你的端口号更改编码部分。

Dim SMSEngine As New SMSCOMMS("COM17")

它对我有用..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 2014-12-21
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多