By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Problems about VB101SamplesBCL2 - UsingTheSerialPort. Pls help me :)

P: 1
Hi everyone,

I am testing one of the example -> <UsingTheSerialPort> at VB101SamplesBCL2.

But there are some error which i don't know how to edit. I found some post that having the same problem with me.

I understand that i need to use Invoke method, because there is a different level of thread, so the update on the textBox is not valid..
But i really don't know where to add and what are the variables.

I at least need to show up something by this week. Pls help me.. :'(

This is the post from this forum that i found it from google.
Accessing Textboxes from threads
The answer over there is confusing me.. where is the head and tail?

The following is the program :
**The bold and underline is the error part..

ERROR: InvalidOperationException was unhandled
"Cross-thread operation not valid: Control 'textBox2' accessed from a thread other than the thread it was created on"

Also can refer to MyPreviousPost

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Text
  7. Imports System.Windows.Forms
  8. Imports System.IO.Ports
  9.  
  10. Class Form1
  11.     Inherits Form
  12.  
  13.     Public Sub New()
  14.         InitializeComponent()
  15.     End Sub 'New
  16.  
  17.     Private stopBits, parity As Array ' arrays to access the enumerations in System.IO.Ports
  18.     Private validStopBits As New ArrayList() ' ArrayLists hold the valid values for this machine
  19.     Private validParity As New ArrayList()
  20.  
  21.  
  22.     ' Send the text in the text box to the serial port.
  23.     ' The data should stay in the buffer until received by the event handler.
  24.     Private Sub SendButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  25.         Try
  26.             ' Write a line to the serial port
  27.             SerialPort1.WriteLine(textBox1.Text)
  28.         Catch ex As System.Exception
  29.             MessageBox.Show(ex.Message)
  30.         End Try
  31.     End Sub 'SendButton_Click
  32.  
  33.  
  34.     ' Event handler for data reception
  35.     Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
  36.         ' Read the buffer to the text box.
  37.         textBox2.Text = SerialPort1.ReadLine()
  38.     End Sub 'serialPort_DataReceived
  39.  
  40.  
  41.     ' Load the form and set up parameters from the default serial port.
  42.     ' Open the port and prepare it for IO.
  43.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  44.         Try
  45.             ' Open the serial port
  46.             SerialPort1.Open()
  47.             ' Set the event handler for data reception
  48.             AddHandler SerialPort1.DataReceived, AddressOf serialPort_DataReceived
  49.         Catch ex As System.Exception
  50.             MessageBox.Show(ex.Message)
  51.         End Try
  52.         label11.Text = SerialPort1.PortName
  53.  
  54.         label2.Text = SerialPort1.BaudRate.ToString()
  55.         label4.Text = SerialPort1.StopBits.ToString()
  56.         label6.Text = SerialPort1.Parity.ToString()
  57.         label8.Text = SerialPort1.DataBits.ToString()
  58.         label10.Text = SerialPort1.RtsEnable.ToString()
  59.  
  60.         ' Populate the stop bits box with all valid options.
  61.         ' Note that the serial port must be open to accurately test the options.
  62.         comboBox1.SelectedText = SerialPort1.StopBits.ToString()
  63.         Dim currentStopBitSetting As System.IO.Ports.StopBits = SerialPort1.StopBits
  64.  
  65.         stopBits = [Enum].GetValues(GetType(System.IO.Ports.StopBits))
  66.         Dim sbtype As System.IO.Ports.StopBits
  67.         For Each sbtype In stopBits
  68.             ' test to make sure the machine supports the setting
  69.             Try
  70.                 SerialPort1.StopBits = sbtype
  71.                 validStopBits.Add(sbtype)
  72.                 comboBox1.Items.Add(sbtype)
  73.             Catch ' ignore this entry as invalid
  74.             End Try
  75.         Next sbtype
  76.         SerialPort1.StopBits = currentStopBitSetting
  77.  
  78.         ' Populate the parity with valid options
  79.         comboBox2.SelectedText = SerialPort1.Parity.ToString()
  80.         Dim currentParitySetting As System.IO.Ports.Parity = SerialPort1.Parity
  81.         parity = [Enum].GetValues(GetType(System.IO.Ports.Parity))
  82.         Dim ptype As System.IO.Ports.Parity
  83.         For Each ptype In parity
  84.             ' test to make sure the machine supports the setting
  85.             Try
  86.                 SerialPort1.Parity = ptype
  87.                 validParity.Add(ptype)
  88.                 comboBox2.Items.Add(ptype)
  89.             Catch ' ignore this entry
  90.             End Try
  91.         Next ptype
  92.     End Sub 'Form1_Load
  93.  
  94.  
  95.     ' Respond to the form closing event by closing the SerialPort instance. 
  96.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
  97.         SerialPort1.Close()
  98.     End Sub 'Form1_FormClosing
  99.  
  100.  
  101.     ' The following methods demonstrate the ability to set parameters of the serial port
  102.     ' through the SerialPort instance.  The baud rate of the port will be set in response to
  103.     ' the corresponding button clicks.
  104.     Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
  105.         SerialPort1.BaudRate = 1200
  106.         label2.Text = SerialPort1.BaudRate.ToString()
  107.     End Sub 'button3_Click
  108.  
  109.  
  110.     Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button4.Click
  111.         SerialPort1.BaudRate = 4800
  112.         label2.Text = SerialPort1.BaudRate.ToString()
  113.     End Sub 'button4_Click
  114.  
  115.  
  116.     Private Sub button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
  117.         SerialPort1.BaudRate = 9600
  118.         label2.Text = SerialPort1.BaudRate.ToString()
  119.     End Sub 'button5_Click
  120.  
  121.  
  122.     Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox1.SelectedIndexChanged
  123.         SerialPort1.StopBits = CType(validStopBits(comboBox1.SelectedIndex), System.IO.Ports.StopBits)
  124.         label4.Text = SerialPort1.StopBits.ToString()
  125.     End Sub 'comboBox1_SelectedIndexChanged
  126.  
  127.  
  128.     Private Sub comboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox2.SelectedIndexChanged
  129.         SerialPort1.Parity = CType(validParity(comboBox2.SelectedIndex), System.IO.Ports.Parity)
  130.         label6.Text = SerialPort1.Parity.ToString()
  131.     End Sub 'comboBox2_SelectedIndexChanged
  132.  
  133. End Class 'Form1
Please reply me as soon as possible.
Thank you very much :)

Regard
Wanxi
Sep 19 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.