Changing form controls after receiving serial port data

Lucretius

New member
Joined
Feb 12, 2009
Messages
4
Programming Experience
3-5
Hello all, first post here at the forums. I am a complete noob when it comes to VB .Net, but I have done a lot of VBA programming and some VB6 back in the day. Forgive me if this post is in the wrong forum, as I said I am new here.

I am (trying) to write my first .net app which reads/writes data to a device connected over the serial port. I have manged to get it to work to send commands to the device and receive the reply back, mainly using the info from this article: Beth Massi - Sharing the goodness that is VB : Reading from a Serial Port and Saving to a Database

However, I am having problems with updating the controls on the main form based on the received data from the serial port. Basically I want the user to click a button (one of six on the form) which will send data to the device, then based on the response from the device, the BackColor of that particular button, will be changed accordingly. I just am missing something b/c the button click is a different sub than the sub that handles the form controls update and I can't get the two to talk. Any suggestions?

VB.NET:
Public Class frmCarouselControl

    Dim m_strResponse As String   'holds the value of the response from the serial device

    Private Sub btnBottle1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBottle1.Click

        Try
            SerialPort_Carousel.Write("#SN1")
 
          '################ I don't think I can check the status of the recieved data here, need to use "UpdateControls Method"
            'If m_strResponse = Chr(Asc(m_strResponse) - 48) Then
            '    Me.btnBottle1.BackColor = Color.Green  'need to check reply for sbe before changing color
            'Else
            '    Me.btnBottle1.BackColor = Color.Red
            'End If

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub SerialPort_Carousel_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
    Handles SerialPort_Carousel.DataReceived

        'This happens on another thread
        m_strResponse = Mid(SerialPort_Carousel.ReadExisting(), 2, 1)  'get the response from the device

        Me.Invoke(New EventHandler(AddressOf UpdateControls))  'update form controls on another thread

    End Sub

Private Sub UpdateControls(ByVal sender As Object, ByVal e As EventArgs)
'Do any UI code here on the main thread
'#########################  Here is where I think i need to update the backcolor of the button , but how?

'some thing like:

"If user clicked "Button1", then change Button1.Backcolor depending on value of m_strResponse (e.g. Red if response is bad, Green if good)
If user clicked Button1 then change Button2.Backcolor....
etc.


End Sub
 
Back
Top