PyQt5 Qserialport Sub-threading operations

Source: Internet
Author: User

Environment:

python3.6 PYQT5

Just a simple idea, please ignore the fragile exception protection:

#-*-Coding:utf-8-*-import sysfrom pyqt5.qtwidgets import *from pyqt5.qtserialport import Qserialport, QSerialPortInfof Rom pyqt5.qtcore import pyqtsignal, Qthread, Qobject, Qtimerclass serialwork (qobject): def __init__ (self): Super (). __init__ () def init (self): self.com = Qserialport () self.com.setPortName (' COM7 ') SELF.COM.S Etbaudrate (115200) if Self.com.open (qserialport.readwrite) = = False:return Self.readtimer = QT        Imer () self.readtimer.timeout.connect (Self.readdata) Self.readtimer.start (+) def readdata (self): Revdata = Self.com.readAll () revdata = bytes (revdata) print ('%d read '% len (revdata)) class Pyqt_ser  Ial (qwidget): def __init__ (self): Super (). __init__ () Self.serialthread = Qthread () self.serialwork = Serialwork () self.serialwork.moveToThread (self.serialthread) self.serialthread.started.connect (Self.seria             Lwork.init)   Self.serialthread.start () if __name__ = = ' __main__ ': app = Qapplication (sys.argv) win = Pyqt_serial () w In.show () Sys.exit (App.exec_ ())

Places to be aware of:

1. Read and write operations on Qserialport need to be on the same thread and cannot operate across threads

2, Serialwork can not have a dead cycle, the reason may be the dead loop blocked the serial port receive event transmission, resulting in readall function has been unable to read data. If you want to join the dead loop, add qapplication.processevents () in the loop to read the serial data

3, through this sub-threading operation Qserialport routines roughly understand the two qthread use of the camp: Run and movetothread, haha

PyQt5 Qserialport Sub-threading operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.