我的程序在使用gsm shield VB.net发送消息时冻结

时间:2016-01-17 09:02:37

标签: vb.net sms

我的程序在发送消息时挂起,我的代码显示没有错误,当我在超级终端测试我的gsm屏蔽时,我可以发送消息并通过我的手机使用AT命令接收它。但在我的程序中它只是冻结。

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("COM4")
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 = 9600
        .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
    If SMSPort.IsOpen = True Then
        'sending AT commands
        SMSPort.WriteLine("AT")
        SMSPort.WriteLine("AT+CMGF=1" & vbCrLf) 'set command message format to text mode(1)
        SMSPort.WriteLine("AT+CSCA="" +639170000130""" & vbCrLf) 'set service center address (which varies for service providers (idea, airtel))
        SMSPort.WriteLine("AT+CMGS=  + TextBox1.text + " & vbCrLf) ' enter the mobile number whom you want to send the SMS
        _ContSMS = False
        SMSPort.WriteLine("+ TextBox1.text +" & vbCrLf & Chr(26)) 'SMS sending
        MessageBox.Show(":send")
        SMSPort.Close()
    End If
End Function

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

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

1 个答案:

答案 0 :(得分:0)

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     SMSEngine.Open() 'open the port      
     If Not BackgroundWorker1.IsBusy Then
         BackgroundWorker1.RunWorkerAsync()
     End If
 End Sub

 Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
     If SMSPort.IsOpen = True Then
        'sending AT commands
        SMSPort.WriteLine("AT")
        SMSPort.WriteLine("AT+CMGF=1" & vbCrLf) 'set command message format to text mode(1)
        SMSPort.WriteLine("AT+CSCA="" +639170000130""" & vbCrLf) 'set service center address (which varies for service providers (idea, airtel))
        SMSPort.WriteLine("AT+CMGS=  + TextBox1.text + " & vbCrLf) ' enter the mobile number whom you want to send the SMS
        _ContSMS = False
        SMSPort.WriteLine("+ TextBox1.text +" & vbCrLf & Chr(26)) 'SMS sending
        MessageBox.Show(":send")
        SMSPort.Close()
     End If
  End Sub
相关问题