Question DataReceived Event of SerialPort class not firing

Akanksha

New member
Joined
Apr 2, 2012
Messages
2
Programming Experience
Beginner
Hi,
my apologies, if this isn't the right section to post in
but i really did not know where else to go.

my problem, apparantly faced by many, is that the DataReceived event is not firing at all.
i have an Arduino attached to COM 9 and i am sending some data to it which correspondingly drives some motors.
what i thought of doing was reflect back the data that the Arduino receives so as to know exactly whats going on there.
but it is not turning out to be that easy.
my code is as follows:

Imports System.IO
Imports System.Windows.Forms
Imports System.Text
Imports System.Threading
Imports System.IO.Ports
Public Class Form1
WithEvents _serialPort As New SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_serialPort.Close()
_serialPort.PortName = "COM9"
_serialPort.BaudRate = 9600
_serialPort.DataBits = 8
_serialPort.Parity = Parity.None
_serialPort.StopBits = StopBits.One
_serialPort.Handshake = Handshake.None
_serialPort.Encoding = System.Text.Encoding.Default
End Sub

Private Sub wr_speed_ValueChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles wr_speed.ValueChanged
Dim wr_speed_value As String
wr_speed_value = System.Math.Round(wr_speed.Value)
TextBox1.Text = wr_speed_value
_serialPort.Open()
_serialPort.Write(wr_speed_value)
If (_serialPort.BytesToRead > 0) Then
While (_serialPort.BytesToRead > 0)
Dim i As Long
i = _serialPort.ReadByte()
rtb.Text = rtb.Text + "<" + Str(i) + ">"
rtb.Refresh()
End While
Else
rtb.Text = rtb.Text + "<no bytes to read>"
End If
_serialPort.Close()
End Sub


Private Sub wr_forth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wr_forth.Click
Dim wr_forth_send As String = "6"
_serialPort.Open()
_serialPort.Write(wr_forth_send)
_serialPort.Close()
End Sub

Private Sub wr_back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wr_back.Click
Dim wr_back_send As String = "7"
_serialPort.Open()
_serialPort.Write(wr_back_send)
If (_serialPort.BytesToRead > 0) Then
While (_serialPort.BytesToRead > 0)
Dim i As Long
i = _serialPort.ReadByte()
rtb.Text = rtb.Text + "<" + Str(i) + ">"
rtb.Refresh()
End While
Else
rtb.Text = rtb.Text + "<no bytes to read>"
End If
_serialPort.Close()
End Sub

Private Sub wr_right_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wr_right.Click
Dim wr_right_send As String = "8"
_serialPort.Open()
_serialPort.Write(wr_right_send)
_serialPort.Close()
End Sub

Private Sub wr_left_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wr_left.Click
Dim wr_left_send As String = "9"
_serialPort.Open()
_serialPort.Write(wr_left_send)
_serialPort.Close()
End Sub

Private Sub wf_fold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wf_fold.Click
Dim wf_fold_send As String = "100"
_serialPort.Open()
_serialPort.Write(wf_fold_send)
_serialPort.Close()
End Sub

Private Sub wf_unfold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wf_unfold.Click
Dim wf_unfold_send As String = "101"
_serialPort.Open()
_serialPort.Write(wf_unfold_send)
_serialPort.Close()
End Sub

Private Sub tr_speed_ValueChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles tr_speed.ValueChanged
Dim tr_speed_value As String
tr_speed_value = System.Math.Round(tr_speed.Value)
TextBox2.Text = tr_speed_value
_serialPort.Open()
_serialPort.Write(tr_speed_value)
_serialPort.Close()
End Sub

Private Sub tr_forth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tr_forth.Click
Dim tr_forth_send As String = "36"
_serialPort.Open()
_serialPort.Write(tr_forth_send)
_serialPort.Close()
End Sub

Private Sub tr_back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tr_back.Click
Dim tr_back_send As String = "37"
_serialPort.Open()
_serialPort.Write(tr_back_send)
_serialPort.Close()
End Sub

Private Sub mr_speed_ValueChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mr_speed.ValueChanged
Dim mr_speed_value As String
mr_speed_value = System.Math.Round(mr_speed.Value)
TextBox3.Text = mr_speed_value
_serialPort.Open()
_serialPort.Write(mr_speed_value)
_serialPort.Close()
End Sub

Private Sub mr_up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mr_up.Click
Dim mr_up_send As String = "91"
_serialPort.Open()
_serialPort.Write(mr_up_send)
_serialPort.Close()
End Sub

Private Sub mr_down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mr_down.Click
Dim mr_down_send As String = "92"
_serialPort.Open()
_serialPort.Write(mr_down_send)
_serialPort.Close()
End Sub

Private Sub mr_right_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mr_right.Click
Dim mr_right_send As String = "93"
_serialPort.Open()
_serialPort.Write(mr_right_send)
_serialPort.Close()
End Sub

Private Sub mr_left_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mr_left.Click
Dim mr_left_send As String = "94"
_serialPort.Open()
_serialPort.Write(mr_left_send)
_serialPort.Close()
End Sub

Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles _serialPort.DataReceived
If (_serialPort.IsOpen() = False) Then
_serialPort.Open()
End If
Dim indata As String = _serialPort.ReadExisting()
If (_serialPort.BytesToRead > 0) Then
While (_serialPort.BytesToRead > 0)
Dim i As Long
i = _serialPort.ReadByte()
rtb.Text = rtb.Text + "<" + Str(i) + ">"
rtb.Refresh()
End While
Else
rtb.Text = rtb.Text + "<no bytes to read>"
End If
End Sub

End Class


i am able to reflect the received value in the rich text box 'rtb' when i do this:



Private Sub wr_back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wr_back.Click
Dim wr_back_send As String = "7"
_serialPort.Open()
_serialPort.Write(wr_back_send)
If (_serialPort.BytesToRead > 0) Then
While (_serialPort.BytesToRead > 0)
Dim i As Long
i = _serialPort.ReadByte()
rtb.Text = rtb.Text + "<" + Str(i) + ">"
rtb.Refresh()
End While
Else
rtb.Text = rtb.Text + "<no bytes to read>"
End If
_serialPort.Close()
End Sub


but as a part of a separate sub built to fire DataReceived event on SerialPort object the same code doesn't fire.

i don't know where i am going wrong.
this is my first vb.net code.

i don't expect someone to give me the right code..
but just some nudge in the right direction i guess would be very helpful.
thanks.
 
SerialPort must be open in order to receive events from it, from what I can see you always close the port immediately after sending something to it.

You also have an error in DataReceivedHandler, read the remarks about threading in SerialPort.DataReceived Event help page.
 

Latest posts

Back
Top