Menu

General Python Branching

2022-03-07
2022-03-12
  • Mike Roberts

    Mike Roberts - 2022-03-07

    I'm new to this forum and new to Python and PAGE. I have used PAGE to create a Proof of Concept project to implement communication with a PIC microprocessor over a USB to Serial Port. It is a single form project with several buttons, some text entry widgets and some labels that change color with state. My form prefaces data being sent to the PIC with special characters and has a simple wait function that parses a single character back from the PIC as an acknowledge. Because there are a lot of transfers back and forth there are multiple ACKs within the various functions. This all works as expected from a proof of concept standpoint but now I want to build in some "recovery" capability if and when an ACK might not be received.

    My general question is this: I would like to know if it's possible to build into my function that waits for an ACK a general branch capability that will jump out of the ACK function, reset any button that is waiting for an ACK that hasn't arrived and return to the main loop waiting for a user input?

    It appears that just completing the function call specified in the button definition in the project file will reset the button. In my case, with several "waitACK" calls within my button function, I don't want to execute any code after any ACK that might be missed. I prefer to jump to the end of the button function and require the user to select the button again.

    I'm hoping not to rewrite my concept code into multiple individual functions for every communication with my PIC so that I can use only break, continue or return to accomplish this capability. I can provide my project and the "_support.py"

    file if needed.

    Thanks for the help

     
  • Greg Walters

    Greg Walters - 2022-03-08

    Hi Mike,

    Tkinter provides a function called root.after(). You can use it to create a timer that fires every n milliseconds. Consider it something like a thread but easier.

    I threw together a quick demo program that creates three root.after timers. One running all the time to update the current system time, one that polls a simulated commbuffer looking for 0x06 (Ascii Ack) and one that counts the number of seconds that the program has been waiting for an ack to "come in".

    There are three buttons on the demo form. One exits the program. One that starts the poll function and one that sends an ack to the simulated commbuffer. Once the ack is "received", it resets the poll start button so you can start the wait process over again and send in another ack.

    The demo should give you enough information to move forward. I'm not sure, however, if it will do exactly what you are looking for. From your description, it looks like you will be trying to wait for multiple acks. This really won't handle that, since there is no provision in my code for that (due to lack of understanding fully your requirements). However, you might be able to take what I've provided and modify it to your needs.

    I also am concerned that you might not use event driven programming methods for your program and are using loop driven. Loop driven programming is what "normal" python uses. Event driven programming is what we use for GUI programming. This requires a paradigm shift from loops to events.

    I sincerely hope this helps,

    Greg

     
  • Anonymous

    Anonymous - 2022-03-12

    The button callback can disable the button & call the transmission routine , set a variable to indicate the time the transmission began (using the time or datetime module functions) & simply exit the button.
    Then in the main graphics loop, it can monitor for reply reception, as well as the current time & can also do other things (it is not held up). If all is well, the button is turned back on. If the time check gets to be too long, then other actions can be taken (maybe re-enable the button & color it red to show a failure).

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.