Solved

Need help trying to get Caller ID info through the SerialPort in VB 2005

Posted on 2006-06-24
14
264 Views
Last Modified: 2012-05-05
Hello,

I am using a device (yes-tele identifier) to retrieve caller id info. The phone lines are fed into the device. The device is then hooked up into the serial port. I am trying to use VB 2005 to read the info via the SerialPort control. I can detect when something comes in, but I keep getting errors.

Has anyone been able to retreive caller id info using the above device in VB 2005?

Thanks
0
Comment
Question by:dijon316
  • 7
  • 4
14 Comments
 

Author Comment

by:dijon316
ID: 16975783
Here is the current code I am using...

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim strValue As String
        Try
            Do
                Dim Incoming As String = SerialPort1.ReadLine
                If Incoming Is Nothing Then
                    Exit Do
                Else
                    strValue += Incoming & vbCrLf
                    txtInfo.Text = strValue
                End If
            Loop
        Catch ex As Exception
            MessageBox.Show("error")
        End Try

    End Sub

I am getting the following exception...

Cross-thread operation not valid: Control 'txtInfo' accessed from a thread other than the thread it was created on.

Any ideas?
0
 

Author Comment

by:dijon316
ID: 16976000
From what I can find already, the reason I am getting the error is because SerialPort1_DataReceived runs on a different thread. I get the error when I try to reference a control on my main form from the DataReceived routine.

Looks like I need to find a way to reference a text box which is on a different thread.
0
 
LVL 14

Expert Comment

by:ptakja
ID: 16976078
You are correct. A non-UI thread cannot modify objects running on the UI thread. I assume that the code you have to monitor the serial port is encapsulated in a class. If so, try adding an Event to the class that you can raise and pass the string through the event to the UI thread that is handling the event.

Something like this:

Public Event CallerIDDataReceived(ByVal Data As String)

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim strValue As String
        Try
            Do
                Dim Incoming As String = SerialPort1.ReadLine
                If Incoming Is Nothing Then
                    Exit Do
                Else
                    strValue &= Incoming & vbCrLf   '<<<<<< Should use "&=" here instead of "+=". += can lead to wierd behavior when dealing with numbers....
                End If
            Loop

            'If you have something to send back, raise the event and send it.
            If strValue.Length > 0 Then
                  RaiseEvent CallerIDDataReceived(strValue)
            End If

        Catch ex As Exception
            MessageBox.Show("error")
        End Try

    End Sub



Then in on your form, you have an event handler

Private Sub MyEventHandler(ByVal Data As String) Handles YourObject.CallerIDDataReceived
       TextBox1.Text = Data
End Sub

Indicentally, you would need to declare your serial port object with events like this:

Private WithEvents MySerialObject As MySerialObject


0
 
LVL 14

Expert Comment

by:ptakja
ID: 16976082
There is another way to do it with delegates and PInvoke, but that is a little more complex and I don't have an example readily at hand. If you want to go that way, reply here and I'll dig up the code.
0
 

Author Comment

by:dijon316
ID: 16976369
When I entered the above code, I still get the same exception. This time it happened in the "Private Sub MyEventHandler". Everything was fine until it hit the main form, and then I got the exception.
0
 

Author Comment

by:dijon316
ID: 16976370
btw, do you do any consulting?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dijon316
ID: 16976397
Woops, I just saw that I was not supposed to ask about consulting. Sorry!
0
 
LVL 14

Expert Comment

by:ptakja
ID: 16977458
Hmmm. I would have to see your code to find out what is going on.

And yes. I do. Send a message to jptak@stny.rr.NOSPAM.com

Just remove the NOSPAM above.
0
 
LVL 14

Expert Comment

by:ptakja
ID: 16977461
Are you sure the data is being passed by value? If it is by Ref, then you would end up in the same boat.
0
 

Author Comment

by:dijon316
ID: 16983209
Here is the class code...

Public Class Identifier

    Private WithEvents mySerialPort As New IO.Ports.SerialPort
    Public Event CallerIDDataReceived(ByVal Data As String)

    Sub New()
        With mySerialPort
            .PortName = "COM1"
            .BaudRate = 4800
            .DataBits = 8
            .Handshake = IO.Ports.Handshake.RequestToSend
            .Parity = IO.Ports.Parity.None
            .ReadBufferSize = 4096
            .ReadTimeout = -1
            .Open()
        End With
    End Sub

    Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles mySerialPort.DataReceived
        Try
            Dim strValue As String = ""
            Dim Incoming As String = mySerialPort.ReadLine
            If Incoming Is Nothing Then
                Exit Sub
            Else
                strValue &= Incoming & vbCrLf
            End If

            'If I have something to send back, then raise the event and send it.
            If strValue.Length > 0 Then
                RaiseEvent CallerIDDataReceived(strValue)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Sub Dispose()
        mySerialPort.Close()
    End Sub
End Class



Here is the main form code....

Imports System.Text
Imports System.IO.Ports


Public Class Form1

    Private WithEvents MyIdentifier As New Identifier

    Private Sub MyEventHandler(ByVal Data As String) Handles myIdentifier.CallerIDDataReceived
        txtInfo.Text = Data
    End Sub

End Class


I hope this Helps.

Thanks
0
 

Author Comment

by:dijon316
ID: 16985892
I found the solution at the following web site:

http://www.devx.com/dotnet/Article/31001/1954?pf=true

I explains how to set up a chat program with SerialPort, but can be easily modified to receive caller ID data.

Thanks for your help!
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 17232332
PAQed with no points refunded (of 500)

DarthMod
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AutoHotkey ActiveX Component Error 8 72
DataGridview Currency Formating? 22 53
Clone Visual studio 2013 solution 5 25
VB.NET Repostiory Pattern 7 14
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now