from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import serial import serial.tools.list_ports import xlwings as xw import time class MainWindow(QMainWindow): #deletes all graphs def deleteGraphs(self): if self.msht is not None: for chart in self.msht.charts: chart.delete() self.graphCreated = False def updateGraph(self): if self.x_axis and self.y_axis is not None: if (self.x_axis == "TIME" and self.y_axis == "TIME"): if (self.cRow > 101): self.chart.set_source_data(self.msht.range(f"B{self.cRow - 100}:B{self.cRow},B{self.cRow - 100}:B{self.cRow}")) else: self.chart.set_source_data(self.msht.range(f"B2:B{self.cRow},B2:B{self.cRow}")) elif (self.x_axis == "TIME"): currentColumn = None for column in self.columnNames.keys(): if column == self.y_axis: currentColumn = self.columnNames.get(column) break if (self.cRow > 101): self.chart.set_source_data( self.msht.range(f"B{self.cRow - 100}:B{self.cRow},{currentColumn}{self.cRow - 100}:{currentColumn}{self.cRow}")) else: self.chart.set_source_data(self.msht.range(f"B2:B{self.cRow},{currentColumn}2:{currentColumn}{self.cRow}")) elif (self.y_axis == "TIME"): currentColumn = None for column in self.columnNames.keys(): if column == self.x_axis: currentColumn = self.columnNames.get(column) break if (self.cRow > 101): self.chart.set_source_data( self.msht.range(f"{currentColumn}{self.cRow - 100}:{currentColumn}{self.cRow},B{self.cRow - 100}:B{self.cRow}")) else: self.chart.set_source_data(self.msht.range(f"{currentColumn}2:{currentColumn}{self.cRow},B2:B{self.cRow}")) else: xColumn = None yColumn = None for column in self.columnNames.keys(): if column == self.x_axis: xColumn = self.columnNames.get(column) if column == self.y_axis: yColumn = self.columnNames.get(column) if (self.cRow > 101): self.chart.set_source_data( self.msht.range(f"{xColumn}{self.cRow - 100}:{xColumn}{self.cRow},{yColumn}{self.cRow - 100}:{yColumn}{self.cRow}")) else: self.chart.set_source_data(self.msht.range(f"{xColumn}2:{xColumn}{self.cRow},{yColumn}2:{yColumn}{self.cRow}")) def graphCreator(self): if self.msht is not None: self.deleteGraphs() location = chr(len(self.columnNames) + 67) self.chart = self.msht.charts.add(left=self.msht.range(f"{location}1").left, top=self.msht.range(f"{location}1").top, width=500, height=300) self.chart.chart_type = 'line' self.graphCreated = True pass def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.xfile = None self.cBox = None self.msht = None self.cPort = None self.mainSheet = None self.arduino = None self.ipWrongWidget = None self.portSelected = False self.graphCreated = False #combobox secilince direkt ayarlanacak self.x_axis = "ddistance" self.y_axis = "deneme" self.cRow = 2; self.tCol = 'B' self.dCol = 'C' self.columnNames = dict() self.columnCount = 0 self.mainWidget = QWidget(self) self.setCentralWidget(self.mainWidget) self.mainLayout = QVBoxLayout(self) self.mainWidget.setLayout(self.mainLayout) self.addComboBox() def updateComboBox(self): self.cBox = None self.addComboBox() def addComboBox(self): self.cBox = QComboBox() available_ports = list(serial.tools.list_ports.comports()) for i in available_ports: self.cBox.addItem(str(i.name)) self.cBox.setFixedSize(200, 30) self.comboLayout = QHBoxLayout(self) self.comboLayout.addWidget(self.cBox) self.mainLayout.addLayout(self.comboLayout) self.baudBox = QLineEdit(self) self.baudBox.setFixedWidth(100) self.baudBox.setText('') self.mainLayout.addWidget(self.baudBox) self.resetButton = QPushButton() self.resetButton.setIcon(QIcon('refreshbuttonimage.png')) self.resetButton.setFixedSize(20, 20) self.resetButton.clicked.connect(self.resetPortSelector) self.comboLayout.addWidget(self.resetButton) self.connectButton = QPushButton("Connect") self.connectButton.setStyleSheet("background-color : green") self.connectButton.clicked.connect(self.connect) self.comboLayout.addWidget(self.connectButton) self.clearButton = QPushButton("Clear columns") self.clearButton.clicked.connect(self.clearData) self.comboLayout.addWidget(self.clearButton) self.resetBox = QCheckBox('Reset on connect') self.comboLayout.addWidget(self.resetBox) ###### self.debugButton = QPushButton("debug") self.debugButton.clicked.connect(self.graphCreator) self.comboLayout.addWidget(self.debugButton) ###### def resetPortSelector(self): self.cBox.clear() available_ports = list(serial.tools.list_ports.comports()) for i in available_ports: self.cBox.addItem(str(i.name)) def openExcel(self): self.xfile = xw.Book('Excel1File.xlsx') self.msht = self.xfile.sheets['Main Sheet'] if self.resetBox.isChecked(): self.msht.clear() self.cRow = 2 elif self.msht.range('A1').value is not None: self.cRow = int(self.msht.range('A1').value) self.msht.api.Rows.AutoFit() self.msht.api.Columns.AutoFit() def closeExcel(self): self.xfile = None self.msht = None def startWrite(self): if self.arduino is None: return self.writeExcelRT() def clearData(self): print('CLEAR DATA') if self.msht is not None: self.msht.clear() self.cRow=2 def selectColumn(self,i): return chr(ord('B')+i) def writeExcelRT(self): try: newData = self.arduino.readline().decode().rstrip() if newData: self.cCell = self.tCol + str(self.cRow) local_time = time.ctime(time.time()) self.msht.range(self.cCell).value = local_time splitData = newData.split(',') length = int(len(splitData)/2) print(length) for i in range(length): if self.columnNames.get(splitData[i]): cCol = self.columnNames.get(splitData[i]) else: self.columnCount += 1 self.columnNames[splitData[i]] = cCol = self.selectColumn(self.columnCount) self.msht.range(cCol+str(1)).value = str(splitData[i]) self.cCell = cCol + str(self.cRow) self.msht.range(self.cCell).value = str(splitData[i+length]) #self.cCell = self.dCol + str(self.cRow) #self.msht.range(self.cCell).value = str(newData) if self.graphCreated: self.updateGraph() self.cRow += 1 self.msht.range('A1').value = self.cRow except Exception as e: msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText(str(e)) msg.setWindowTitle("Write Error") msg.exec_() self.timer.stop() def connect(self): exception = True rate = 9600 if self.portSelected: # Check if a port has been selected try: self.arduino.close() # Try to close the existing connection except Exception as e: msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText(str(e)) msg.setWindowTitle("Error 1") msg.exec_() pass # Ignore any errors self.portSelected = False # Set portSelected to False before attempting to open a new port port = self.cBox.currentText() if port == " " or port is None: return port = port[:4] try: self.portSelected = True if self.baudBox.text() == '': rate = 9600 else: rate = self.baudBox.text() self.arduino = serial.Serial(port=port, baudrate=rate, timeout=.1) self.openExcel() self.timer = QTimer() self.timer.setInterval(200) self.timer.timeout.connect(self.startWrite) self.timer.start() except Exception as e: self.arduino = None self.portSelected = False self.timer = None exception = False msg = QMessageBox() msg.setIcon(QMessageBox.Critical) msg.setText(str(e)) msg.setWindowTitle("Arduino Error") msg.exec_() if self.arduino is not None and exception: self.connectButton.setText("Disconnect") self.connectButton.setStyleSheet("background-color : red") self.connectButton.clicked.disconnect(self.connect) self.connectButton.clicked.connect(self.disconnectArduino) def disconnectArduino(self): self.closeExcel() if self.arduino is not None: self.arduino = None self.portSelected = False self.timer = None self.connectButton.setText('Connect') self.connectButton.setStyleSheet("background-color : green") self.connectButton.clicked.disconnect(self.disconnectArduino) self.connectButton.clicked.connect(self.connect) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec_()