Issue
@app.route("/MODBUS", methods=["GET","POST"])
def mod():
if request.method == "POST":
setup.slave_id= request.form.get("slave_id",type=int)
setup.port = request.form.get("port",type=str)
setup.baudrate= request.form.get("baud_rate",type=int)
setup.num_registers= request.form.get("registers",type=int)
setup.timeout= request.form.get("timeout",type=int)
print(setup.slave_id)
print(setup.port)
print(setup.baudrate)
print(setup.num_registers)
print(setup.timeout)
setup.setup_modbus()
state= request.form.get("start_button")
state1= request.form.get("stop_button")
while(str(state) == "START"):
print(state)
openapplib.open_app_modbus()
startapplib.start_app_modbus()
if(str(state1) == "STOP"):
break
print(state1)
closeapplib.close_app_modbus()
return render_template("modbus.html")
I am running this flask server where i am using a html form to submit and update the values in a different python setup file. In the HTML file i am using "START" and "STOP" as inputs to call the above functions. But when i am inside the while when i click start and when i click stop it wont call the stop function which is in the if(str(state1) == "STOP"): condition.
This my HTML file
<h1>MODBUS SETTINGS</h1>
<form name="form1" id="form1" method="POST">
SLAVE_ID: <select name="slave_id" id="slave_id">
<option value="" selected="selected">Select Slave-ID</option>
</select>
<br><br>
PORT: <select name="port" id="port">
<option value="" selected="selected">Select No of Port</option>
</select>
<br><br>
REGISTERS: <select name="registers" id="registers">
<option value="" selected="selected">Select No of Registers</option>
</select>
<br><br>
BAUD_RATE: <select name="baud_rate" id="baud_rate">
<option value="" selected="selected">Select Baud-Rate</option>
</select>
<br><br>
TIMEOUT: <select name="timeout" id="timeout">
<option value="" selected="selected">Select Timeout</option>
</select>
<br><br>
<input type="submit" value="SETUP"/>
<br><br>
<input type="submit" name="start_button" value="START">
<br><br>
<input type="submit" name="stop_button" value="STOP">
<br><br>
<input type="button" value="BACK" onclick="history.back()">
<br><br>
</form>
Solution
When you have multiple submit buttons on a form, I believe only the clicked one is sent to the server. [https://www.coderslexicon.com/using-multiple-submit-buttons-with-a-single-form/
From your code, when you click on start
, only that button is sent and your while loop takes off. When you click on stop
, only that value is sent. The start
is never sent which means your condition while(str(state) == "START"):
is never triggered and so your stop
won't work.
Instead of using a while loop, use a thread and some javascript code on the front end
Stop is hidden or disabled until the start button is clicked
When you click the start button, you kick off a separate thread to handle the processing server side and return control to your form. The stop button is then enabled or becomes visible while the start button is hidden or disabled
When user clicks the stop button, you then stop the thread or the function server side and the start button becomes enabled again
Answered By - NoCommandLine
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.