I use gnuplot for plotting live data in my project. The small demo program is attached below.
I use Open Pipe trick, which I have learned from this forum. I am mot sure if the same results
can be obtained using shell command.
Carlos
Code: Select all
'
' Gnuplot plotting of live data demo
'
#Include Once "crt\stdio.bi"
#Include Once "crt.bi"
#Include Once "file.bi"
'
Dim Shared gnuplot_hand As FILE Ptr
Dim Shared pltstr As String
Dim Shared gplpipe As Integer
Dim Shared LFeed As String
LFeed = Chr(10)
Dim As Integer ii
'
Sub Gnuplot_Pipe ()
'
' open pipe to gnuplot
'
Dim As Integer res
Dim As String ts
'
' change path as needed, test with persistent mode
'
ts = """c:\program files\gnuplot\bin\gnuplot.exe"" -persist"
gplpipe = Freefile
res = Open Pipe (ts For Output As #gplpipe)
Print "Gnuplot Pipe:"; res
gnuplot_hand = Cast(FILE Ptr,Fileattr(gplpipe,fbFileAttrHandle))
'
End Sub
'
Sub Gnuplot_Setup ( )
'
' must be called AFTER Gnuplot_Pipe
'
pltstr = ""
pltstr = pltstr + "set terminal windows linewidth 2" + LFeed
pltstr = pltstr + "set datafile separator ','" + LFeed
pltstr = pltstr + "set datafile commentschars '#!%'" + LFeed
pltstr = pltstr + "set y2tics" + LFeed
pltstr = pltstr + "set autoscale y2" + LFeed
pltstr = pltstr + "set minussign" + LFeed
Print #gplpipe, pltstr
fflush(gnuplot_hand)
End Sub
'
Sub Gnuplot_Online (labx As String, labyl As String, labyp As String, _
colx As String, colyl As String, colyp As String)
'
' must be called AFTER Gnuplot_Pipe and Gnuplot_Setup
'
pltstr = ""
pltstr = pltstr + "set xlabel " + labx + LFeed
pltstr = pltstr + "set ylabel " + labyl + LFeed
pltstr = pltstr + "set y2label " + labyp + LFeed
pltstr = pltstr + "plot 'online.csv' using "+colx+":"+colyl+" w lp pt 7 lc 7 axes x1y1,\" + LFeed
pltstr = pltstr + "'online.csv' using "+colx+":"+colyp+" w lp pt 11 lc 11 axes x1y2"
Print #gplpipe, pltstr
fflush(gnuplot_hand)
'
End Sub
'
Sub Create_Online_File
'
' create online file
'
Dim As Integer fil
'
fil = Freefile
Open "online.csv" For Output As #fil
Print #fil, "# online data"
Close #fil
'
End Sub
'
Sub Add_Data_to_File ( idx As Integer)
'
' appends data to obline.csv
'
Dim As Integer filo
Randomize
filo = Freefile
Open "online.csv" For Append As #filo
Print #filo, Str(idx) + "," + Str(Sin(0.3*idx) + 0.4*Rnd) + "," + Str(0.01*idx+0.1*Cos(0.2*idx) + 0.03*Rnd)
Print Str(idx) + "," + Str(Sin(0.3*idx) + 0.4*Rnd) + "," + Str(0.01*idx+0.1*Cos(0.2*idx) + 0.03*Rnd)
Close #filo
'
End Sub
'
' Generate and plot live data
'
Gnuplot_Pipe
Gnuplot_Setup
Create_Online_File
For ii = 0 To 100
Add_Data_to_File (ii)
Sleep 300
Gnuplot_Online ("'index'", "'Y_1'", "'Y_2'", "1", "2", "3")
Next ii
Sleep