forked from Ultrawipf/OpenFFBoard-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_ui.py
88 lines (70 loc) · 2.87 KB
/
serial_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QWidget
from PyQt5.QtSerialPort import QSerialPort,QSerialPortInfo
from PyQt5.QtCore import QIODevice,pyqtSignal
import main
from base_ui import WidgetUI
class SerialChooser(WidgetUI):
connected = pyqtSignal(bool)
port = None
def __init__(self,serial, main):
WidgetUI.__init__(self, main,'serialchooser.ui')
self.serial = serial
self.pushButton_refresh.clicked.connect(self.getPorts)
self.pushButton_connect.clicked.connect(self.serialConnect)
self.comboBox_port.currentIndexChanged.connect(self.selectPort)
self.pushButton_send.clicked.connect(self.sendLine)
self.lineEdit_cmd.returnPressed.connect(self.sendLine)
self.getPorts()
self.ports = []
self.update()
def serialLog(self,txt):
self.serialLogBox.append(txt)
def sendLine(self):
cmd = self.lineEdit_cmd.text()+"\n"
self.serialLog(cmd)
#self.serial.write(bytes(cmd,"utf-8"))
self.serialLog(self.main.comms.serialGet(cmd))
def write(self,data):
self.serial.write(data)
def update(self):
if(self.serial.isOpen()):
self.pushButton_connect.setText("Disconnect")
self.comboBox_port.setEnabled(False)
self.pushButton_refresh.setEnabled(False)
self.pushButton_send.setEnabled(True)
self.lineEdit_cmd.setEnabled(True)
self.connected.emit(True)
else:
self.pushButton_connect.setText("Connect")
self.comboBox_port.setEnabled(True)
self.pushButton_refresh.setEnabled(True)
self.pushButton_send.setEnabled(False)
self.lineEdit_cmd.setEnabled(False)
self.connected.emit(False)
def serialConnect(self):
self.selectPort(self.comboBox_port.currentIndex())
if(not self.serial.isOpen() and self.port != None):
self.main.log("Connecting...")
self.serial.setPort(self.port)
self.serial.open(QIODevice.ReadWrite)
else:
self.serial.close()
self.update()
def selectPort(self,id):
if(id != -1):
self.port = self.ports[id]
else:
self.port = None
def getPorts(self):
oldport = self.port.portName() if self.port else None
self.ports = QSerialPortInfo().availablePorts()
self.comboBox_port.clear()
for port in self.ports:
self.comboBox_port.addItem(port.portName() + " : " + port.description())
self.update()
plist = [p.portName() for p in self.ports]
if oldport in plist:
self.comboBox_port.setCurrentIndex(plist.index(oldport))
self.selectPort(self.comboBox_port.currentIndex())