Solved

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

Posted on 2006-06-24
14
266 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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