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

  • SEARCH
  • Home
  • 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 9243631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:50:56+00:00 2026-06-18T08:50:56+00:00

I am using a main script in my tcl code in which i am

  • 0

I am using a main script in my tcl code in which i am opening a udp port to read the data coming on it. I have made this using fileevent for which i am entering into the event loop using “vwait” command. But i don’t want the main script to be blocked due to this. In short i want main script continuing in flow and the wait for udp data event should be in background. How to achieve this. Please help me. I have wasted a lot of time for this, but all in vein. Any help would be really appreciable.

Here is my main script :

#!/usr/bin/env tclsh8.5

proc create_udp_payload { command data } {
    set payload     ""
    append payload $command
    append payload  [ format %08x [ expr $data ] ]
    return $payload
}

proc send_udp_packet { payload } {
    puts -nonewline $soc::s [binary format H* $payload]
    puts "\nTransmitting the UDP payload $payload"
    return 0
}

set DEST_IP_ADDR        10.10.20.241
set DEST_UDP_PORT       5558
set soc::s              [udp_open]
puts "Listening on udp port : [fconfigure $soc::s -myport]"
udp_conf $soc::s $DEST_IP_ADDR $DEST_UDP_PORT
fconfigure $soc::s -buffering none -translation binary
fileevent $soc::s readable [list ::udp_listen]

for { set j 0 } { $j < 10 } { incr j} {
    puts "Sending read request to read register $j"
    set command "00"
    append command  [format %02x $j]
    send_udp_packet [create_udp_payload $command  0x0000]
    after 1000
    flush $soc::s
}

wait forever #to enter into the event loop

for { set j 2 } { $j < 12 } { incr j} {
    puts "Sending  write request to write register $j"
    set command "80"
    append command [format %02x $j]
    send_udp_packet [create_udp_payload $command [expr {0x0000 | $j }] ]
    after 2000
    flush $soc::s
}

Actually i want to read 10 registers one by one from a hardware, for which i am using a for loop, but i am not getting how to enter into event loop for this. Because it blocks the main script. Also after that i want to write 10 registers of the same hardware one by one. Can anybody please modify this code to work accordingly. Thanks in advance for that……..

  • 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-18T08:50:58+00:00Added an answer on June 18, 2026 at 8:50 am

    I think your problem could be easily solved using the after idle “trick”. The idea is to set up your “channel readable” event handlers, then schedule the execution of your data sending code for the “event loop is idle” condition, and then enter the event loop:

    Like this:

    # 1. Create and configure the UDP endpoint:
    set udp [udp_open ...]
    ...
    # 2. Install "chan readable" callback handlers for that endpoint:
    fileevent $udp readable ...
    ...
    # 3. Schedule the one-off callback to send your data:
    after idle [list post_data $udp]
    # 4. Enter the event loop:
    vwait ::done
    

    As soon as vwait is called, the event loop machinery sees if there’s any pending events (there should be none) and as soon as the queue of pending events is drained or is empty, all the handlers installed using after idle are called and your data is sent out.

    Note that this approach works for other similar cases as well, for instance, nothing prevents you from installing such after idle handlers from other handlers called by the event loop. This allows you to never ever leave the event loop (as GUI programs and network servers usually do).

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

Sidebar

Related Questions

this is the idea. I'll have 'main' python script that will start (using subprocess)
I have a python script which I call using python main.py on my terminal.
Wanting to make sure I'm using classes properly. The main script accepts this from
I'm using this CSS: #main{ border-radius: 50px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; -webkit-border-radius: 50px; -webkit-border-bottom-right-radius:
My main solution is using code from a utility class library, that I wrote
I have a main script that is running other scripts and loading the variables
Please consider the following code and the explanation from this Mozilla tutorial Using web
I am reorganizing all my js introducing namespaces with a main script (using jQuery)
I have an sh script which runs on our server. It is started with
I have a shell script which I'd like to trigger from a J2EE web

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.