We help IT Professionals succeed at work.

Updating a label on a form when serial port data is recieved

564 Views
Last Modified: 2013-12-20
I have a simple form with a single label. I want to change the text of that label to display the data that has been received from the serial port. I have looked at various posts on the internet that talk of using invoke and delegates but I'm struggling to understand how they work.

I want to display the data as soon as its received with no button pushes etc.. I was looking at using the DataReceived function but i keep getting cross thread errors. I've tried reading up on it but to be honest I get lost. I've used vb 6 quite a lot and to be honest it looks a  lot simpler in that. Unfortunately I have to use .net.

Any ideas on how to solve this or a simple idiots guide to the invoke and delegate functions?

Many thanks
Comment
Watch Question

Maybe include the code your using?

Author

Commented:

Public Class Form1


   

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim msg As String
        msg = SerialPort1.ReadExisting
        lblsend.Text = " " & msg
        SerialPort1.Close()
        SerialPort1.Open()

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SerialPort1.Open()

    End Sub

   
End Class

Open in new window

Is lblsend a label or textfield?

Author

Commented:
its a label but equally it could be a text box if needed? I've tried making it a text box and I get the same error...
Could you post the actual error text as well as the code you are receieving?

Author

Commented:
System.InvalidOperationException was unhandled
  Message="Cross-thread operation not valid: Control 'txtrec' accessed from a thread other than the thread it was created on."
  Source="System.Windows.Forms"

Author

Commented:
sorry should have been

System.InvalidOperationException was unhandled
  Message="Cross-thread operation not valid: Control 'lblsend' accessed from a thread other than the thread it was created on."
  Source="System.Windows.Forms"

Author

Commented:
I tried changing it to a txt box hence the txtrec error
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Many thanks... I looked at that post a few days back before I realised I needed to use a delegate function. A good clear guide...
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.