How to successfully read and process data received through the Visual . NET serial port?

Asked

Viewed 51 times

2

I got a arduino who is the detect tapping on a signal line audio level.

Whenever it is detected a beat the arduino sends the character b to the serial port.

So far I’ve managed to do.

Now I was trying to make a small graphical interface that shows the beats per minute in visual .NET.

I mean, I have a Serialport1 and whenever she receives a b, a timer ( *with an interval of 1 millisecond closed in the properties of the timer ) until the next b received.

The value of time which passed between the arrival of two b must be stored in a variable.

Then do the same for the according to b received and the third.

Finally do this until you have 4 values time in 4 variables.

After having 4 variables it is necessary to divide 60000 milliseconds for average among the 4 variables and show the result ( bpm - beats per minute ) in a TextBox.

Then repeat everything ( start receiving again the b, store the time variables and show the calculation of the bpm in TextBox, thus making a refresh of value ).

Since I’m a little inexperienced at programming, I’d like to know how to get this thing up and running in the visual .NET since I’m not getting it.

I can "connect" to Arduin but there seems to be something wrong with the code that is within the "while(true)" cycle. The calculation of bpm’s does not appear in the textbox, instead "+/-infinite" appears or nothing appears. What is wrong ?

I know that later it will be necessary to implement code to break the infinite cycle but for now I just wanted to show the calculation of bpm’s in the textbox.

Thank you very much.

I tried this code:

Public Class Form1

    Dim time4beats As Integer
    Dim connected As Boolean
    Dim beatcount As Integer
    Dim beat1 As Integer
    Dim beat2 As Integer
    Dim beat3 As Integer
    Dim beat4 As Integer

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        SerialPort1.PortName = TextBox1.Text
        SerialPort1.BaudRate = TextBox2.Text
        SerialPort1.Open()
        connected = True

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        time4beats = time4beats + 1

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        beatcount = 0
        beat1 = 0
        beat2 = 0
        beat3 = 0
        beat4 = 0

        time4beats = 0

        While (True)

            If (SerialPort1.ReadChar().Equals("b") & beatcount.Equals(0)) Then

                Timer1.Start()
                beatcount = beatcount + 1

            ElseIf (SerialPort1.ReadChar().Equals("b") & beatcount.Equals(1)) Then

                Timer1.Stop()
                beat1 = time4beats

                time4beats = 0
                Timer1.Start()
                beatcount = beatcount + 1

            ElseIf (SerialPort1.ReadChar().Equals("b") & beatcount.Equals(2)) Then

                beat2 = time4beats
                Timer1.Stop()
                time4beats = 0
                Timer1.Start()
                beatcount = beatcount + 1

            ElseIf (SerialPort1.ReadChar().Equals("b") & beatcount.Equals(3)) Then

                beat3 = time4beats
                Timer1.Stop()
                time4beats = 0
                Timer1.Start()
                beatcount = beatcount + 1

            ElseIf (SerialPort1.ReadChar().Equals("b") & beatcount.Equals(4)) Then

                beat4 = time4beats
                Timer1.Stop()
                time4beats = 0

                beatcount = 0

                TextBox3.Text = (60000 / ((beat1 + beat2 + beat3 + beat4) / 4)).ToString

            End If
        End While
    End Sub
End Class
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.