-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCOM.py
109 lines (89 loc) · 3.77 KB
/
MyCOM.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#-*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
from MyCOM_UiHandler import MyCOM_UiHandler
from MySerial import MySerial
from Icons import *
import Util
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self)
self.ui = MyCOM_UiHandler()
self.flags = {"__isopen__": False, "__datatype__": "ascii"}
self.ui.setupUi(self)
self.ui.setupWidget(self)
self.__setupSignal()
config=Util.configRead()
try:
self.ui.setComboBox.addItems(config['ComboBoxList'])
self.ui.setComboBox.setCurrentIndex(config['curIndex'])
except Exception, e:
QtGui.QMessageBox.critical(self, "Error", '读取config.json错误')
def closeEvent(self, e):
if self.flags["__isopen__"]:
self.serial.terminate()
e.accept()
def __setupSignal(self):
self.ui.openButton.clicked.connect(self.__onOpenPort)
self.ui.sendButton.clicked.connect(self.__onSendData)
self.ui.resetArduinoButton.clicked.connect(self.__onResetArduino)
self.ui.clearHistoryButton.clicked.connect(self.ui.clearHistory)
self.ui.clearLcdButton.clicked.connect(self.ui.clearLcdNumber)
self.ui.filterRegex.textChanged.connect(self.ui.onFilterChanged)
def __openPort(self, settings=None):
if not settings:
settings = self.ui.getPortSettings()
ret, msg = Util.formatPortSettins(settings)
if not ret:
return False, msg
if not settings["port"]:
return False, u"错误的串口号"
self.serial = MySerial()
self.connect(self.serial.qtobj, QtCore.SIGNAL("NewData"), self.ui.onRecvData)
ret, msg = self.serial.open(settings)
return ret, msg
def __closePort(self):
self.serial.terminate()
self.ui.onPortClosed()
self.flags["__isopen__"] = False
def __onOpenPort(self):
if self.flags["__isopen__"]:
return self.__closePort()
self.ui.onPortOpening()
ret, msg = self.__openPort()
if not ret:
QtGui.QMessageBox.critical(self, "Error", msg)
else:
self.flags["__isopen__"] = True
self.serial.start()
self.ui.onPortOpened()
curText=self.ui.setComboBox.currentText()
curIndex=self.ui.setComboBox.currentIndex()
if self.ui.setComboBox.findText(curText)==-1:
self.ui.setComboBox.insertItem(0,curText)
config={}
combolist=[]
for i in range(self.ui.setComboBox.count()):
combolist.append(str(self.ui.setComboBox.itemText(i)))
combolist=sorted(set(combolist),key=combolist.index)
config['ComboBoxList']=combolist
config['curIndex']=curIndex
Util.configSave(config)
def __onSendData(self):
if not self.flags["__isopen__"]:
QtGui.QMessageBox.information(self, "Tips", u"请先打开串口")
return
data, _type = self.ui.getDataAndType()
ret, msg = Util.checkData(data, _type)
if not ret:
QtGui.QMessageBox.critical(self, "Error", u"%s" % msg)
return
self.ui.onSendData(data, _type)
if _type == "hex":
data = Util.toHex(''.join(data.split()))
self.serial.send(data, _type)
def __onResetArduino(self):
if not self.flags["__isopen__"]:
QtGui.QMessageBox.information(self, "Tips", u"请先打开串口")
return
self.serial.resetArduino()
QtGui.QMessageBox.information(self, "Tips", u"Reset Arduino完成")