Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-06-24
14
Medium Priority
?
276 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
Independent Software Vendors: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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