0

I am having a really strange issue. My script works when I run it from within Visual Studio Code, but when I go to the terminal and type the command line below, it does not work. The code detects when a USB device is inserted. When running from the terminal, the "Device Detected" line never appears.

python3.6 MAINtest.py

When running from the terminal, something happens where either the QProcess does not run, or it never detects output from usbDetectTEST.py . Again, this code works when I run it from within Visual Studio Code. What is the difference between running it there vs from the commandline?

These are the three files that replicate the issue I am having. Any insight would be greatly appreciated.

MAINtest.py

import sys
import time
import subprocess
from CLASSEStest import detectUSB # a file with the detectUSB class
from PyQt5 import QtCore, QtGui, QtWidgets, uic


qtcreator_file  = "GUI.ui" # This can be any .ui file, it does not matter
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtcreator_file)

class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):

        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        self.detectLogs = detectUSB()

        self.detectLogs.deviceDetected.connect(self.RefreshLogs2)
        self.detectLogs.begin()

    def RefreshLogs2(self):
        print("Device Detected")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

CLASSEStest.py

import sys
from PyQt5 import QtCore, QtGui
import pyudev
import time

class detectUSB(QtCore.QObject):
    deviceDetected = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        QtCore.QObject.__init__(self, parent)

    def begin(self):
        process = QtCore.QProcess(self)
        process.readyReadStandardOutput.connect(self.ReadyReadReady)
        process.start("python3.6 usbDetectTEST.py")
        process.waitForStarted()

    def ReadyReadReady(self):
        time.sleep(2) # it outputs 2 things every time one USB is detected unless this is here
        self.deviceDetected.emit()

usbDetectTEST.py

# Credit: https://avilpage.com/2016/09/detecting-device-events-in-ubuntu-with-python.html

import pyudev
import sys
import time

context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')

for device in iter(monitor.poll, None): # iter(monitor.poll, None)
    if device.action == 'add':
        #print(device)
        print(device.action, file = sys.stdout)

Update: I added " print(QtCore.QFile.exists("/my/path/usbDetectTEST.py")) " in the class right before it starts the program, and it sent back "False". So it cannot find the usbDetectTEST.py file. Not sure why...

  • add `process.errorOccurred.connect(print)` – eyllanesc Jul 13 '20 at 21:19
  • @eyllanesc I added " print(QtCore.QFile.exists("/my/path/usbDetectTEST.py")) " in the class right before it starts the program, and it sent back "False". So it cannot find the usbDetectTEST.py file. But I am not sure why... any suggestions? – RamboPenguin Jul 13 '20 at 21:24
  • use `print(device.action, file = sys.stdout, flush=True)` – eyllanesc Jul 13 '20 at 21:35
  • Why do you use QProcess? you could use pyudev directly: https://stackoverflow.com/questions/54870652/notify-qml-for-usb-device-inserted-events-using-pyqt5-and-pyudev/54876584#54876584: `self.observer = QtMonitorObserver()` `self.observer.filter_by("usb")` `self.observer.deviceAdded.connect(self.RefreshLogs2)` `self.observer.start()` – eyllanesc Jul 13 '20 at 21:37
  • @eyllanesc Thank you for your help once again. The flush=True portion solved the immediate issue. As far as just using pyudev directly, I looked at that exact post that you sent but I could not get it to work with my code. QtMonitorObserver was not working. I will try your suggestion. – RamboPenguin Jul 13 '20 at 22:03
  • @eyllanesc I didn't realize QtMonitorObserver() was from the file provided in that post. Went with your suggestion, and the code is much cleaner now. Thank you again.... – RamboPenguin Jul 13 '20 at 22:35

0 Answers0