Adding autocomplete to the portlist
This commit is contained in:
parent
c44b362d16
commit
5ed2c2e86b
@ -1,8 +1,10 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from PyQt4 import QtCore, QtGui
|
from PyQt4 import QtCore, QtGui
|
||||||
from serial import FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS, PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK,\
|
import os
|
||||||
PARITY_SPACE, STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
|
from serial import Serial, FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS, PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK,\
|
||||||
|
PARITY_SPACE, STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO, SerialException
|
||||||
|
from serial.tools import list_ports
|
||||||
|
|
||||||
from colourterm import fromUtf8, translate
|
from colourterm import fromUtf8, translate
|
||||||
|
|
||||||
@ -36,9 +38,9 @@ class Ui_ConnectDialog(object):
|
|||||||
self.portLabel = QtGui.QLabel(connectDialog)
|
self.portLabel = QtGui.QLabel(connectDialog)
|
||||||
self.portLabel.setObjectName(fromUtf8('portLabel'))
|
self.portLabel.setObjectName(fromUtf8('portLabel'))
|
||||||
self.deviceLayout.addWidget(self.portLabel, 0, 0, 1, 1)
|
self.deviceLayout.addWidget(self.portLabel, 0, 0, 1, 1)
|
||||||
self.portEdit = QtGui.QLineEdit(connectDialog)
|
self.portEdit = QtGui.QComboBox(connectDialog)
|
||||||
self.portEdit.setText(fromUtf8('/dev/ttyUSB0'))
|
|
||||||
self.portEdit.setObjectName(fromUtf8('portEdit'))
|
self.portEdit.setObjectName(fromUtf8('portEdit'))
|
||||||
|
self.portEdit.setEditable(True)
|
||||||
self.deviceLayout.addWidget(self.portEdit, 0, 1, 1, 1)
|
self.deviceLayout.addWidget(self.portEdit, 0, 1, 1, 1)
|
||||||
self.baudLabel = QtGui.QLabel(connectDialog)
|
self.baudLabel = QtGui.QLabel(connectDialog)
|
||||||
self.baudLabel.setObjectName(fromUtf8('baudLabel'))
|
self.baudLabel.setObjectName(fromUtf8('baudLabel'))
|
||||||
@ -115,6 +117,14 @@ class Ui_ConnectDialog(object):
|
|||||||
self.buttonBox.accepted.connect(connectDialog.accept)
|
self.buttonBox.accepted.connect(connectDialog.accept)
|
||||||
self.buttonBox.rejected.connect(connectDialog.reject)
|
self.buttonBox.rejected.connect(connectDialog.reject)
|
||||||
|
|
||||||
|
def update_port_list(self):
|
||||||
|
self.portEdit.clear()
|
||||||
|
ports = []
|
||||||
|
for port in self.serial_ports():
|
||||||
|
ports.append(port)
|
||||||
|
ports.sort()
|
||||||
|
self.portEdit.addItems(ports)
|
||||||
|
|
||||||
def retranslateUi(self, connectDialog):
|
def retranslateUi(self, connectDialog):
|
||||||
connectDialog.setWindowTitle(translate('ConnectDialog', 'Open Port'))
|
connectDialog.setWindowTitle(translate('ConnectDialog', 'Open Port'))
|
||||||
self.portLabel.setText(translate('ConnectDialog', 'Port:'))
|
self.portLabel.setText(translate('ConnectDialog', 'Port:'))
|
||||||
@ -151,6 +161,27 @@ class Ui_ConnectDialog(object):
|
|||||||
self.readingCheckBox.setText(translate('ConnectDialog', 'Reading'))
|
self.readingCheckBox.setText(translate('ConnectDialog', 'Reading'))
|
||||||
self.writingCheckBox.setText(translate('ConnectDialog', 'Writing'))
|
self.writingCheckBox.setText(translate('ConnectDialog', 'Writing'))
|
||||||
|
|
||||||
|
def serial_ports(self):
|
||||||
|
"""
|
||||||
|
Returns a generator for all available serial ports
|
||||||
|
"""
|
||||||
|
if os.name == 'nt':
|
||||||
|
# windows
|
||||||
|
for i in range(256):
|
||||||
|
try:
|
||||||
|
s = Serial(i)
|
||||||
|
s.close()
|
||||||
|
yield 'COM' + str(i + 1)
|
||||||
|
except SerialException:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# unix
|
||||||
|
try:
|
||||||
|
for port in list_ports.comports():
|
||||||
|
yield port[0]
|
||||||
|
except NameError:
|
||||||
|
# pyserial 2.6 cannot handle serial usb ports very well, in that case just do not provide a list
|
||||||
|
pass
|
||||||
|
|
||||||
class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
|
class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
|
||||||
|
|
||||||
@ -159,7 +190,7 @@ class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
|
|||||||
self.setupUi(self)
|
self.setupUi(self)
|
||||||
|
|
||||||
def getPort(self):
|
def getPort(self):
|
||||||
return unicode(self.portEdit.text())
|
return unicode(self.portEdit.currentText())
|
||||||
|
|
||||||
def getBaud(self):
|
def getBaud(self):
|
||||||
return int(unicode(self.baudComboBox.currentText()))
|
return int(unicode(self.baudComboBox.currentText()))
|
||||||
|
@ -166,6 +166,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
|
|||||||
output = ''
|
output = ''
|
||||||
|
|
||||||
def onOpenActionTriggered(self):
|
def onOpenActionTriggered(self):
|
||||||
|
self.connectDialog.update_port_list()
|
||||||
if self.connectDialog.exec_() == QtGui.QDialog.Accepted:
|
if self.connectDialog.exec_() == QtGui.QDialog.Accepted:
|
||||||
if not self.deviceClosed:
|
if not self.deviceClosed:
|
||||||
self.deviceClosed = True
|
self.deviceClosed = True
|
||||||
|
Loading…
Reference in New Issue
Block a user