tcl and gnuplot interaction - tcl

Currently, I will tcl handle the data processing part. Let's say the data is stored in a tcl list variable.
If I want to use gnuplot to show the data. what is the best way to do this.
Based on my study, gnuplot needs data file provided. so I can not directly pass the list variable to gnuplot command.
I guess that creating the gnu command inside tcl with string operation. and then print all the command into a file,for example name it "command.dat".
And call the gnuplot this way in tcl:
exec gnuplot "command.dat"
any other method? or good reference.

Related

gtkwave tcl script for adding specific signals

I have a huge VCD file that I use in combination with gtkwave to observe certain signal behaviors. I have a list of signals stored into a .txt file which are the ones that I wish to probe. The thing is that by doing the insertion of the signals manually by hand is a painstakingly long process. So my question here is,
Is there a way, given the .txt file to compose a .tcl script that filters and adds the designated signals from the list to the waveform editor?
Well, after scouting on manuals and some gists I found here and there seems that there is a load of gtkwave instructions one can use that are listed (most of them) on the gtkwave manual (Appendix E) here. So in a nutshell all one has to do is to write a .tcl script in the following format:
# add_waves.tcl
set sig_list [list sig_name_a, register_name\[32:0\], ... ] # note the escaping of the [,] brackets
gtkwave::addSignalsFromList $sig_list
and then invoke the gktwave as:
gtkwave VCD_file.vcd --script=add_waves.tcl
Furthermore, access to the GUI menu options are viable as well via the following syntax in tcl:
gtkwave::/Edit/<Option> <value>

Plotting with Gnuplot in Tk

I have seen a topic regarding to how we plot graphs with Gnuplot in Tk Canvas. Here are the simple code sample from Donal Fellows#Donal Fellows. Can someone help me on these two commands in Bold(set term tk;gnuplot .c)? I can not understand what does it mean.Thanks.
package require Tk
eval [exec gnuplot << "
**set term tk**
plot x*x
"]
pack [canvas .c]
**gnuplot .c**
When you run the gnuplot program with the terminal set to tk, it writes to its standard output a Tcl script that will create a procedure. That procedure is called gnuplot, and it takes a single argument which is the name of the canvas to plot onto. So we call the gnuplot program with the appropriate arguments to get it to tell you how to make a command that will actually do the plotting. We eval that result, make the canvas, and delegate to that newly-created gnuplot command the actual plotting on the canvas.
It's a little odd, and theoretically unsafe (what if the gnuplot is hacked?!?!?! Oh noes!) but actually works quite well in practice.
To see why it works, try doing:
puts [exec gnuplot << "
set term tk
plot x*x
"]
Instead of evaluating the code, that will print it out. You'll see that it's a procedure definition, and how exactly it all works. (Alas, I've not got gnuplot installed on this computer at the moment, so I can't do the check quite instantly for you…)
I'm not an expert of gnuplot, but as far as understand the 2 command are very simple.
set term tk
Assign the value string tk to the variable term.
gnuplot .c
Launch the command gnuplot with argument .c.
In your code the .c is just the name of the tk canvas widget.
More intriguing is the first [exec gnuplot <<...] that execute an external command called gnuplot that initialize the tk script and define the tk command gnuplot used to draw the plot on a canvas.
It looks like the external gnuplot command generate the tck code to define all what is needed.

TCL/expect for generating log file in xml or json format

Is there any way to generate the log file in xml or json format using TCL.
Using the log_file the logs are stored in text format.
Please suggest
You can of course use one of the JSON or XML libraries available for Tcl to write log messages in any format you want. Probably to put the data into one of those ELK stacks?
The expect log_file command takes the -open or -leaveopen argument with a tcl file identifier. Combine this with Tcl reflected channels and you can divert the log to some other logging system that writes the JSON you want.
See the documentation for chan create and the API description at refchan. For writing JSON, you can use json::write from tcllib.
You could probably adapt the code of tcl::chan::textwidget to dump out JSON instead of writing to a text widget.

I need to run tcl script with options from another tcl script

I have a tcl script drakon_gen.tcl . I am running it, from another script run.tcl like this:
source "d:\\del 3\\drakon_editor1.22\\drakon_gen.tcl"
When I run run.tcl I have following output:
This utility generates code from a .drn file.
Usage: tclsh8.5 drakon_gen.tcl <options>
Options:
-in <filename> The input filename.
-out <dir> The output directory. Optional.
Now I need to add to run.tcl options that are in the output. I tried many ways but I receive errors. What is the right way to add options?
When you source a script into a tcl interpreter, you are evaluating the script file in the context of the current interpreter. If it was written to be a standalone program you may run into problems with conflicting variables and procedures in the global namespace. One way to avoid that is to investigate the use of slave interpreters (see the interp command) to provide a separate environment for the child script.
In your specific example it looks like you just need to provide some command line arguments. These are normally provided by the argv variable which holds a list of all the command line arguments. If you define this list before sourcing the script you can feed it the required command line. eg:
set original_argv $argv
set argv [list "--optionname" "value"]
source $additional_script_filename
set argv $original_argv

How to access user-supplied command-line options in Tcl?

in the command line i m giving input as
filename option
here option starting with hyphen.
how to pass command line arguments starts with -
The list of all the arguments passed to the process hosting your Tcl interpreter can be accessed using the global variable argv, which you can iterate over and inspect.
Special packages implementing ready-made solutions for "declarative" parsing of command-line arguments exist, with cmdline being one example. Also search the wiki for alternatives.
I think, you need the cmdline package from tcllib.
Documentation is here
http://tcllib.sourceforge.net/doc/cmdline.html
Joachim