I have a sensor (a Quartzonix pressure transducer, actually) that spits out data via the serial port, around 3 times a second. I'd like to set up some code to give me an average reading based on x-amount of samples.
The output looks something like this:
01+ 1.502347091823e01
01+ 1.501987234092e01
01+ 1.50234524524e01
01+ 1.502123412341e01
01+ 1.502236234523e01
01+ 1.50198345e01
01+ 1.502346234523e01
.. and will keep going on forever until the com port is closed or the transducer gets another command.
This is what code I have so far, and the code works to show me what the transducer is actually outputting:
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Dim a As String
a = "$01MC" & Chr(13)
MyComPort.WriteLine(a)
Do
Dim Incoming As String = MyComPort.ReadLine()
Dim incomingtext As String = Incoming.Remove(0, 3)
If Incoming Is Nothing Then
Exit Do
Else
txtRawData.Text = Incoming
boxPSIA.Text = Format(Val(incomingtext), "##0.000")
End If
Application.DoEvents()
Loop
End Sub
The "$01MC" is the command the transducer needs to start spitting out the data. I've got some wierd timeout thing happening when i click the start button, but that's another show (maybe a .readtimeout adjustment is needed, not sure).
I have a text box txtReadingsToAvg for input of how many readings to average.. I'm just not wrapping my head around how to actually get it to caluclate the average (on, say, a button click and then spitting it out into a msgbox, or even into another text box).