From The Compiler, 5 Years ago, written in Python.
This paste is a reply to Re: serial fail from The Compiler
- view diff
Embed
  1. #!/usr/bin/python
  2.  
  3. import sys
  4.  
  5. from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
  6. from PyQt5.QtCore import QObject, pyqtSlot, QIODevice, pyqtSignal
  7. from PyQt5.QtSerialPort import QSerialPort
  8.  
  9. PORT = 'COM59'
  10.  
  11. class DVIFlasher(QObject):
  12.     ser = None
  13.     flash_done = pyqtSignal(int, int)
  14.  
  15.     def __init__(self):
  16.         super(DVIFlasher, self).__init__()
  17.         self.ser = QSerialPort(PORT)
  18.         self.ser.readyRead.connect(self.read)
  19.  
  20.     def flash(self):
  21.         print(self.ser.open(QIODevice.ReadWrite))
  22.         self.ser.setBaudRate(115200)
  23.  
  24.     def read(self):
  25.         print("Got data")
  26.         self.ser.close()
  27.         self.flash_done.emit(0, 0)
  28.  
  29. class Widget(QWidget):
  30.     def __init__(self):
  31.         super(Widget, self).__init__()
  32.         self.btn = QPushButton("Start", self)
  33.         self.flasher = DVIFlasher()
  34.         self.btn.clicked.connect(self.flasher.flash)
  35.         self.resize(self.btn.sizeHint())
  36.  
  37. if __name__ == '__main__':
  38.     app = QApplication(sys.argv)
  39.     win = Widget()
  40.     win.show()
  41.     sys.exit(app.exec_())