Cannot access control on form from delegate routine

Discussion in 'Visual Basic ( VB )' started by Orbitcoms, Apr 16, 2015.

  1. Orbitcoms

    Orbitcoms New Member

    Jan 20, 2015
    Likes Received:
    Trophy Points:
    I am wiring a serial port handler class. The transmit and receive work and correct data is appearing in the ComBuffer in Serial_PortDataReceived event handler.

    Next, I want to show the data in a label on the form so set up a delegate function "UpdateDisplay". In thi sroutine I convert the data byte array to string (mystr) and attempt to update the label1.text component on the form but it appears to not do anything. In the adjacent line I display mystr in a messagebox - this line works fine.

    Any help much appreciated, I am new to delegates and threading (in fact new to OOP).
    Imports System.IO.Ports
    Public Class SerialConnection
        Dim port As SerialPort
        Private Delegate Sub UpdateFormDelegate()
        Private UpdateFormDelegate1 As UpdateFormDelegate
        Private comBuffer As Byte()
        Public Sub New(ByVal ComPort As String)
            Me.port = New SerialPort
            AddHandler port.DataReceived, AddressOf SerialPort_DataReceived
            With Me.port
                .PortName = ComPort
                .BaudRate = 115200
            End With
        End Sub
        Public Sub Open()
            If Not Me.port.IsOpen Then
                    MessageBox.Show(Me.port.PortName & " Open")
                Catch ex As Exception
                    MessageBox.Show(ex.Message & vbCrLf & "Select a port from the list")
                End Try
            End If
        End Sub
        Public Sub close()
            If Not Me.port.IsOpen Then Me.port.Close()
            MessageBox.Show("Port Closed")
        End Sub
        Public Sub Send(ByVal txdata As String)
            If Me.port.IsOpen Then
                If txdata <> "" Then
                    MessageBox.Show("No Data to send")
                End If
                MessageBox.Show("Cannot send Message, Port not open")
            End If
        End Sub
        Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
            Dim n As Integer = Me.port.BytesToRead
            comBuffer = New Byte(n - 1) {}
            Me.port.Read(comBuffer, 0, n)
            UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
        End Sub
        Private Sub UpdateDisplay()
            Dim mystr As String = System.Text.ASCIIEncoding.ASCII.GetString(comBuffer)
            If Form1.Label1.InvokeRequired Then
                Form1.Label1.Invoke(New MethodInvoker(AddressOf UpdateDisplay))
                MessageBox.Show(mystr)   ' this line shows received data ok
                Form1.Label1.Text = mystr  'this line is executed but appears to do nothing
            End If
        End Sub
    End Class
    Last edited by a moderator: Apr 17, 2015

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice