Reading data from serial port and plotting graph

  • Thread starter Thread starter CodeManInd
  • Start date Start date
C

CodeManInd

Guest
Hi all,


I am developing a VB.Net program to read data from serial port and plot a graph real time. Serial port is reading data continuously without end. I have implemented reading and plotting with the help answers from previous questions. But happens is that my screen freezes while running the program. Even though reading and plotting occurs continuosly, I cannot click on any other controls like button,combo etc


Form Load

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim addDataThreadStart As New ThreadStart(AddressOf AddDataThreadLoop)
addDataRunner = New Thread(addDataThreadStart)
addDataDel = New AddDataDelegate(AddressOf pltGraph)

End Sub


Serialport reading code


Private Sub srpPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles srpPort.DataReceived
Dim dtLen As Long
Dim mnclpos As Integer = 0, mnrpos As Integer = 0, mnipos As Integer = 0
Dim pos As Integer = 0, rpos As Integer = 0
Dim vrpos As Integer = 0
Dim variable(3) As Integer
Dim srpos As Integer = 0
Dim Ch1vl As Integer, Ch2vl As Integer, Ch3vl As Integer, Ch4vl As Integer
Dim mnpos As Integer
Dim nBytes As Integer
If srpPort.IsOpen Then
Dim dtRecvd(dtLen) As Byte

Me.BeginInvoke(DirectCast((Sub()

dtLen = srpPort.BytesToRead
Do
If srpPort.IsOpen Then
Dim Incoming As Byte = srpPort.ReadByte()
If IsNothing(Incoming) Then Exit Do
If Rd1Free Then
dtRd1.SetValue(Incoming, posMn)
Rd1Data = True
ElseIf Rd2Free Then
dtRd2.SetValue(Incoming, posMn)
Rd2Data = True
End If

posMn += 1
If Rd1Free Then
If posMn = RdBfrSz Then
dtRd1.CopyTo(dtMain, CopyposMn)
Rd1Free = False
Rd2Free = True
posMn = 0
CopyposMn += RdBfrSz
MnArrLen += RdBfrSz
End If
ElseIf Rd2Free Then
If posMn = RdBfrSz Then
dtRd2.CopyTo(dtMain, CopyposMn)
Rd2Free = False
Rd1Free = True
posMn = 0
CopyposMn += RdBfrSz
MnArrLen += RdBfrSz
End If
End If

ChMain.Invoke(addDataDel)
End If
Loop While posMn < dtLen

End Sub), Action))

End If


End Sub

Public Sub pltGraph()
Dim Ch1vl As Integer, Ch2vl As Integer, Ch3vl As Integer, Ch4vl As Integer
Dim PntXVal As Long
Dim value As Double
blnThrdStrtd = True
If MnArrLen Mod 20 <> 0 Then
blnThrdStrtd = False
Exit Sub
Else

End If
While mnArrPos < MnArrLen - 1

value = BitConverter.ToInt16(dtMain, mnArrPos)

If value = 1246 Then
Dim ipos As Integer
For ipos = 0 To 3
ChMain.Series(ipos).Points.AddXY((CurXVal), variable(ipos))

Next
If CurXVal > myArea1.AxisX.Maximum Then
If mySeries1.Points.Count > 0 Then
PntXVal = mySeries1.Points(0).XValue + PltIntrvl
While mySeries1.Points(0).XValue < PntXVal
mySeries1.Points.RemoveAt(0)
End While
myArea1.AxisX.Minimum = mySeries1.Points(0).XValue - PltIntrvl
myArea1.AxisX.Maximum = myArea1.AxisX.Maximum + PltIntrvl
myArea1.AxisX.Crossing = (myArea1.AxisX.Minimum + myArea1.AxisX.Maximum) / 2
Ar1XMin = myArea1.AxisX.Minimum
Ar1XMax = myArea1.AxisX.Maximum
End If
End If
If CurXVal <= myArea1.AxisX.Maximum Then
CurXVal += PltIntrvl
End If
ChMain.ResetAutoValues()
ChMain.ResumeLayout()
vrpos = 0
Else
variable(vrpos) = value / 1000
vrpos += 1

TotPnts += 1

End If
mnArrPos += 2


End While

blnThrdStrtd = False


End Sub

Hope somebody can helpme

Continue reading...
 
Back
Top