473,233 Members | 1,409 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,233 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 1317

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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.