This commit is contained in:
Raoul Snyman 2013-12-10 10:44:35 +02:00
commit 1cc6a13596
2 changed files with 42 additions and 10 deletions

View File

@ -1,8 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
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,\ 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 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 +37,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'))
@ -151,7 +152,6 @@ 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'))
class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog): class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
def __init__(self): def __init__(self):
@ -159,7 +159,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()))
@ -178,3 +178,33 @@ class ConnectDialog(QtGui.QDialog, Ui_ConnectDialog):
def getHardwareHandshake(self): def getHardwareHandshake(self):
return self.hardwareCheckBox.isChecked() 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 -*- # -*- coding: utf-8 -*-
import os
import threading import threading
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
@ -175,6 +176,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
output = '' output = ''
def onOpenActionTriggered(self): def onOpenActionTriggered(self):
self.connectDialog.updatePortCombobox()
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
@ -304,9 +306,9 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
output = u'<div style="%s">%s</div>' % (style, unicode(output, u'utf-8')) output = u'<div style="%s">%s</div>' % (style, unicode(output, u'utf-8'))
except TypeError: except TypeError:
output = u'<div style="%s">%s</div>' % (style, output) output = u'<div style="%s">%s</div>' % (style, output)
else: else:
output = u'<div>%s</div>' % output output = u'<div>%s</div>' % output
return output return output
def saveHighlights(self, highlights): def saveHighlights(self, highlights):
settings = QtCore.QSettings() settings = QtCore.QSettings()