Solved

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

Posted on 2006-06-24
14
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

635 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