-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserialreader.py
172 lines (135 loc) · 5.68 KB
/
serialreader.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import serial
import serial.tools.list_ports
def main():
app = QApplication(sys.argv)
global ex
ex = Arabirim()
sys.exit(app.exec_())
class ArabirimThread(QThread):
def __init__(self, parent=None):
QThread.__init__(self, parent)
self.emit(SIGNAL("SystemText(QString)"), "{0}\r\n".format("Background Thread Init"))
self.exiting = False
def __del__(self):
self.wait()
def run(self):
#print("Entering SerialLoop")
self.exiting = False;
self.emit(SIGNAL("SystemText(QString)"), "{0}\r\n".format("--- CONNECTED ---"))
while not self.exiting:
if ex.ser.isOpen:
time.sleep(0.1)
incomingSerial = ex.ser.read(ex.ser.inWaiting())
incomingLines = incomingSerial.split("\r\n")
for line in incomingLines:
if line:
self.emit(SIGNAL("IncomingSerialText(QString)"), "{0}\r\n".format(line))
ex.ser.close()
self.emit(SIGNAL("SystemText(QString)"), "{0}\r\n".format("--- DISCONNECTED ---"))
def stop(self):
self.exiting = True
class Arabirim(QWidget):
def __init__(self, parent=None):
super(Arabirim, self).__init__()
self.thread = ArabirimThread()
self.initUI()
def initUI(self):
self.connect(self.thread, SIGNAL("IncomingSerialText(QString)"), self.IncomingSerialText)
self.connect(self.thread, SIGNAL("SystemText(QString)"), self.SystemText)
self.PortName = QLabel('Port')
self.BaudRate = QLabel('Baud Rate')
self.PortNameEdit = QLineEdit("/dev/ttyUSB0")
self.BaudRateEdit = QLineEdit("115200")
self.PortList = QComboBox(self)
PortListItems = serial.tools.list_ports.comports()
for item in PortListItems:
if item[2] != 'n/a':
self.PortList.addItem(item[0])
self.connectbutton = QPushButton("&Connect")
self.connectbutton.clicked.connect(self.onConnectClicked)
self.TextEdit = QTextEdit()
self.TextDocument = QTextDocument(self.TextEdit)
self.TextCursor = QTextCursor(self.TextEdit.document())
self.CommandBox = QLineEdit()
self.CommandBox.returnPressed.connect(self.onSendClicked)
self.SendButton = QPushButton("&Send")
self.SendButton.clicked.connect(self.onSendClicked)
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(self.PortName, 1, 0)
grid.addWidget(self.PortList, 1, 1)
grid.addWidget(self.BaudRate, 2, 0)
grid.addWidget(self.BaudRateEdit, 2, 1)
grid.addWidget(self.connectbutton, 1, 3, 2, 1)
grid.addWidget(self.TextEdit, 3, 0, 5, 4)
grid.addWidget(self.CommandBox, 9, 0, 1, 3)
grid.addWidget(self.SendButton, 9, 3)
self.CommandBox.setEnabled(False)
self.SendButton.setEnabled(False)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Serial Port Tool')
self.show()
def onConnectClicked(self):
self.connectbutton.clicked.disconnect(self.onConnectClicked)
self.connectbutton.clicked.connect(self.onDisconnectClicked)
self.connectbutton.setText("&Disconnect")
portname = unicode(self.PortList.currentText())
baudrate = int(self.BaudRateEdit.text())
print("Connecting to port {0} {1}".format(portname, baudrate))
global serialopen
serialopen = True
self.ser = serial.Serial(portname, baudrate)
if not self.ser.isOpen:
self.ser.open()
if self.ser.isOpen:
self.thread.start()
self.CommandBox.setEnabled(True)
self.SendButton.setEnabled(True)
def onDisconnectClicked(self):
self.connectbutton.clicked.disconnect(self.onDisconnectClicked)
self.connectbutton.clicked.connect(self.onConnectClicked)
self.connectbutton.setText("&Connect")
if self.ser.isOpen:
serialopen = False
self.CommandBox.setEnabled(False)
self.SendButton.setEnabled(False)
self.thread.stop()
def onSendClicked(self):
command = "{0}\r\n".format(str(self.CommandBox.text()))
#print(command)
if self.ser.isOpen:
self.OutgoingSerialText(command)
self.ser.write(command)
def IncomingSerialText(self, line):
blockformat = QTextBlockFormat()
textformat = QTextCharFormat()
textformat.setProperty(QTextFormat.ForegroundBrush, QVariant(QBrush(QColor(Qt.red))))
self.TextCursor.setBlockFormat(blockformat)
self.TextCursor.setCharFormat(textformat)
self.TextCursor.insertText(line)
self.TextEdit.moveCursor(QTextCursor.End)
def OutgoingSerialText(self, line):
blockformat = QTextBlockFormat()
textformat = QTextCharFormat()
textformat.setProperty(QTextFormat.ForegroundBrush, QVariant(QBrush(QColor(Qt.blue))))
self.TextCursor.setBlockFormat(blockformat)
self.TextCursor.setCharFormat(textformat)
self.TextCursor.insertText(line)
self.TextEdit.moveCursor(QTextCursor.End)
def SystemText(self, line):
blockformat = QTextBlockFormat()
textformat = QTextCharFormat()
textformat.setProperty(QTextFormat.ForegroundBrush, QVariant(QBrush(QColor(Qt.black))))
self.TextCursor.setBlockFormat(blockformat)
self.TextCursor.setCharFormat(textformat)
self.TextCursor.insertText(line)
self.TextEdit.moveCursor(QTextCursor.End)
if __name__ == '__main__':
main()