commwidgets.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #
  2. # Moisture control - Serial communication widgets
  3. #
  4. # Copyright (c) 2013 Michael Buesch <m@bues.ch>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. from pymoistcontrol.util import *
  21. import os
  22. class SerialOpenDialog(QDialog):
  23. """Serial port connection dialog."""
  24. def __init__(self, parent):
  25. """Class constructor."""
  26. QDialog.__init__(self, parent)
  27. self.setLayout(QGridLayout(self))
  28. self.setWindowTitle("Select serial port")
  29. self.portCombo = QComboBox(self)
  30. if os.name.lower() == "posix":
  31. # Unix operating system
  32. # Add all serial devices from /dev to
  33. # the combo box.
  34. devNodes = QDir("/dev").entryInfoList(QDir.System,
  35. QDir.Name)
  36. select = None
  37. for node in devNodes:
  38. name = node.fileName()
  39. if not name.startswith("ttyS") and\
  40. not name.startswith("ttyUSB"):
  41. continue
  42. path = node.filePath()
  43. self.portCombo.addItem(path, path)
  44. if select is None and\
  45. name.startswith("ttyUSB"):
  46. # Select the first ttyUSB by default.
  47. select = self.portCombo.count() - 1
  48. if select is not None:
  49. self.portCombo.setCurrentIndex(select)
  50. elif os.name.lower() in ("nt", "ce"):
  51. # Windows operating system
  52. # Add 8 COM ports to the combo box.
  53. for i in range(8):
  54. port = "COM%d" % (i + 1)
  55. self.portCombo.addItem(port, port)
  56. else:
  57. raise Error("Operating system not supported")
  58. self.layout().addWidget(self.portCombo, 0, 0, 1, 2)
  59. self.okButton = QPushButton("&Ok", self)
  60. self.layout().addWidget(self.okButton, 1, 0)
  61. self.cancelButton = QPushButton("&Cancel", self)
  62. self.layout().addWidget(self.cancelButton, 1, 1)
  63. self.okButton.released.connect(self.accept)
  64. self.cancelButton.released.connect(self.reject)
  65. def getSelectedPort(self):
  66. """Get the selected port name."""
  67. index = self.portCombo.currentIndex()
  68. if index < 0:
  69. return None
  70. return self.portCombo.itemData(index)