473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problems about VB101SamplesBCL2 - UsingTheSerialPort. Pls help me :)

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
0 1324

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

Similar topics

0
by: Jerome Lefebvre | last post by:
Hello, Hope this will interest a few. I been working with a friend on the problems given out during the "International Collegiate Programming Contest" (ICPC) http://icpc.baylor.edu/icpc/ ....
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
1
by: 3f | last post by:
Hello; We have made a web application that people can download from our web site and installed on: Windows XP Windows 2000 Professional Windows 2003 Server Windows 2000 Server
5
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) -...
2
by: Ellen Graves | last post by:
I am having a lot of problems with DB2 8.3.1 on RH Linux AS2.1. Installing and running stored procedures is problematic. Stored procedures I have used for years on V7 on WinNT are now failing...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
0
by: Sergistm | last post by:
Hello World, :D I have a problem that it is making me crazy, I hope you can help me. I'm trying to execute a .exe file with the Procces.Start, and there is no problem when the file is on my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.