nataraja833
New member
- Joined
- Dec 14, 2009
- Messages
- 3
- Programming Experience
- Beginner
i started with this code
then after going through this forum , http://www.vbdotnetforums.com/vb-net-general-discussion/31425-beginners-question-about-serial-ports-visual-basic-net.htmli have used Delegate stilll the code didnt work the same error saying
Cross-thread operation not valid:
this is the code
where is the problem !!!!
VB.NET:
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Forms
Public Class Form1
Dim rxBuff As String 'Buffer for receievd data
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "com1"
SerialPort1.BaudRate = 9600
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
Button2.Text = "close com1 port"
Button1.Text = "open com1 port"
Button3.Text = "exit"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
If SerialPort1.IsOpen = True Then SerialPort1.Close()
If SerialPort1.IsOpen = False Then MsgBox("com1 port closed
sucessfully")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
If SerialPort1.IsOpen = False Then SerialPort1.Open()
If SerialPort1.IsOpen = True Then MsgBox("com1 port opened sucessfully")
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal
e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived
rxBuff = (SerialPort1.ReadExisting)
textbox1.text=rxbuff 'error here
End Sub
Private Sub ReceiveData()
textbox1.text = rxBuff
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button3.Click
If SerialPort1.IsOpen = True Then SerialPort1.Close()
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox2.TextChanged
SerialPort1.Write(TextBox2.Text)
End Sub
End Class
then after going through this forum , http://www.vbdotnetforums.com/vb-net-general-discussion/31425-beginners-question-about-serial-ports-visual-basic-net.htmli have used Delegate stilll the code didnt work the same error saying
Cross-thread operation not valid:
this is the code
VB.NET:
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Forms
Public Class Form1
Dim rxBuff As String 'Buffer for receievd data
Private Delegate Sub AddListBoxItemInvoker(ByVal item As Object)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "com1"
SerialPort1.BaudRate = 9600
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
Button2.Text = "close com1 port"
Button1.Text = "open com1 port"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
If SerialPort1.IsOpen = True Then SerialPort1.Close()
If SerialPort1.IsOpen = False Then MsgBox("com1 port closed
sucessfully")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
If SerialPort1.IsOpen = False Then SerialPort1.Open()
If SerialPort1.IsOpen = True Then MsgBox("com1 port opened sucessfully")
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
ByVal e As
IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Me.AddListBoxItem(Me.SerialPort1.ReadLine())
End Sub
Private Sub AddListBoxItem(ByVal item As Object)
If Me.ListBox1.InvokeRequired Then
'We are on a secondary thread so delegation is required.
Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf
AddListBoxItem), _
item)
Else
'We are on the primary thread so add the item.
Me.ListBox1.Items.Add(item)
End If
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox2.TextChanged
SerialPort1.Write(TextBox2.Text)
End Sub
End Class
where is the problem !!!!
Last edited: