From: Rolf Schroedter on
sandy wrote, On 04.07.2008 11:47:
In my application I want to send device commands through serial port.
>
> Suppose I am sending device commands through tcl loop, how can i make
> tcl loop executes only as fast as my serial device can handle it? What
> I have observed is that tcl loop finished much faster then the device
> and data gets lost.

Please read through the thread "Events on Serial port".
You'll find my posting on blocking/non-blocking writes.
What are your serial port [fconfigure] settings and how do you write data ?

From: Alexandre Ferrieux on
On Jul 4, 2:16 pm, sandy <motif_u...(a)yahoo.com> wrote:
> On Jul 4, 3:00 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> wrote:
>
>
>
>
>
> > On Jul 4, 11:47 am, sandy <motif_u...(a)yahoo.com> wrote:
>
> > > On Jul 3, 1:24 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> > > wrote:
>
> > > > On Jul 3, 5:56 am, sandy <motif_u...(a)yahoo.com> wrote:
>
> > > > > Hello,
>
> > > > > I'm trying to embed tcl interepreter in my c code and wanted to
> > > > > redirect stdout and stderr to
> > > > > different files. stdout works fine but stderr (errlog.txt) is always
> > > > > empty( even though tcl script
> > > > > contain errors). I am trying this first time, please suggest.
>
> > > > > ---- Here is the code ---
>
> > > > > main (int argc, char *argv[])
> > > > > {
> > > > >         Tcl_Interp *interp;
> > > > >         int code, temp;
>
> > > > >         interp = Tcl_CreateInterp();
>
> > > > >         Tcl_Channel stdconsoleChannel;
> > > > >         stdconsoleChannel = Tcl_OpenFileChannel(interp, "outlog.txt",
> > > > > "w+", 777);
> > > > >         Tcl_SetStdChannel(stdconsoleChannel, TCL_STDOUT);
>
> > > > >         Tcl_Channel errconsoleChannel;
> > > > >         errconsoleChannel = Tcl_OpenFileChannel(interp, "errlog.txt",
> > > > > "w+", 777);
> > > > >         Tcl_SetStdChannel(errconsoleChannel, TCL_STDERR);
>
> > > > >         code = Tcl_EvalFile(interp, argv[1]);
>
> > > > >         Tcl_DeleteInterp(interp);
>
> > > > >         exit(0);
>
> > > > > }
>
> > > > The fact that the script has errors doesn't imply immediate output to
> > > > stderr. Instead, Tcl_EvalFile will return TCL_ERROR, the interp's
> > > > result containing the error message, and it is up to the caller to
> > > > provide an outer "last chance handler".
>
> > > > So, my bet is that Tcl_SetStdChannel(TCL_STDERR) works fine, and you
> > > > can verify this prediction by adding into your script:
>
> > > >     puts stderr Hey!
> > > >     flush stderr
>
> > > > before any error.
> > > > Once this is verified, either write the outer handler at the C level
> > > > as suggested above, or in Tcl by wrapping your script in a [catch].
>
> > > > By the way, tclsh and wish both provide excellent outer handlers.
> > > > Are you sure you really want to embed, instead of extending Tcl ?
> > > > Please describe your situation.
>
> > > > -Alex- Hide quoted text -
>
> > > > - Show quoted text -
>
> > > Thanks Alex,
>
> > > Just, one more question.
>
> > > In my application I want to send device commands through serial port.
>
> > > Suppose I am sending device commands through tcl loop, how can i make
> > > tcl loop executes only as fast as my serial device can handle it? What
> > > I have observed is that tcl loop finished much faster then the device
> > > and data gets lost.
>
> > The problem is not with the event loop, it has to do with low-level
> > flow control being enabled or not. When you [puts] to a channel in
> > Tcl, sonner or later a write() syscall will occur. For a socket, pipe,
> > or file, you have a stream semantics, in that no data is lost: the
> > write is done either in blocking or nonblocking mode, but in the
> > latter case data not immediately writte are retried later.
>
> > For a serial port, if flow control is enabled, it should work the
> > same. But maybe this requires the physical device on the other side to
> > cooperate...
>
> > Now you didn't answer *my* question ;-)
>
> > -Alex- Hide quoted text -
>
> > - Show quoted text -
>
> thanks again Alex, In my situations i am trying to provide tcl support
> to a MFC application, hence thinking of embedding and not extending. I
> hope i am not wrong ;-)

It all depends on how far you want to go along that route. For
example, if your next question is about how to blend MFC's and Tcl's
event loop, or how to untangle threading issues across the Tcl-MFC
boundary, then I'd suggest to reserve this for a time of better
familiarity with the Tcl C API. It's not impossible, it's just not
something to do as the beginning of your learning curve.

Can you elaborate on the kind of interactions between the MFC app and
the Tcl interpreter ?

-Alex
From: sandy on
On Jul 4, 8:26 am, Rolf Schroedter <m...(a)privacy.net> wrote:
> sandy wrote, On 04.07.2008 11:47:
> In my application I want to send device commands through serial port.
>
>
>
> > Suppose I am sending device commands through tcl loop, how can i make
> > tcl loop executes only as fast as my serial device can handle it? What
> > I have observed is that tcl loop finished much faster then the device
> > and data gets lost.
>
> Please read through the thread "Events on Serial port".
> You'll find my posting on blocking/non-blocking writes.
> What are your serial port [fconfigure] settings and how do you write data ?

rolf, your post is very useful, it cleared my some wrong
assumptions ;-)

Thanks,
Sandy
From: sandy on
On Jul 4, 7:24 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
wrote:
> On Jul 4, 2:16 pm, sandy <motif_u...(a)yahoo.com> wrote:
>
>
>
> > On Jul 4, 3:00 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> > wrote:
>
> > > On Jul 4, 11:47 am, sandy <motif_u...(a)yahoo.com> wrote:
>
> > > > On Jul 3, 1:24 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> > > > wrote:
>
> > > > > On Jul 3, 5:56 am, sandy <motif_u...(a)yahoo.com> wrote:
>
> > > > > > Hello,
>
> > > > > > I'm trying to embed tcl interepreter in my c code and wanted to
> > > > > > redirect stdout and stderr to
> > > > > > different files. stdout works fine but stderr (errlog.txt) is always
> > > > > > empty( even though tcl script
> > > > > > contain errors). I am trying this first time, please suggest.
>
> > > > > > ---- Here is the code ---
>
> > > > > > main (int argc, char *argv[])
> > > > > > {
> > > > > > Tcl_Interp *interp;
> > > > > > int code, temp;
>
> > > > > > interp = Tcl_CreateInterp();
>
> > > > > > Tcl_Channel stdconsoleChannel;
> > > > > > stdconsoleChannel = Tcl_OpenFileChannel(interp, "outlog.txt",
> > > > > > "w+", 777);
> > > > > > Tcl_SetStdChannel(stdconsoleChannel, TCL_STDOUT);
>
> > > > > > Tcl_Channel errconsoleChannel;
> > > > > > errconsoleChannel = Tcl_OpenFileChannel(interp, "errlog.txt",
> > > > > > "w+", 777);
> > > > > > Tcl_SetStdChannel(errconsoleChannel, TCL_STDERR);
>
> > > > > > code = Tcl_EvalFile(interp, argv[1]);
>
> > > > > > Tcl_DeleteInterp(interp);
>
> > > > > > exit(0);
>
> > > > > > }
>
> > > > > The fact that the script has errors doesn't imply immediate output to
> > > > > stderr. Instead, Tcl_EvalFile will return TCL_ERROR, the interp's
> > > > > result containing the error message, and it is up to the caller to
> > > > > provide an outer "last chance handler".
>
> > > > > So, my bet is that Tcl_SetStdChannel(TCL_STDERR) works fine, and you
> > > > > can verify this prediction by adding into your script:
>
> > > > > puts stderr Hey!
> > > > > flush stderr
>
> > > > > before any error.
> > > > > Once this is verified, either write the outer handler at the C level
> > > > > as suggested above, or in Tcl by wrapping your script in a [catch].
>
> > > > > By the way, tclsh and wish both provide excellent outer handlers.
> > > > > Are you sure you really want to embed, instead of extending Tcl ?
> > > > > Please describe your situation.
>
> > > > > -Alex- Hide quoted text -
>
> > > > > - Show quoted text -
>
> > > > Thanks Alex,
>
> > > > Just, one more question.
>
> > > > In my application I want to send device commands through serial port.
>
> > > > Suppose I am sending device commands through tcl loop, how can i make
> > > > tcl loop executes only as fast as my serial device can handle it? What
> > > > I have observed is that tcl loop finished much faster then the device
> > > > and data gets lost.
>
> > > The problem is not with the event loop, it has to do with low-level
> > > flow control being enabled or not. When you [puts] to a channel in
> > > Tcl, sonner or later a write() syscall will occur. For a socket, pipe,
> > > or file, you have a stream semantics, in that no data is lost: the
> > > write is done either in blocking or nonblocking mode, but in the
> > > latter case data not immediately writte are retried later.
>
> > > For a serial port, if flow control is enabled, it should work the
> > > same. But maybe this requires the physical device on the other side to
> > > cooperate...
>
> > > Now you didn't answer *my* question ;-)
>
> > > -Alex- Hide quoted text -
>
> > > - Show quoted text -
>
> > thanks again Alex, In my situations i am trying to provide tcl support
> > to a MFC application, hence thinking of embedding and not extending. I
> > hope i am not wrong ;-)
>
> It all depends on how far you want to go along that route. For
> example, if your next question is about how to blend MFC's and Tcl's
> event loop, or how to untangle threading issues across the Tcl-MFC
> boundary, then I'd suggest to reserve this for a time of better
> familiarity with the Tcl C API. It's not impossible, it's just not
> something to do as the beginning of your learning curve.
>
> Can you elaborate on the kind of interactions between the MFC app and
> the Tcl interpreter ?
>
> -Alex

I understand your point alex, my tcl interpreter is just to provide
familiar interface to the user, i don't think I'm going to blend mfc-
tcl for those complex/advanced situations presently. Thanks for your
valuable inputs.

Regards,
Sandy