View Single Post
  #1 (permalink)  
Old 01-08-2009, 7:52 PM
jbrookley jbrookley is offline
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Jan 2009
Posts: 3
Reputation: 0
jbrookley is on a distinguished programming path ahead
Default Beginner's Question About Serial Ports in VB.net

Hello. I'm primarily a hardware engineer but I just recently started working with microcontrollers and I'm now working on my software coding. I programmed an Atmel chip to receive data from the RS232 port and send it back to the computer where it is received successfully by hyperterminal.

I am now trying to make a VB.net application (using VB Express 2008) that will send data to the RS232 port where the microcontroller will send it back and it can be viewed on hyperterminal.

Below is the code I've been working with. I basically have a button (Button2) that opens and closes the serial port, textbox 4 where I can write code to be sent, and button3 which sends the code to the RS232 port. It should display what is sent as well as what is received on listbox1.

Enclosed is the code I have:

Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ((CheckBox1.Checked = True) And (CheckBox2.Checked = True)) Then
            TextBox1.Text = "Both"
        ElseIf (CheckBox1.Checked = True) Then
            TextBox1.Text = "One"
        ElseIf (CheckBox2.Checked = True) Then
            TextBox1.Text = "Two"
        Else
            TextBox1.Text = "You suck"
            TextBox2.Clear()
            MsgBox("Learn to Read, Noob!", MsgBoxStyle.Critical, "YOU FAIL")
        End If
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        TextBox2.Text = "You should pick one . . ."
        If (ComboBox1.SelectedItem = "Apple") Then
            TextBox2.Text = "Apple!"
        ElseIf (ComboBox1.SelectedItem = "Orange") Then
            TextBox2.Text = "Orange!"
        ElseIf (ComboBox1.SelectedItem = "Monkey") Then
            TextBox2.Text = "Monkey!"
        ElseIf (ComboBox1.SelectedItem = "Donkey") Then
            TextBox2.Text = "Donkey!"
        End If
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub

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

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If SerialPort1.IsOpen = False Then
            SerialPort1.Open()
            If SerialPort1.IsOpen() Then
                CheckBox3.Checked = True
                TextBox3.Text = "Port Open"
            Else
                CheckBox3.Checked = False
                TextBox3.Text = "Error: Port Closed"
            End If
        ElseIf SerialPort1.IsOpen = True Then
            SerialPort1.Close()
            If SerialPort1.IsOpen() Then
                CheckBox3.Checked = True
                TextBox3.Text = "Error: Port Open"
            Else
                CheckBox3.Checked = False
                TextBox3.Text = "Port Closed"
            End If
        End If
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ListBox1.Items.Add("Received: " + SerialPort1.ReadLine())
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged

    End Sub

    Private Sub TextBox3_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        SerialPort1.WriteLine(TextBox4.Text)

        ListBox1.Items.Add("Sent: " + TextBox4.Text)
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged

    End Sub
End Class
I apologize for having some additional code (I've just been playing with this, learning how everything works) so some of it is unrelated but I figured I'd post everything just in case.

The error I'm seeing is:

"Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on."

Does anyone happen to know what this error entails or how I can fix it? Please let me know when you get the chance. Thanks in advance!
Reply With Quote