|
|
Reading Data from a Serial Interface
This application note shows
how to read data from a serial interface and display the samples on a DynaPlot chart in real time.
The basic ideas behind the code below are
|
 |
We let the serial interface call the receive event handler if there are at least
Blocksize new samples in the input buffer. (Calling the handler for each and every sample would place a too heavy load on the system.)
|
|
 |
In the
event handler we convert the data to a signed format shift out the oldest samples to make room for the new samples and append the new block of samples to the curve by calling method
PushData,
|
The application is equipped with a built-in transmitter which generates a data stream which can be
looped back to the serial input by simply connecting Tx and Rx (pins 2 and 3 of a D-Sub connector). An external data source is therefore not required to try out this demo.
|
'
-------------------------------------------------------------------------- ' DataFromComm.vbs ' This demo shows how to read data from a serial link and display them in a
DynaPlot chart. ' A simulation creates an output data stream on the Tx pin, which can be used for testing. ' To feed the simulated output back into the serial input, connect Rx
and Tx ' (pins 2 and 3 of the D-Sub connector) ' ------------------------------------------------------------------------- Option Explicit Const MaxNumberOfPoints =
100 ' the number of points in the chart i.e. number of
' most recent samples which shall be displayed
Private Sub Form_Load() Dim i As Long Dim data(MaxNumberOfPoints - 1) As Double
' ------ simulation ----- MSComm1.OutBufferSize = 10 * MaxNumberOfPoints ' ------ simulation -----
Const Blocksize =
5 ' number of samples
after which the chart shall be updated MSComm1.RThreshold = Blocksize ' fire event if the input buffer contains at least Blocksize
new samples MSComm1.InputLen = 10 * MaxNumberOfPoints MSComm1.InputMode = comInputModeBinary
MSComm1.CommPort = 1 ' open the serial port
MSComm1.Settings = "2400,N,8" MSComm1.PortOpen = True
DynaPlot1.Axes.XAxis.From = 0
DynaPlot1.Axes.XAxis.To = MaxNumberOfPoints - 1
DynaPlot1.Axes.YAxis.From = -130 DynaPlot1.Axes.YAxis.To = 130
For i = 0 To MaxNumberOfPoints - 1 ' clear data array data(i) = 0
Next
' add empty array to chart DynaPlot1.DataCurves.AddParametric "Curve1", 0, 1, data, MaxNumberOfPoints
' ------ simulation ----- Timer1.Interval = 20 Timer1.Enabled = True
' ------ simulation ----- End Sub
Private Sub MSComm1_OnComm() Dim i As Long, NumNewPoints As Long
Dim Arr() As Byte Dim data() As Double
Select Case MSComm1.CommEvent
Case comEvReceive NumNewPoints =
MSComm1.InBufferCount ' the actual number of samples in the input buffer
' can be greater than Blocksize! Arr =
MSComm1.Input
' move the new data to a temporary array ReDim data(NumNewPoints - 1)
For i = 0 To NumNewPoints - 1
data(i) = Arr(i) -
128
' convert to signed data and scale if necessary Next
' Shift curve to the left and append new data
DynaPlot1.DataCurves.Item(0).Curve.PushData Empty, data, NumNewPoints End Select End Sub
' ------ simulation ----- Private Sub Timer1_Timer()
Static phase As Double Dim i As Integer Dim s As String Dim y As Double
Const numsamples =
3
' number of new samples to append to the data stream
' simulate a sine wave signal For i = 0 To numsamples - 1
y = 127 * Sin((i + phase) * 0.11) + 128 s = s & Chr(y) Next
phase = phase + numsamples
MSComm1.Output = s ' send End Sub ' ------ simulation -----
|
The above code can also be downloaded:
Download DataFromComm.zip
|