-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCOM_UiHandler.py
86 lines (71 loc) · 3.17 KB
/
MyCOM_UiHandler.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
#-*- coding: utf-8 -*-
from time import ctime
from PyQt4.QtGui import QKeySequence, QIcon, QPixmap
from PyQt4.QtCore import Qt
from MyCOM_Ui import Ui_Form as MyCOM_UIForm
import Util
class MyCOM_UiHandler(MyCOM_UIForm):
def __init__(self, parent=None):
self.filterData=".*"
MyCOM_UIForm.__init__(self)
def setupWidget(self, wobj):
wobj.setWindowIcon(QIcon(QPixmap(":/icon/icons/logo.png")))
self.sendButton.setShortcut(QKeySequence(Qt.Key_Return + Qt.CTRL))
def getPortSettings(self):
settings = {
"port": None, "baund": 9600, "bytesize": 8,
"parity": "no", "stopbits": 1, "timeout": 1}
fieldmap = ("port", "baund", "bytesize", "parity", "stopbits")
settings_line = self.setComboBox.currentText().toUtf8().data()
fields = settings_line.split(':')
for i in xrange(len(fields)):
settings[fieldmap[i]] = fields[i]
return settings
def getDataAndType(self):
return self.sendTextEdit.toPlainText().toUtf8().data(), self.radioButton.isChecked() and "ascii" or "hex"
def onPortOpened(self):
self.openButton.setText(u"关闭")
self.tipsLabel.setText("++Port is open:)")
def onPortOpening(self):
self.tipsLabel.setText("Try to open port, please wait...")
def onPortClosed(self):
self.openButton.setText(u"打开")
self.tipsLabel.setText("--Port is closed:-")
def onSendData(self, data=None, _type="ascii"):
if not data: data = self.sendTextEdit.toPlainText()
if _type == "hex":
data = ''.join(data.split())
data = ' '.join([data[i:i+2] for i in xrange(0, len(data), 2)]).upper()
else:
data = data.replace('\n', '<br/>')
self.chatTextBrowser.append('<b>Send</b> @%s<br/><font color="white">%s</font><br/><br/>'
% (ctime(), data))
self.sendTextEdit.clear()
bytes = _type == "ascii" and len(data) or len(data) / 2
self.sendLcdNumber.display(self.sendLcdNumber.intValue() + bytes)
def onRecvData(self, data):
bytes = len(data)
if not self.radioButton.isChecked():
data = Util.toVisualHex(data)
else:
try:
data = Util.filterData(data,self.filterData)
except Exception, e:
print "Error filterData!",self.filterData,e
if not data:
return
data = data.replace('\n', '<br/>')
self.chatTextBrowser.append('<b>Recv</b> @%s<br/><font color="yellow">%s</font><br/><br/>'
% (ctime(), data))
self.recvLcdNumber.display(self.recvLcdNumber.intValue() + bytes)
def clearLcdNumber(self):
self.sendLcdNumber.display(0)
self.recvLcdNumber.display(0)
def clearHistory(self):
self.chatTextBrowser.clear()
def onFilterChanged(self):
self.filterData=self.filterRegex.displayText().toUtf8().data()
if self.filterData=='':
self.filterData='.*'
self.filterRegex.setText(self.filterData)
print self.filterData