Hello!

I'm trying to receive data from my COM port with simple setting I found from internet:-

Qt Code:
  1. from PyQt5 import QtCore, QtWidgets, QtSerialPort
  2. from PyQt5.QtCore import pyqtSignal
  3. from subprocess import Popen
  4. import time
  5.  
  6. class Widget(QtWidgets.QWidget):
  7. def __init__(self, parent=None):
  8. super(Widget, self).__init__(parent)
  9. self.message_le = QtWidgets.QLineEdit()
  10. self.send_btn = QtWidgets.QPushButton(
  11. text="Send",
  12. clicked=self.send
  13. )
  14. self.output_te = QtWidgets.QTextEdit(readOnly=True)
  15. self.button = QtWidgets.QPushButton(
  16. text="Connect",
  17. checkable=True,
  18. toggled=self.on_toggled
  19. )
  20. lay = QtWidgets.QVBoxLayout(self)
  21. hlay = QtWidgets.QHBoxLayout()
  22. hlay.addWidget(self.message_le)
  23. hlay.addWidget(self.send_btn)
  24. lay.addLayout(hlay)
  25. lay.addWidget(self.output_te)
  26. lay.addWidget(self.button)
  27.  
  28.  
  29. self.serial = QtSerialPort.QSerialPort(
  30. 'COM6',
  31. baudRate=921600,
  32. readyRead=self.receive
  33. )
  34.  
  35. @QtCore.pyqtSlot()
  36. def receive(self):
  37. while self.serial.canReadLine():
  38. text = self.serial.readLine().data().decode()
  39. text = text.rstrip('\r\n')
  40. self.output_te.append(text)
  41.  
  42. @QtCore.pyqtSlot()
  43. def send(self):
  44. self.serial.write(self.message_le.text().encode())
  45.  
  46. @QtCore.pyqtSlot(bool)
  47. def on_toggled(self, checked):
  48. self.button.setText("Disconnect" if checked else "Connect")
  49. if checked:
  50. if not self.serial.isOpen():
  51. if not self.serial.open(QtCore.QIODevice.ReadWrite):
  52. self.button.setChecked(False)
  53. else:
  54. self.serial.close()
  55.  
  56. if __name__ == '__main__':
  57. import sys
  58. app = QtWidgets.QApplication(sys.argv)
  59. w = Widget()
  60. w.show()
  61. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode 

This would work fine on my Raspberry Pi 4 with Ubuntu 20.04 and

python 3.8.5
PyQt5 5.14.1
PyQt5-sip 12.9.0
pyqtgraph 0.12.1

However, on my Windows 10, even though I reinstalled my packages to match the exact same version:-

Package Version
------------------------- ---------
PyQt5 5.14.1
PyQt5-sip 12.9.0
pyqtgraph 0.12.1
pytz 2021.1

It still would not get any data received even though HW has always been generating data.

And then, I noticed if I open my TeraTerm or Putty once to connect to the same COM port and disconnect even without any correct baud rate setting,
the Python script would start to receive the readyRead signal correctly without problem.

Therefore, I tried to add special script before the QSerialPort code like this:

Qt Code:
  1. process = Popen("putty.exe -serial com6", shell=True)
  2. time.sleep(1)
  3. Popen("TASKKILL /F /PID {pid} /T".format(pid=process.pid))
  4.  
  5. self.serial = QtSerialPort.QSerialPort(
  6. 'COM6',
  7. baudRate=921600,
  8. readyRead=self.receive
  9. )
To copy to clipboard, switch view to plain text mode 

and then the readyRead and data received would be ok.

Therefore, I am wondering if I am missing anything in my script which actually got triggered by the Teraterm or Putty on Windows 10 please.
On Ubuntu 20.04, I don't need that though.

I already tried older python 3.7.4 and pyqt5 5.9.2 on Windows 10 but it's the same issue.

Thanks for any suggestion in advance!