Problem with serial port VB 2008

nataraja833

New member
Joined
Dec 14, 2009
Messages
3
Programming Experience
Beginner
i started with this 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 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:
Hi,

I tried your code under Visual Studio 2008, Win7 (and XP) but no luck. To start with I was getting " Port is closed" error message. This is caused by
the textCahnge on TextBox2 as follows:


Private Sub TextBox2_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox2.TextChanged
SerialPort1.Write(TextBox2.Text)
End Sub

You would get this as soon as you start the prog because the textchange is tue at form load which tries to write to the port before it is opened.
So, did you get this? I was able to re3solev this by ensuring that the port is open before this takes effect. However, the SerialPOrt1.DataReceived necer gets invoked? I wonder if you have resolved this and how?

Kind regards
Timo
 
this code was a part of my serial servo controller
when ever microcontroller sends data to PC
SerialPOrt1.DataReceived acutomatically get's invoked ,
this worked perfect as i have completed my project ;)
the data which the microcontroller's has sent will be displayed in textbox
 
Last edited by a moderator:
Back
Top