Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. from PyQt5.QtWidgets import *
  5. from PyQt5.QtGui import *
  6. from PyQt5.QtCore import *
  7. from PyQt5 import *
  8. import serial
  9. import time
  10. import glob
  11. import codecs
  12. import binascii
  13.  
  14.  
  15. ser = serial.Serial()
  16. recieved = []
  17. class ComboBox(QComboBox):
  18. popupAboutToBeShown = pyqtSignal()
  19.  
  20. def showPopup(self):
  21. self.popupAboutToBeShown.emit()
  22. super(ComboBox, self).showPopup()
  23.  
  24. class Example(QMainWindow):
  25.  
  26.  
  27.  
  28.  
  29. def __init__(self):
  30. super().__init__()
  31. self.initUI()
  32.  
  33.  
  34. def initUI(self):
  35.  
  36. QToolTip.setFont(QFont('SansSerif', 10))
  37.  
  38. self.setToolTip('Это главное окно программы')
  39.  
  40. ## btn = QPushButton('Закрыть программу', self)
  41. ## btn.setToolTip('<b>Это кнопка</b>')
  42. ## btn.resize(btn.sizeHint())
  43. ## btn.move(100, 100)
  44. ## btn.clicked.connect(QCoreApplication.instance().quit)
  45.  
  46. global recieveBox
  47. recieveBox = QTextEdit(self) #текстовое поле отправки данных
  48. recieveBox.setGeometry(50, 100, 350, 240)
  49. recieveBox.setPlainText("Поле для приема данных")
  50. recieveBox.setToolTip("Поле для приема данных")
  51. ## while ser.isOpen:
  52. ## line = ser.readline()
  53. ## recieveBox.setText(line)
  54. ## while True:
  55. ## line = ser.read()
  56. ## if line:
  57. ## received.append(line.decode().strip())
  58.  
  59. clearRecieveBox = QPushButton('Очистить поле принятых данных', self)
  60. clearRecieveBox.resize(clearRecieveBox.sizeHint())
  61. clearRecieveBox.setGeometry(50, 345, 350, 35)
  62. clearRecieveBox.setToolTip("Нажмите чтобы очистить поле принятых данных")
  63. clearRecieveBox.clicked.connect(self.clear_Recieve_Box)
  64.  
  65.  
  66.  
  67. global transmitBox
  68. transmitBox = QTextEdit(self) # текстовое поле приема данных
  69. transmitBox.setGeometry(50, 385, 350, 60)
  70. transmitBox.setPlainText("Поле для отправки данных")
  71. transmitBox.setToolTip("Поле для отправки данных")
  72.  
  73. sendData= QPushButton('Отправить', self)
  74. sendData.resize(sendData.sizeHint())
  75. sendData.setGeometry(50, 450, 350, 35)
  76. sendData.setToolTip('Нажмите чтобы отправить данные')
  77. sendData.clicked.connect(self.send_Data)
  78.  
  79. global setPort
  80. setPort = ComboBox(self) # комбобокс для выбора порта
  81. setPort.setStatusTip('Hi!')
  82. setPort.setGeometry(50, 50, 50, 25)
  83. setPort.setToolTip('Выберите порт')
  84. setPort.resize(setPort.sizeHint())
  85. setPort.popupAboutToBeShown.connect(self.setPortClicked)
  86. setPort.activated[str].connect(self.setPort_Activated)
  87.  
  88. labelPort = QLabel(self) # лейбл "выберите порт"
  89. labelPort.move(50, 25)
  90. labelPort.setText('Выберите порт')
  91.  
  92. setSpeed = QComboBox(self) # комбобокс для выбора скорости
  93. setSpeed.setGeometry(150, 50, 50, 25)
  94. setSpeed.setToolTip('Выберите скорость')
  95. setSpeed.addItems(['1200', '2400', '4800', '9600', '14400', '19200', '56000', '115200'])
  96. setSpeed.resize(setSpeed.sizeHint())
  97. setSpeed.activated[str].connect(self.setSpeed_Activated)
  98.  
  99. labelSpeed = QLabel(self)# лейбл "выберите скорость"
  100. labelSpeed.move(150, 25)
  101. labelSpeed.setText('Выберите скорость')
  102.  
  103. closePortButton = QPushButton("Закрыть порт", self)
  104. closePortButton.setToolTip('Нажмите на кнопку чтобы закрыть порт')
  105. closePortButton.resize(closePortButton.sizeHint())
  106. closePortButton.move(250, 50)
  107. closePortButton.clicked.connect(self.closePort)
  108.  
  109. global portStatus
  110. portStatus = QLabel(self)
  111. portStatus.move(50, 75)
  112. portStatus.setText('Порт закрыт')
  113. portStatus.setStyleSheet("color: rgb(255, 0, 0);")
  114. portStatus.resize(portStatus.sizeHint())
  115.  
  116.  
  117. self.setGeometry(300, 300, 450, 500)
  118. self.setWindowTitle('Бесполезная программа')
  119. self.show()
  120.  
  121. exitAction = QAction(QIcon('web.ico'), '&Exit', self)
  122. exitAction.setShortcut('Ctrl+Q')
  123. exitAction.setStatusTip('Exit')
  124. exitAction.triggered.connect(qApp.quit)
  125.  
  126. printAction = QAction(QIcon('web.ico'), 'Printhello', self)
  127. printAction.setShortcut('Ctrl+H')
  128. printAction.setStatusTip('print Hello')
  129. printAction.triggered.connect(self.printhello)
  130.  
  131. self.statusBar()
  132. menubar = self.menuBar()
  133.  
  134. fileMenu = menubar.addMenu('&File')
  135. fileMenu.addAction(exitAction)
  136. optionsMenu = menubar.addMenu('&Options')
  137. optionsMenu.addAction(printAction)
  138.  
  139.  
  140. def setPortClicked(self):
  141. if ser.isOpen():
  142. ser.close()
  143. setPort.clear()
  144. setPort.addItems(self.serial_ports())
  145.  
  146.  
  147.  
  148. def serial_ports(self): # список компортов
  149.  
  150. if sys.platform.startswith('win'):
  151. ports = ['COM%s' % (i + 1) for i in range(256)]
  152. elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
  153. # this excludes your current terminal "/dev/tty"
  154. ports = glob.glob('/dev/tty[A-Za-z]*')
  155. else:
  156. raise EnvironmentError('Unsupported platform')
  157.  
  158. result = []
  159. for port in ports:
  160. try:
  161. ## ser.port = port
  162. ## ser.close()
  163. s = serial.Serial(port)
  164. s.close()
  165. result.append(port)
  166. except (OSError, serial.SerialException):
  167. pass
  168. return result
  169.  
  170. def closeEvent(self, event):
  171. reply = QMessageBox.question(self, 'Выход', "Закрыть программу?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  172. if reply == QMessageBox.Yes:
  173. event.accept()
  174. else:
  175. event.ignore()
  176.  
  177. def printhello(self):
  178. print('Hello!')
  179.  
  180. def setSpeed_Activated(self, text): # функция выбора скорости
  181. self.br = text
  182. ser.baudrate = text
  183. print(ser.baudrate)
  184. ## setSpeed.setStatusTip("установлена скорость")
  185.  
  186. def setPort_Activated(self, text): # функция выбора компорта и его открытия
  187. self.sp = text
  188. ser.port = text
  189. if (ser.isOpen == True):
  190. print("open")
  191. else:
  192. ser.open()
  193. print('%s' % ser.port + " opened")
  194. portStatus.setText('Порт ' + '%s' % ser.port + ' открыт')
  195. portStatus.setStyleSheet("color: rgb(0, 255, 0);")
  196. portStatus.resize(portStatus.sizeHint())
  197. ser.timeout = 1
  198. ser.writeTimeout = 10
  199. time.sleep(0.5)
  200.  
  201. def closePort(self):
  202. ser.close()
  203. if (ser.isOpen != True):
  204. print('%s' % ser.port + " closed")
  205. portStatus.setText('Порт ' + '%s' % ser.port + ' закрыт')
  206. portStatus.setStyleSheet("color: rgb(255, 0, 0);")
  207.  
  208. def send_Data(self):
  209. ## data = transmitBox.toPlainText()
  210. ## data.encode("ascii")
  211. ## print(data)
  212. ## print(help(data.encode))
  213. ## print(ascii(transmitBox.toPlainText()))
  214. ## print(ser)
  215. if ser.isOpen():
  216. ## ser.flushInput()
  217. ## ser.flushOutput()
  218. ## ser.write(ascii(transmitBox.toPlainText()))
  219. ser.write(transmitBox.toPlainText().encode())
  220. print(transmitBox.toPlainText().encode())
  221. ## s = str(transmitBox.toPlainText())
  222. ## print(s.hex())
  223. ## s.hex()
  224. ## ser.write(b'\x55')
  225.  
  226. def clear_Recieve_Box(self):
  227. print('RecieveBox cleared')
  228. recieveBox.clear()
  229.  
  230.  
  231. if __name__ == '__main__':
  232. app = QApplication(sys.argv)
  233. ex = Example()
  234. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement