简体   繁体   English

带有线程的Python串行端口-冻结计算机

[英]Python Serial Port with threading - freezing computer

Okay, time for another question/post... 好的,是时候再问一个问题/发表...

So currently i am trying to develop a simple python program that has a webkit/ webpage view and a serial port interface.. Not that it should matter, but this is also running on a raspberry pi. 因此,当前我正在尝试开发一个具有webkit /网页视图和串行端口接口的简单python程序。没关系,但这也可以在树莓派上运行。

The following code works fine.. But it will freeze the system as soon as i uncomment the serial port line that you can see commented out. 以下代码可以正常工作..但是,只要我取消注释您可以看到已注释掉的串行端口行,它将立即冻结系统。

The day has been long and this one for some reason has my brain fried.. Python is not my strongest point, but mind you this is just a quick test script for now... Yes i have used google and other resources... 这一天已经很长了,由于某种原因,这使我的大脑发疯了。.Python不是我的长处,但是请注意,这只是一个快速的测试脚本...是的,我已经使用了Google和其他资源...

#!/usr/bin/env python

import sys
import serial
import threading
import time

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

sURL = ""
sURL2 = ""
objSerial = serial.Serial(0)

def SerialLooper():
    global objSerial
    if objSerial.isOpen() == True:
        print("is_responding")
        #objSerial.write("is_responding")
        time.sleep(10)
        SerialLooper()

class TestCLASS(object):
    def __init__(self):
        global sURL
        global sURL2
        global objSerial
        objSerial = serial.Serial(0)
        sURL = "http://localhost/tester"
        app = QApplication(sys.argv)
        webMain = QWebView()
        webMain.loadFinished.connect(self.load_finished)
        webMain.load(QUrl(sURL))
        webMain.show()
        thread = threading.Thread(target=SerialLooper)
        thread.start()
        sys.exit(app.exec_())

    def load_finished(boolNoErrors):
        global sURL
        print("Url - " + sURL)
        #something here
        #something else here

newObjClass = TestCLASS()

EDIT Futher on this, it appears its not the multithreading but the serial.write() 对此进行编辑 ,它似乎不是多线程的,而是serial.write()

It has been a while since I used serial, but IIRC it is not threadsafe (on Windows at least). 自从我使用串行以来已经有一段时间了,但是IIRC并不是线程安全的(至少在Windows上是这样)。 You are opening the port in the main thread and performing a write in another thread. 您正在打开主线程中的端口,并在另一个线程中执行写操作。 It's a bad practice anyway. 无论如何,这是一个坏习惯。 You might also consider writing a simple single-threaded program to see if the serial port is actually working. 您可能还考虑编写一个简单的单线程程序,以查看串行端口是否真正在工作。

PS Your program structure could use some work. PS您的程序结构可能需要一些工作。 You only need one of the global statements (global objSerial), the rest do nothing. 您只需要一个全局语句(global objSerial),其余的什么都不做。 It would be better to get rid of that one, too. 最好也摆脱掉那个。

And the recursive call to SerialLooper() will eventually fail when the recursion depth is exceeded; 当超过递归深度时,对SerialLooper()的递归调用最终将失败。 why not just use a while loop... 为什么不只是使用while循环...

def SerialLooper():
    while objSerial().isOpen():  # Drop the == True
        # print something 
        # write to the port
        # Sleep or do whatever

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM