Advertisement
Guest User

Untitled

a guest
May 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Aug 16 19:30:09 2017
  4. @author: rlc
  5. """
  6. import sys
  7. import ctypes
  8. import time
  9. import pyqtgraph as pg
  10. import threading
  11. import serial
  12. from collections import deque
  13. from PyQt5.QtCore import *
  14. from PyQt5.QtGui import *
  15. __version__ = '1.0'
  16. class VentanaPrincipal(QMainWindow):
  17. newdata = pyqtSignal(object)
  18. def __init__(self, filename=None, parent=None):
  19. super(VentanaPrincipal, self).__init__(parent)
  20. length = 400
  21. self.ColaEntradaX = deque([1.]*length)
  22. self.ColaEntradaY = deque([1.]*length)
  23. self.ColaEntradaZ = deque([1.]*length)
  24.  
  25. self.ColaTiempos = deque([0.]*length)
  26. self.Conectado = False
  27. self.Puerto = 'COM6'
  28. self.baud = 115200
  29.  
  30. QTimer.singleShot(0, self.IniciarHilo)
  31.  
  32. self.cwidget = QWidget()
  33. vb = QVBoxLayout()
  34.  
  35. p = pg.PlotWidget()
  36. vb.addWidget(p)
  37. p2 = pg.PlotWidget()
  38. vb.addWidget(p2)
  39. p3 = pg.PlotWidget()
  40. vb.addWidget(p3)
  41.  
  42.  
  43.  
  44. self.cwidget.setLayout(vb)
  45. self.setCentralWidget(self.cwidget)
  46. self.curve = p.plot(pen=(255,0,0))
  47. self.curve3 = p3.plot(pen=(0,0,255))
  48. self.curve2 = p2.plot(pen=(0,255,0))
  49.  
  50. self.curve.setData(self.ColaEntradaX, self.ColaEntradaX)
  51.  
  52. self.curve3.setData(self.ColaEntradaZ, self.ColaEntradaZ)
  53.  
  54. self.curve2.setData(self.ColaEntradaY, self.ColaEntradaY)
  55.  
  56. self.newdata.connect(self.onNewData)
  57.  
  58. def onNewData(self, signal):
  59. if self.ColaTiempos[0] != 0:
  60. self.curve.setData(self.ColaTiempos, signal[0])
  61. self.curve2.setData(self.ColaTiempos, signal[1])
  62. self.curve3.setData(self.ColaTiempos, signal[2])
  63.  
  64.  
  65. else:
  66. self.curve.setData(signal[0])
  67. self.curve2.setData(signal[1])
  68. self.curve3.setData(signal[2])
  69.  
  70. def IniciarHilo(self):
  71. print('Start lisnening to the COM-Puerto')
  72. serial_Puerto = serial.Serial(self.Puerto, self.baud, timeout=0)
  73. thread = threading.Thread(target=self.LeerArduino, args=(serial_Puerto,))
  74. thread.setDaemon(True) #Makes sures that the threas stops when exitting the program
  75. thread.start()
  76. self.tstart = time.clock()
  77.  
  78. def DetenerHilo(self):
  79. print('Stop the thread...')
  80.  
  81. def ManejadorDatos(self,data):
  82. listadatos= data.split("\r\n")
  83. for i, val in enumerate(listadatos):
  84. if "|" in listadatos[i] and "$" in listadatos[i] and "#" in listadatos[i] and "!" in listadatos[i]:
  85. datoRevisado = listadatos[i]
  86. datoRevisado= datoRevisado.replace("!","")
  87. datoRevisado=datoRevisado.replace("#","")
  88. datoRevisado=datoRevisado.replace("$","|")
  89. X,Y,Z=datoRevisado.split("|")
  90.  
  91. try:
  92. X = float(X)
  93. except ValueError:
  94. X = 0.
  95. try:
  96. Y = float(Y)
  97. except ValueError:
  98. Y = 0.
  99. try:
  100. Z = float(Z)
  101. except ValueError:
  102. Z = 0.
  103.  
  104. self.ColaEntradaX.append(X)
  105. self.ColaEntradaY.append(Y)
  106. self.ColaEntradaZ.append(Z)
  107. t = time.clock()
  108. dt = t-self.tstart
  109. self.ColaTiempos.append(dt)
  110.  
  111.  
  112. fullsignal = [self.ColaEntradaX,self.ColaEntradaY,self.ColaEntradaZ]
  113. self.newdata.emit(fullsignal)
  114.  
  115.  
  116. else:
  117. pass
  118. def LeerArduino(self,ser):
  119. reading=""
  120. while True:
  121. bytesToRead = ser.inWaiting()
  122. reading = ser.read(bytesToRead).decode('utf-8','ignore')
  123. if len(reading) > 1:
  124. self.ManejadorDatos(reading)
  125.  
  126.  
  127.  
  128.  
  129. def closeEvent(self, event):
  130. if self.okToContinue():
  131. event.accept()
  132. self.DetenerHilo()
  133. else:
  134. event.ignore()
  135.  
  136. def okToContinue(self):
  137. return True
  138.  
  139. if __name__ == '__main__':
  140.  
  141. app = QApplication(sys.argv)
  142. form = VentanaPrincipal()
  143.  
  144. form.show()
  145. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement