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

2

I have here a arduino that is to detect beats on a line signal audio level.

Whenever detected a hit the arduino sends the b character to the serial port.

So far I've been able to do it.

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

That is, I have a SerialPort1 and whenever it receives a b , a timer (* with an interval of 1 millisecond set in the timer properties) must be initialized % received%.

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

Then do the same for the second b received and the third .

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

Once you have 4 variables, you must divide the 60000 milliseconds by the average between the 4 variables and show the result (bpm) beats per minute ) in b .

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

Since I'm a little experienced programming, I would like to know how to get everything working on TextBox since I'm not getting it.

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

I know that afterwards it will be necessary to implement code to make break to 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
    
asked by anonymous 30.11.2018 / 21:37

0 answers