GAiXz 0 Newbie Poster

GOOD DAY .NET PROGRAMMERS!! ^_^

http://www.daniweb.com/forums/attachment.php?attachmentid=23184&stc=1&d=1323465600

I'm having a problem with SMS APPLICATION "see the picture"?

when i click the SEND button after typing the number of the recipient and the text messages
it's not responding same as the LOAD button and nothing happens after that so i have no choice but to close the application

and i don't know if i'm really connected to port...
please check it if the code for the connection port is correct...

i really need help please...

here's the code:

for the windows form ConnSMS

Imports System.IO.Ports

Public Class ConnSMS
    Dim WithEvents SerialPort As New IO.Ports.SerialPort
    Private Declare Sub Sleep Lib "kernel32" (ByVal milsec As Long)

    Dim ObjPort As New SMSclass

    Private Sub ConnSMS_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
            cmbPort.Items.Add(My.Computer.Ports.SerialPortNames(i))
        Next

    End Sub

    Private Sub btnSend_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If ObjPort.SendSMS(SerialPort, txtTO.Text, txtWto.Text) Then
            MsgBox("Message Sent!", MsgBoxStyle.Information)
        Else
            MsgBox("Message Sending Failed!", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub btnLoad_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim ObjShortMessageCollection = ObjPort.ReadSMS(SerialPort, txtTO.Text)

        For Each msg As ShortMessage In ObjShortMessageCollection
            Dim item As New ListViewItem(New String() {msg.Index, msg.Sent, msg.Sender, msg.Message})
            item.Tag = msg

            ListView1.Items.Add(item)
        Next
    End Sub

    Private Sub btnConnect_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Try
            SerialPort = ObjPort.OpenPort(cmbPort.Text)
            If SerialPort.IsOpen Then
                lblstatStrip.Text = "Modem is connected at PORT " & cmbPort.Text & "."
            Else
                lblstatStrip.Text = "Invalid port settings."
            End If
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Private Sub btnDisconnect_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        Try
            ObjPort.ClosePort(SerialPort)
            lblstatStrip.Text = "Not Connected"
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Private Sub txtWto_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWto.TextChanged
        TextBox1.Text = Len(txtWto.Text)
    End Sub
End Class

and for the class file SMSclass

Imports System.IO.Ports
Imports System.Text
Imports System.Threading
Imports System.Text.RegularExpressions

Public Class SMSclass
    Private Declare Sub Sleep Lib "kernel32" (ByVal milsec As Long)
    Public RecieveNow As New AutoResetEvent(False)
    Shared ReadNow As New AutoResetEvent(False)
    Public Function OpenPort(ByVal ComPort As String) As SerialPort

        Dim SerialPort As New IO.Ports.SerialPort

        Try
            If ComPort <> Nothing Then
                With SerialPort
                    .PortName = ComPort
                    .BaudRate = 9600
                    .Parity = Parity.None
                    .DataBits = 8
                    .StopBits = StopBits.One
                    .Handshake = Handshake.RequestToSend
                    .DtrEnable = True
                    .RtsEnable = True
                    .NewLine = vbCrLf
                End With
                SerialPort.Open()
                Return SerialPort
            Else
                Return SerialPort
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Sub ClosePort(ByVal Serialport As SerialPort)
        Try
            Serialport.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Function ReadResponse(ByVal Port As SerialPort, ByVal TimeOut As Integer)

        Dim Buff As String = ""
        Try
            Do
                Dim t As String = Port.ReadExisting()
                Buff = Buff & t
            Loop Until Buff.EndsWith(vbCrLf & "Ok" & vbCrLf) Or Buff.EndsWith(vbCrLf & "> ") Or Buff.EndsWith(vbCrLf & "ERROR" & vbCrLf)
        Catch ex As Exception
            Throw ex
        End Try
        Return Buff
    End Function

    Public Sub PortDataRecieved(ByVal Sender As Object, ByVal e As SerialDataReceivedEventArgs)

        If e.EventType = SerialData.Chars Then
            ReadNow.Set()
        End If

    End Sub

    Private Shared Sub DataReceived(ByVal Sender As Object, ByVal e As SerialDataReceivedEventArgs)
        If e.EventType = SerialData.Chars Then
            ReadNow.Set()
        End If
    End Sub


    Public Function ExecCommand(ByVal Port As SerialPort, ByVal StrCommand As String, ByVal ErrMess As String)

        Port.WriteLine(StrCommand)
        Dim Input As String = ReadResponse(Port, 400)
        ConnSMS.SendMsg.Text = ConnSMS.SendMsg.Text & Input
        Return Input

    End Function

    Public Function SendSMS(ByVal Port As SerialPort, ByVal PhoneNo As String, ByVal Message As String) As Boolean

        Dim IsSent As Boolean = False
        Dim ReceiveData As String = ""

        ReceiveData = ExecCommand(Port, "AT" & vbCr, "Unable to connect.")
        System.Threading.Thread.Sleep(200)

        ReceiveData = ExecCommand(Port, "AT+CMGF=1" & vbCr, "Failed to set message format.")
        System.Threading.Thread.Sleep(200)

        ReceiveData = ExecCommand(Port, "AT+CMGS=" & Chr(34) & PhoneNo & Chr(34) & vbCr, "Failed to accept PhoneNo")
        System.Threading.Thread.Sleep(200)

        ReceiveData = ExecCommand(Port, vbBack & vbBack & "FROM PICTU SMS: " & Message & Chr(26), "Failed to send message")
        System.Threading.Thread.Sleep(200)

        If ReceiveData.EndsWith(vbCrLf & "OK" & vbCrLf) Then
            IsSent = True
        ElseIf ReceiveData.Contains("ERROR") Then
            IsSent = False
        End If
        Return IsSent
    End Function

    Public Function ParseMessages(ByVal Input As String) As ShortMessageCollection
        Dim Messages As New ShortMessageCollection()
        Try
            Dim r As New Regex("\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n")
            Dim m As Match = r.Match(Input)
            While m.Success
                Dim msg As New ShortMessage()
                msg.Index = m.Groups(1).Value
                msg.Status = m.Groups(2).Value
                msg.Sender = m.Groups(3).Value
                msg.Alphabet = m.Groups(4).Value
                msg.Sent = m.Groups(5).Value
                msg.Message = m.Groups(6).Value
                Messages.Add(msg)

                m = m.NextMatch()

            End While
        Catch ex As Exception
            Throw ex
        End Try
        Return Messages
    End Function


    Public Function ReadSMS(ByVal Port As SerialPort, ByVal Command As String)
        Dim Messages As ShortMessageCollection = Nothing
        Dim ReceivedData As String = ""

        ExecCommand(Port, "AT" & vbCr, "Unable to connect.")
        System.Threading.Thread.Sleep(200)
        ExecCommand(Port, "AT+CMGF=1" & vbCr, "Failed to set message format.")
        System.Threading.Thread.Sleep(200)
        ExecCommand(Port, "AT+CSCS=" & Chr(34) & "PCCP437" & Chr(34), "Failed to set character set.")
        System.Threading.Thread.Sleep(200)
        ExecCommand(Port, "AT+CPMS=" & Chr(34) & "ME" & Chr(34), "Failed to select message storage.")
        System.Threading.Thread.Sleep(200)
        ReceivedData = ExecCommand(Port, "AT+CMGL=" & Chr(34) & "REC UNREAD" & Chr(34), "Failed to read all message.")
        System.Threading.Thread.Sleep(200)
        Messages = ParseMessages(ReceivedData)

        If Not Messages Is Nothing Then
            Return Messages
        Else
            Return Nothing
        End If
    End Function

End Class

and another class file ShortMessage

Public Class ShortMessage
    Public Index As String
    Public Status As String
    Public Sender As String
    Public Alphabet As String
    Public Sent As String
    Public Message As String
End Class

andlast class file ShortMessageCollection

Public Class ShortMessageCollection
    Inherits List(Of ShortMessage)
End Class

please help O_O? co'z i don't know why it's not responding...
i know that the code is right... i justwant to know if i'm having a problem with the AT commands??

please .Net developers.... help me with this ^_^

Thank you in advance!! ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.