问题
I am trying to decide on a technology for developing a desktop application that can interface with the serial port. I have looked into python and it looks like a console based app would not be difficult - http://pyserial.sourceforge.net/ , and I have also looked at PyQt, which would fulfill the GUI portion of my project.
But is it possible to include 3rd party modules like PySerial in PyQt?
回答1:
PyQt and pyserial play nice together (in fact, I'm using them in one of my apps.) The examples on the pyserial website are mostly simple console-like examples, but there's no reason you cannot take data from any of the pyserial objects and use them in PyQt. You could, for instance, take data that you received over a serial port and push it into a QByteArray or NumPy array or anything similar as fits your fancy.
A couple of caveats: if you use Serial.readline(), it is blocking until it gets a newline (\n). This could be bad for your GUI. If you must use readline() instead of read(), I recommend putting your pyserial related activities in a separate thread. How you do this is up to you, but I'd recommend using Qt's built in threading. You can then do data conversions to Qt types and such inside the thread. More info on threading in pyqt.
Edit: almost forgot. If anyone cares, you can use PyQt to write console apps too. Just use QCoreApplication instead of QApplication. Not quite sure why one would do that in python, however, unless you were really fond of Qt's data types... in this case you could use PyQt and pyserial to write a pure console app :D
来源:https://stackoverflow.com/questions/11676874/3rd-party-python-modules-in-pyqt