我和Marven here有同样的问题 除了我通过串口接收数据。 Marven接受的解决方案涉及DDE,我不使用它,也不知道任何事情。我不知道我的问题是在我的串口设置或我的线程或什么。无论如何,这是我的代码: (我也在使用FastLine图表,并且Arduino在串口上每0.1秒发送一次数据)
我的串口设置:
With serialPort
.PortName = port
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
我的线程是如何创建和启动的:
trd = New Thread(AddressOf graph)
trd.IsBackground = True
trd.Start()
我的绘图功能:
Private Sub graph()
Dim i As Long = 0
Dim currentArray(0) As Double
Dim length As Long = 0
Dim mole As Double = 0
serialPort.DiscardInBuffer()
While (serialPort.IsOpen())
If length >= 0 Then
ReDim Preserve currentArray(length + 1)
End If
currentArray(i) = serialPort.ReadLine()
length = UBound(currentArray)
If (currentArray(i) > Chart1.ChartAreas(0).AxisY.Maximum) Then
Chart1.ChartAreas(0).AxisY.Maximum = currentArray(i) + 2
End If
Chart1.Series(0).Points.AddXY(i, currentArray(i))
If Chart1.Series(0).Points.Count >= 3000 Then
Chart1.Series(0).Points.RemoveAt(0)
Chart1.ChartAreas(0).AxisX.Maximum = UBound(currentArray)
Chart1.ChartAreas(0).AxisX.Minimum = UBound(currentArray) - 3000
End If
If chkIntergrate.Checked = True Then
mole += 0.1 * currentArray(i) * 0.00001036
pgrBar.Value = mole * 1000
lblValue.Text = FormatNumber(mole, 3)
End If
i += 1
End While
End Sub