Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capturing AT responses and showing them on a label

Status
Not open for further replies.

comboy

Instructor
May 23, 2003
226
Hi All,

I'm trying to design a sms application, I've done most of the work in that it can send sms both in plain and pdu mode but I'm stuck on one thing, getting the AT command responses once the send message button is clicked to show on a label on the application form itself.

I've written a procedure that will capture the respose from the serial port class to a class level string variable called response and the responses do show up on a message box but I can't seem to get the same text to show on the label.

Code:
Private sub dataReceived (ByVal sender as Object, ByVal e As SerialDataReceivedEventArgs) Handels sPort.DataReceived

dim receivedDataLenght As Long

Try  
    response = sPort.ReadExisting

    receivedDataLenght += response.Length

    MsgBox("Received: " & response)

Catch ex As Exception
   MsgBox (ex.ToString)
End Try
End Sub


I'm new at VB so if I'm asking a silly question sorry just looking for a solution as I can't find exactly what I'm looking for via Google.
 

Set up an event in your class to do this:

Code:
Public Event ResponseRecieved(ByVal Response As String)


Then in your dataReceived sub:

Code:
Private sub dataReceived (ByVal sender as Object, ByVal e As SerialDataReceivedEventArgs) Handels sPort.DataReceived

dim receivedDataLenght As Long

Try  
    response = sPort.ReadExisting

    receivedDataLenght += response.Length

    [red]RaiseEvent ResponseReceived(response)[/red]

Catch ex As Exception
   MsgBox (ex.ToString)
End Try
End Sub


Next, in the code that uses the class, Dim the class object WithEvents:

Code:
Dim WithEvents MyClass As New MyClass

Finally, set up the event handler for the ResponseReceived event:

Code:
Private Sub MyClass_ResponseReceived(ByVal Response As String) Handles MyClass.ResponseReceived
    Label1.Text = Response
    Application.DoEvents()
End Sub

Hope this helps.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Cheers jebenson I'll give the code a shot and see how it goes.

G
 
Hi Jebenson,

The code did not work as I'm sure I did not provide you with enough information sorry my fault.

The application is just one class apart from a PDU class imported as a module.

I've set the following code already within the public class declaration and I think that is where I'm getting confused with the code you already supplied.

Code:
Imports System.IO.Ports
Public Class frmSmartSMS
    ' Create a instance of the serial port class
    Dim WithEvents sPort As New SerialPort

    Dim dlgSelectPort As New frmComSelect

    ' Set a variable to 
    ' get the AT responses
    Dim response As String

    'Set a variable to determine the length of the sms message
    Dim smsMessLen As Integer

    Public Event responseReceived(ByVal response As String)

    Private sEventRXEventHandles As New SerialDataReceivedEventHandler(AddressOf dataReceived)

    Private Sub dataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles sPort.DataReceived

        Dim receivedDataLength As Long

        Try
            'get the data from the port
            response = sPort.ReadExisting

            'save the number of characters received
            receivedDataLength += response.Length

            RaiseEvent responseReceived(response)
            'show the response in a messagebox
            MsgBox("Received: " & response)

            'catch any errors and dispaly them
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

I think because I've already declared the serial port class with events and the event handler sEventRXEventHandles I can't get your code to work as suggested.


Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top