0

I would like to check connection vie COM-Port permanently, I have found here how to check connection, but when I implement it to my application in order to check if device is connected I do not achieve wanted result, perhaps because I am checking it using QTimer every second, but maybe it goes okay only first time during powering on..

Hope with code it will be easier to understand.

So, I open port:

    import serial
    global ser
    from PyQt5.QtCore import QTimer
    timer = QTimer()
    def setting_COMPort():
        global ser
        ser = serial.Serial(
                  port='/dev/ttyAMA0',
                  baudrate = 115200,
                  parity=serial.PARITY_NONE,
                  stopbits=serial.STOPBITS_ONE,
                  bytesize=serial.EIGHTBITS,
                  timeout=1)

timer.timeout.connect(self.statusComPort)

in "main" function there is QTimer

global timer
timer.start(1000)

and in slot statusComPort I want to check every second via QTimer if device is connected.

def statusComPort(self):
    global ser
    colorr=self.palette()
    if(ser.isOpen()):
        self.lineEdit_6.setText(str("Port is open!"))
        print("Port is Open")
        colorr.setColor(self.backgroundRole(), QtGui.QColor(155,155,155))
        self.setPalette(colorr)
    else:
        self.lineEdit_6.setText(str("Error! Port is close!"))
        colorr.setColor(self.backgroundRole(), QtGui.QColor(155,0,5))
        self.setPalette(colorr)
        print("Port is close")

In case if connection is lost I change color of my window. The problem is that applied solution which was suggested in link does not work in this case.

So what should I change in my slot to achieve result..?

Maybe use different function, not isOpen() ?

OS - Raspbian

UPDATE 1

link to project, everything is cut, except for needed functionality https://github.com/ivanwolodin/statusofcomport

Summing up. I have Qtimer which acts every second, therefore my app every second goes into slot "statusComPort", where I want to check if device is connected via serial port. So I want to check status of my serial port permanently (every second) and question is: how should I write this slot to achieve correct check

John
  • 451
  • 6
  • 21
  • You could share your complete code through github, drive or similar. – eyllanesc Aug 14 '17 at 11:51
  • You must provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). This should be minimal and complete, that is, I must copy it and without adding anything else should reproduce your problem. – eyllanesc Aug 14 '17 at 15:37
  • 1
    I am asking for something reasonable to be able to test your code, it has happened to me many times that the authors of the question say they have made some code, but when I create my own test it works and then they tell me that they have misclassified their code, An mvce. If you do not provide what I ask unfortunately, I will not be able to help you. – eyllanesc Aug 14 '17 at 17:12
  • Okay, got it! Thank you! See update 1 – John Aug 14 '17 at 17:29
  • Why do not you make the serial object part of your main class?, Using global variables is a bad programming practice. – eyllanesc Aug 14 '17 at 17:37
  • If you can make it better I will be very grateful for that.. – John Aug 14 '17 at 17:38
  • Are you using pyqt4 or pyqt5? – eyllanesc Aug 14 '17 at 18:29
  • Are you using python2 or python3? – eyllanesc Aug 14 '17 at 18:31
  • If you use pyqt5 you could give a robust solution using QSerialPort. – eyllanesc Aug 14 '17 at 18:42
  • python 3 and pyqt4 – John Aug 14 '17 at 18:46
  • In your example you place pyqt5, in github pyqt4. Also in raspbian is available and you can install it with python3-pyqt5. – eyllanesc Aug 14 '17 at 18:50
  • I know, mix happened because I have different machines to execute the code.One support PyQt4 another one - PyQt5. But does it really matter since I am asking about content of the one single slot... – John Aug 14 '17 at 20:44
  • The problem I noticed is that pyserial does not realize that the serial has been disconnected unless you send a data. – eyllanesc Aug 14 '17 at 20:46
  • Instead QSerialPort checks if it is disconnected without sending data. – eyllanesc Aug 14 '17 at 20:48
  • Achso! Thank you very much, so I need to port the code to PyQt5 then... or maybe there is a way with pyserial sending some data? – John Aug 14 '17 at 20:50
  • And unfortunately QSerialPort was introduced in Qt5 and therefore in PyQt5 – eyllanesc Aug 14 '17 at 20:50
  • 1
    Sending data is not scalable since the device that receives the data does not expect you to send unnecessary data, it is more convenient to use QSerialPort, so I would recommend using pyqt5 – eyllanesc Aug 14 '17 at 20:52
  • Okay! Thank you very much for your time and for your help! – John Aug 14 '17 at 20:54
  • But which function in PyQt5 does the job by the way? – John Aug 14 '17 at 21:05
  • 1
    Just connect the error signal to some slot: `your_serial.error.connect(self.handleError)`, And then the slot will receive an error code, in your case: `def handleError(self, error): if error != QSerialPort.NoError: print("Serial Port error:" + self.sender().errorString())` – eyllanesc Aug 14 '17 at 21:11
  • Can you help me with the new topic please connected to this thread? https://stackoverflow.com/questions/45709986/pyqt5-qserialport-qsocketnotifier-invalid-socket-specified – John Aug 16 '17 at 09:37

0 Answers0