我的程序中执行到这里时 while (time.time() - start_time <= 40): read_byte = uart_r.read(1) if read_byte != b'': print(".", end="") received_bytes_list.append(read_byte.decode())我的QTimer()停止了直到while执行完,怎么让这个while不影响我的QTimer
时间: 2024-03-13 16:45:22 浏览: 71
你可以将这个while循环放在一个单独的线程中运行,这样就不会阻塞主线程,也就不会影响QTimer的执行。下面是一个简单的示例代码:
```python
import threading
import time
def read_uart(uart_r, received_bytes_list):
start_time = time.time()
while (time.time() - start_time <= 40):
read_byte = uart_r.read(1)
if read_byte != b'':
print(".", end="")
received_bytes_list.append(read_byte.decode())
# 在主线程中启动一个QTimer
timer = QTimer()
timer.timeout.connect(my_function)
timer.start(1000)
# 在另一个线程中执行读取串口数据的操作
uart_thread = threading.Thread(target=read_uart, args=(uart_r, received_bytes_list))
uart_thread.start()
```
在上面的代码中,`read_uart`函数会在一个单独的线程中执行,不会阻塞主线程。同时,在主线程中启动了一个QTimer,保证每隔一段时间会执行`my_function`函数。
相关问题
我在我的ui类中建立了两个线程其中执行到这个线程时线程等待直到运行完为止,是因为这个线程中while的存在吗?线程代码:uart_r = self.uart_r received_bytes_list = [] start_time = time.time() while (time.time() - start_time <= 40): read_byte = uart_r.read(1) if read_byte != b'': print(".", end="") received_bytes_list.append(read_byte.decode()) # self.uart_plot() if len(received_bytes_list) == 0: print("Warning: timeout!") else: print("Received data successfully!") # Save to Excel file received_bytes = ''.join(received_bytes_list) received_data = received_bytes.split() received_data_8 = [received_data[i: i + 8] for i in range(0, len(received_data), 8)] df = pd.DataFrame(columns=['0', '1', '2', '3', '4', '5', '6', '7']) for i, str in enumerate(received_data_8): print("Write to excel line:", i, str) if (len(str) < 8): print("Populate data.") str += [0] * (8 - len(str)) df.loc[i] = str df.to_excel('%s.xlsx' % TIME)
是的,这个线程中的 while 循环会一直阻塞线程,直到满足条件才会退出循环,因此其他线程需要等待该线程执行完毕后才能继续执行。如果您想让该线程在等待数据的同时不阻塞其他线程,您可以将 while 循环放在一个单独的线程中执行,然后使用线程间通信机制(例如 Queue)来传递数据。这样,其他线程就可以在该线程等待数据的同时继续执行。
我想在接收串口数据并写入数据这个方法里,同时进行折线图绘制串口数据,方法为: def writ_excel(self, TIME): uart_r = self.uart_r received_bytes_list = [] start_time = time.time() while (time.time() - start_time <= 40): read_byte = uart_r.read(1) if read_byte != b'': print(".", end="") received_bytes_list.append(read_byte.decode()) if len(received_bytes_list) == 0: print("Warning: timeout!") else: print("Received data successfully!") # Save to Excel file received_bytes = ''.join(received_bytes_list) received_data = received_bytes.split() received_data_8 = [received_data[i: i + 8] for i in range(0, len(received_data), 8)] df = pd.DataFrame(columns=['0', '1', '2', '3', '4', '5', '6', '7']) for i, str in enumerate(received_data_8): print("Write to excel line:", i, str) if (len(str) < 8): print("Populate data.") str += [0] * (8 - len(str)) df.loc[i] = str df.to_excel('%s.xlsx' % TIME)
好的,您可以在这个方法中添加绘制折线图的代码。具体来说,您需要在方法开始时创建一个Matplotlib图形窗口和坐标轴对象,然后在每次接收到数据后更新折线图。下面是一个示例代码:
```python
import matplotlib.pyplot as plt
import pandas as pd
def writ_excel(self, TIME):
uart_r = self.uart_r
received_bytes_list = []
start_time = time.time()
# 创建Matplotlib图形窗口和坐标轴对象
fig, ax = plt.subplots()
line, = ax.plot([], [])
ax.set_xlim(0, 100)
ax.set_ylim(0, 1024)
ax.set_title('Real-time data')
xdata, ydata = [], []
while (time.time() - start_time <= 40):
read_byte = uart_r.read(1)
if read_byte != b'':
print(".", end="")
received_bytes_list.append(read_byte.decode())
# 更新折线图
data = ''.join(received_bytes_list).strip()
if data:
x, y = data.split(',')
xdata.append(int(x))
ydata.append(int(y))
line.set_data(xdata, ydata)
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
if len(received_bytes_list) == 0:
print("Warning: timeout!")
else:
print("Received data successfully!")
# 保存数据到Excel文件
received_bytes = ''.join(received_bytes_list)
received_data = received_bytes.split()
received_data_8 = [received_data[i: i + 8] for i in range(0, len(received_data), 8)]
df = pd.DataFrame(columns=['0', '1', '2', '3', '4', '5', '6', '7'])
for i, str in enumerate(received_data_8):
print("Write to excel line:", i, str)
if (len(str) < 8):
print("Populate data.")
str += [0] * (8 - len(str))
df.loc[i] = str
df.to_excel('%s.xlsx' % TIME)
```
在这个示例代码中,我们首先在方法开始时创建了Matplotlib图形窗口和坐标轴对象。然后,在每次接收到数据时,我们更新了数据列表,并解析接收到的数据并更新折线图。最后,我们在保存数据到Excel文件之前,将数据存储在DataFrame对象中。
请注意,这只是一个示例代码,您需要根据您的具体需求进行修改和扩展。
阅读全文