LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 11-04-2011, 06:39 AM   #1
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 65

Rep: Reputation: 0
Plotting realtime data with GNUPLOT


Hi!

I am trying to write a C program to plot data from a bluetooth device.

GNUPLOT seemed to be the way to go.

So far i got:
Code:
set xrange [0:200]
set yrange [0:255]
set xtics 10
set ytics 10
set ylabel "Value"
set style data lines
set grid

plot \"-\" notitle
1
2
3
4
...and so on
e
The problem is that it doesn't plot the data on screen until it gets the terminating character 'e'.

Is there a way to get around this, and watch the data in real time?

//John
 
Old 11-04-2011, 07:34 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,037

Rep: Reputation: 756Reputation: 756Reputation: 756Reputation: 756Reputation: 756Reputation: 756Reputation: 756
Quote:
Originally Posted by john_erlandsson View Post
Is there a way to get around this, and watch the data in real time?
Well, not really -- as with many things that monitor; e.g., top, ntop and others, you need to periodically refresh.

I had to do something similar once upon a once and I wound up gathering data for some period of time then spitting it out to GNUPLOT (with the execlp function I think, have a look at the manual page), killing that after a time and looping back to regenerate. Worked OK and most systems are fast enough nowadays that you don't really notice the "blink" when the old chart goes away and the new one pops up.

There might be some sort of Java thing you can do or a JavaScript thing to an HTML page but the GNUPLOT way isn't a bad idea either -- what works works, eh?

Hope this helps some.
 
Old 11-04-2011, 07:51 AM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,067

Rep: Reputation: 491Reputation: 491Reputation: 491Reputation: 491Reputation: 491
It is fundamentally impossible to do that, because Gnuplot is a batch operation -- that is you first have to feed all the data before it can create a plot.

The solution would be to use not a single process loop, but a dual loop. Single means that you produce the data, and run Gnuplot immediately in the hope it will display the plot while data is produced. Dual loop means: one process collects the data and writes it to a file periodically. Then a second process picks up the data, and periodically creates the plot. This is not different as what tronayne wrote, it is just a more generic description.

Since you already seems to have your data acquisition process running, you have a number of choices for the Gnuplot loop: a bash script periodically calling display, or integrate it is a web page, where Gnuplot would produce the plot and Javascript refreshes it periodically. As a matter of fact this start to look like a triple loop.

If the plot is real simple you could consider using the Tk toolkit with Tcl or Python. Tk has some graph widgets.

jlinkels
 
Old 11-04-2011, 04:42 PM   #4
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 65

Original Poster
Rep: Reputation: 0
Hi guys!

Thanks for taking an interest.

Just to make shure that I'm not confusing anyone by using the words realtime and plot, here is a snippet of my C application.

This function opens up a pipe to gnuplot and initiates the graph: (Basically stolen from the GNU Guile tutorial)
Code:
static FILE* start_gnuplot()
{
  FILE* output;
  int pipes[2];
  pid_t pid;

  pipe( pipes );
  pid = fork ();

  if ( !pid )
    {
      dup2( pipes[0], STDIN_FILENO );
      execlp( "gnuplot", NULL );
      return -1;
    }

  output = fdopen( pipes[1], "w" );

  fprintf (output, "set xrange [0:200]\n" );
  fprintf (output, "set yrange [0:255]\n" );
  fprintf (output, "set xtics 10\n");
  fprintf (output, "set ytics 10\n");
  fprintf (output, "set ylabel \"Value\"\n");
  fprintf (output, "set style data lines\n" );
  fprintf (output, "set grid\n" );
  fflush (output);

  return output;
}
The loop i use for testing looks like this: (It will be replaced by a loop that receives 8bits of data from a bluetooth socket and shipps that to gnuplot)
Code:
fprintf( global_output, "plot \"-\" notitle\n" );

int x;
  for( x = 0; x < 100; x++ )
  {
	  fprintf( global_output, "%d\n", x );
	  usleep( 30000 );
  }
  for( x = 100; x > 0; x-- )
  {
	  fprintf( global_output, "%d\n", x );
	  usleep( 30000 );
  }

fprintf( global_output, "e\n" ); //the gnuplot window doesn't open until it
I want to be able to log a continuous flow of data. It doesnt matter if i have to buffer a couple of sets before displaying them, as long as it appends to the curve instead of replacing it.

Does that make sense?


If this is not possible in gnuplot, I will probably do it in Qt...

Last edited by john_erlandsson; 11-04-2011 at 04:45 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] What plotting software should I use for Plotting ASCII data in Ubuntu? msbstar Linux - Newbie 7 04-12-2011 10:30 PM
LXer: Real Time Data Plotting with GnuPlot LXer Syndicated Linux News 0 02-03-2010 03:30 PM
gnuplot (plotting simultaneously) hanamilani Linux - Software 2 02-02-2009 12:18 AM
gnuplot, pull X data from file, specify Y data at cmd line? hedpe Programming 5 03-15-2007 11:32 PM
Data/Map Plotting codyman General 0 11-24-2004 09:24 AM


All times are GMT -5. The time now is 03:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration