Solved

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

Posted on 2006-06-24
14
257 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

12 Experts available now in Live!

Get 1:1 Help Now