Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:35:11+00:00 2026-06-10T12:35:11+00:00

I am new to tcl. I want to create a thread in tcl which

  • 0

I am new to tcl. I want to create a thread in tcl which should keep calling itself in background.

#!/usr/bin/env tclsh

set serPort [open "/dev/ttyS0" RDWR]

fconfigure $serPort  -mode 115200,n,8,1 -blocking 0 

while { 1 } {
set data [gets  $chan]
puts $data

}

I want to avoid using the above while loop and create a repeatable thread for the functionality inside the while loop. Basically i am connecting the COM1 of my PC to a device and getting the serial data from the device. But if there is no data on the port it still doesn’t come out of loop even if i use “eof” command. That is the reason i want to create the thread.

I am planning to use Tcl_CreateThread for that but I don’t understood how to use it

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:35:13+00:00Added an answer on June 10, 2026 at 12:35 pm

    Don’t do that. Instead, use the usual Tcl’s idiom for working with non-blocking channels: set up a handler for the “channel readable” event, and enter the event loop; when the device sends data to the port you opened, the OS passes the data to your application and the callback gets called.

    A minimal program to demonstrate the concept looks like this:

    proc my_read_handler ch {
        set data [read $ch]
        if {[eof $ch]} {
            close $ch
            set ::forever done ;# this makes the call to `vwait` below to quit
            return
        }
        # Otherwise process the data here ...
    }
    
    set serPort [open "/dev/ttyS0" RDWR]
    
    fconfigure $serPort -mode 115200,n,8,1 -blocking no
    fileevent $serPort readable [list my_read_handler $serPort]
    
    vwait ::forever ;# the program enters the event loop here
    

    Read more on this in the examples.

    Several observations:

    • The EOF only happens when the remote side closes. If you call close on your channel, the “readable” even won’t be called in this case.
    • If you’re writing a Tk application, it will already have an event loop, so no calls to vwait are necessary (moreover, they’re highly advised against, as this will re-enter the event loop): you just open your device, say, in a code which executes when the users clicks a button, set up the readable callback on the acquired channel and then just do the rest of the processing in that callback (as shown above).

    Read this (and the links there) for more info on the event-oriented programming. Also search the wiki — it contains lots of examples and background knowledge.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a new operator in Tcl, by operator I mean a
I have a flat Tcl list. Now I want to append a new element
I am new to this 'GUI' but i want to create a GUI in
New to DDD here and have a architecture question which should be a typical
Greetings, I'm new to TCL scripting, and I have a very very basic xml
I use TCL 8.4 and for that version I need to add a new
new on ruby and using windows xp and rails 3, i want to send
New to Regex. I want to validate to this format: Any character allowed, except
I am relatively new to TCL / expect and mostly modifying existing code. expect_before
New to MonoTouch and MonoTouch.Dialog, but I'm sure it's possible to create a tab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.