英文:
Serial Read thread not running while flask thread is running python
问题
以下是您要翻译的内容:
"I am trying to read serial data on a raspberry pi and display it on a webpage within a Flask server
The issue that I am having is that when I run the code with both threads started, the thread that is listening for serial input is not running and only the flask server thread is running.
When running just the serial read thread, I can read what is being sent.
How can I allow both of these threads to run parallel, to allow the message to be displayed on the webpage?
Thanks"
from flask import Flask, render_template, request, url_for, flash, redirect, Response
from flask_socketio import SocketIO, emit
from threading import Thread
import time
import serial
global serialMessage
serialMessage = ""
class SerialReadProgram:
def __init__(self):
self._running = True
def terminate(self):
self._running = False
def run(self):
global serialMessage
ser = serial.Serial("/dev/ttyS0", 9600)
print("Serial Start")
while self._running:
serialMessage = ser.read() #read serial port
time.sleep(0.03)
data_left = ser.inWaiting() #check for remaining byte
serialMessage += ser.read(data_left)
print (serialMessage)
class FlaskServer:
app = Flask(__name__)
socketio = SocketIO(app=app, cors_allowed_origins='*')
def __init__(self):
self._running = True
def terminate(self):
self._running = False
@app.route('/', methods=('GET', 'POST'))
def home():
if request.method == 'POST':
message = request.form['message']
print(message)
return render_template('index.html')
# Handler for a message received over 'connect' channel
@socketio.on('connect')
def test_connect():
emit('after connect', {'data':'Connected'})
@socketio.on('Fetch For Data')
def value_changed():
global cycle
emit('Updated Message', {'data': serialMessage}, broadcast=True)
socketio.run(app, host='0.0.0.0', allow_unsafe_werkzeug=True)
# 创建类
SerialRead = SerialReadProgram()
# 创建线程
SerialReadThread = Thread(target=SerialRead.run)
# 启动线程
SerialReadThread.start()
# 创建类
Flask = FlaskServer()
# 创建线程
FlaskThread = Thread(target=Flask)
# 启动线程
FlaskThread.start()
英文:
I am trying to read serial data on a raspberry pi and display it on a webpage within a Flask server
The issue that I am having is that when I run the code with both threads started, the thread that is listening for serial input is not running and only the flask server thread is running.
When running just the serial read thread, I can read what is being sent.
How can I allow both of these threads to run parallel, to allow the message to be displayed on the webpage?
Thanks
from flask import Flask, render_template, request, url_for, flash, redirect, Response
from flask_socketio import SocketIO, emit
from threading import Thread
import time
import serial
global serialMessage
serialMessage = ""
class SerialReadProgram:
def __init__(self):
self._running = True
def terminate(self):
self._running = False
def run(self):
global serialMessage
ser = serial.Serial ("/dev/ttyS0", 9600)
print("Serial Start")
while self._running:
serialMessage = ser.read() #read serial port
time.sleep(0.03)
data_left = ser.inWaiting() #check for remaining byte
serialMessage += ser.read(data_left)
print (serialMessage)
class FlaskServer:
app = Flask(__name__)
socketio = SocketIO(app=app, cors_allowed_origins='*')
def __init__(self):
self._running = True
def terminate(self):
self._running = False
@app.route('/', methods=('GET', 'POST'))
def home():
if request.method == 'POST':
message = request.form['message']
print(message)
return render_template('index.html')
# Handler for a message recieved over 'connect' channel
@socketio.on('connect')
def test_connect():
emit('after connect', {'data':'Connnected'})
@socketio.on('Fetch For Data')
def value_changed():
global cycle
emit('Updated Message', {'data': serialMessage}, broadcast=True)
socketio.run(app, host='0.0.0.0', allow_unsafe_werkzeug=True)
#Create Class
SerialRead = SerialReadProgram()
#Create Thread
SerialReadThread = Thread(target=SerialRead.run)
#Start Thread
SerialReadThread.start()
#Create Class
Flask = FlaskServer()
#Create Thread
FlaskThread = Thread(target=Flask)
#Start Thread
FlaskThread.start()
答案1
得分: 1
将我的评论转换为答案
socketio.run
创建了自己的线程,所以不需要在线程中运行它,只需正常运行socketio服务器和serial在不同的线程中。
所以你的代码将会是这样的
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
from threading import Thread
import time
import serial
app = Flask(__name__)
socketio = SocketIO(app=app, cors_allowed_origins='*')
serial_message = ""
class SerialReadProgram:
def __init__(self):
self._running = True
def terminate(self):
self._running = False
def run(self):
global serial_message
ser = serial.Serial("/dev/ttyS0", 9600)
print("Serial Start")
while self._running:
serial_message = ser.readline().decode().strip() # 读取串口
print(serial_message)
time.sleep(0.03)
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
message = request.form['message']
print(message)
return render_template('index.html')
@socketio.on('connect')
def test_connect():
emit('after connect', {'data': 'Connected'})
@socketio.on('Fetch For Data')
def value_changed():
global serial_message
emit('Updated Message', {'data': serial_message}, broadcast=True)
if __name__ == '__main__':
serial_read = SerialReadProgram()
serial_read_thread = Thread(target=serial_read.run)
serial_read_thread.start()
socketio.run(app, host='0.0.0.0', debug=True)
英文:
Converting my comment as an answer
socketio.run
creates it's own thread so don't need to run it in thread just run normal socketio server and serial in diffrent thread.
So your code will look like this
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
from threading import Thread
import time
import serial
app = Flask(__name__)
socketio = SocketIO(app=app, cors_allowed_origins='*')
serial_message = ""
class SerialReadProgram:
def __init__(self):
self._running = True
def terminate(self):
self._running = False
def run(self):
global serial_message
ser = serial.Serial("/dev/ttyS0", 9600)
print("Serial Start")
while self._running:
serial_message = ser.readline().decode().strip() # read serial port
print(serial_message)
time.sleep(0.03)
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
message = request.form['message']
print(message)
return render_template('index.html')
@socketio.on('connect')
def test_connect():
emit('after connect', {'data': 'Connected'})
@socketio.on('Fetch For Data')
def value_changed():
global serial_message
emit('Updated Message', {'data': serial_message}, broadcast=True)
if __name__ == '__main__':
serial_read = SerialReadProgram()
serial_read_thread = Thread(target=serial_read.run)
serial_read_thread.start()
socketio.run(app, host='0.0.0.0', debug=True)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论