Autocomplete Ports using pyserials tools-method. Requires pyserial 2.6 or higher.

This commit is contained in:
Patrick Brueckner 2013-12-10 10:31:38 +02:00 committed by Raoul Snyman
commit af7bb5a791
2 changed files with 45 additions and 13 deletions

View File

@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
import os
from PyQt4 import QtCore, QtGui
from serial import FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS, PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK,\
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
@ -36,9 +37,9 @@ class Ui_ConnectDialog(object):
self.portLabel = QtGui.QLabel(connectDialog)
self.portLabel.setObjectName(fromUtf8('portLabel'))
self.deviceLayout.addWidget(self.portLabel, 0, 0, 1, 1)
self.portEdit = QtGui.QLineEdit(connectDialog)
self.portEdit.setText(fromUtf8('/dev/ttyUSB0'))
self.portEdit = QtGui.QComboBox(connectDialog)
self.portEdit.setObjectName(fromUtf8('portEdit'))
self.portEdit.setEditable(True)
self.deviceLayout.addWidget(self.portEdit, 0, 1, 1, 1)
self.baudLabel = QtGui.QLabel(connectDialog)
self.baudLabel.setObjectName(fromUtf8('baudLabel'))
@ -151,7 +152,6 @@ class Ui_ConnectDialog(object):
self.readingCheckBox.setText(translate('ConnectDialog', 'Reading'))
self.writingCheckBox.setText(translate('ConnectDialog', 'Writing'))
class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
def __init__(self):
@ -159,7 +159,7 @@ class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
self.setupUi(self)
def getPort(self):
return unicode(self.portEdit.text())
return unicode(self.portEdit.currentText())
def getBaud(self):
return int(unicode(self.baudComboBox.currentText()))
@ -178,3 +178,33 @@ class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
def getHardwareHandshake(self):
return self.hardwareCheckBox.isChecked()
def updatePortCombobox(self):
self.portEdit.clear()
ports = []
for port in self._getSerialPorts():
ports.append(port)
ports.sort()
self.portEdit.addItems(ports)
def _getSerialPorts(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

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import os
import threading
from PyQt4 import QtCore, QtGui
@ -166,6 +167,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
output = ''
def onOpenActionTriggered(self):
self.connectDialog.updatePortCombobox()
if self.connectDialog.exec_() == QtGui.QDialog.Accepted:
if not self.deviceClosed:
self.deviceClosed = True
@ -239,7 +241,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
self.sendComboBox.insertItem(0, output)
self.sendComboBox.setCurrentIndex(0)
self.sendComboBox.clearEditText()
self.device.write(output + u'\r\n')
self.device.write(output + '\r\n')
def onContentsSizeChanged(self, size):
if self.followOutput: