def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) QtCore.QObject.connect(self.ui.R_color, QtCore.SIGNAL('valueChanged(int)'), self.R_color_changed) QtCore.QObject.connect(self.ui.G_color, QtCore.SIGNAL('valueChanged(int)'), self.G_color_changed) QtCore.QObject.connect(self.ui.B_color, QtCore.SIGNAL('valueChanged(int)'), self.B_color_changed) QtCore.QObject.connect(self.ui.R_color_spinbox, QtCore.SIGNAL('valueChanged(int)'), self.R_color_spinbox_changed) QtCore.QObject.connect(self.ui.G_color_spinbox, QtCore.SIGNAL('valueChanged(int)'), self.G_color_spinbox_changed) QtCore.QObject.connect(self.ui.B_color_spinbox, QtCore.SIGNAL('valueChanged(int)'), self.B_color_spinbox_changed) QtCore.QObject.connect(self.ui.SEND_button, QtCore.SIGNAL('clicked()'), self.send_message) QtCore.QObject.connect(self.ui.CONNECT_button, QtCore.SIGNAL('clicked()'), self.connect_serial) QtCore.QObject.connect(self.ui.DISCONNECT_button, QtCore.SIGNAL('clicked()'), self.disconnect_serial) QtCore.QObject.connect(self.ui.CLEAR_button, QtCore.SIGNAL('clicked()'), self.clear_textedit)
class MyForm(QtGui.QMainWindow): global ser, recvmsg, port, commthread, sendmsg def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) QtCore.QObject.connect(self.ui.R_color, QtCore.SIGNAL('valueChanged(int)'), self.R_color_changed) QtCore.QObject.connect(self.ui.G_color, QtCore.SIGNAL('valueChanged(int)'), self.G_color_changed) QtCore.QObject.connect(self.ui.B_color, QtCore.SIGNAL('valueChanged(int)'), self.B_color_changed) QtCore.QObject.connect(self.ui.R_color_spinbox, QtCore.SIGNAL('valueChanged(int)'), self.R_color_spinbox_changed) QtCore.QObject.connect(self.ui.G_color_spinbox, QtCore.SIGNAL('valueChanged(int)'), self.G_color_spinbox_changed) QtCore.QObject.connect(self.ui.B_color_spinbox, QtCore.SIGNAL('valueChanged(int)'), self.B_color_spinbox_changed) QtCore.QObject.connect(self.ui.SEND_button, QtCore.SIGNAL('clicked()'), self.send_message) QtCore.QObject.connect(self.ui.CONNECT_button, QtCore.SIGNAL('clicked()'), self.connect_serial) QtCore.QObject.connect(self.ui.DISCONNECT_button, QtCore.SIGNAL('clicked()'), self.disconnect_serial) QtCore.QObject.connect(self.ui.CLEAR_button, QtCore.SIGNAL('clicked()'), self.clear_textedit) def R_color_changed(self): self.ui.R_color_spinbox.setValue(self.ui.R_color.value()) def G_color_changed(self): self.ui.G_color_spinbox.setValue(self.ui.G_color.value()) def B_color_changed(self): self.ui.B_color_spinbox.setValue(self.ui.B_color.value()) def R_color_spinbox_changed(self): self.ui.R_color.setValue(self.ui.R_color_spinbox.value()) def G_color_spinbox_changed(self): self.ui.G_color.setValue(self.ui.G_color_spinbox.value()) def B_color_spinbox_changed(self): self.ui.B_color.setValue(self.ui.B_color_spinbox.value()) def send_message(self): global ser, sendmsg, commthread R_color = str(self.ui.R_color_spinbox.value()) G_color = str(self.ui.G_color_spinbox.value()) B_color = str(self.ui.B_color_spinbox.value()) mode = str(self.ui.mode_combobox.currentIndex() + 1) if mode == Send_mode['Write_program']: input_file = open("program.txt", "r") ser.write(mode + "1" + "090" + "\r") received = ser.readline() print "Recv: " + received program = input_file.read(16) while program != "": sendmsg = program + '\r' print "Send:" + sendmsg ser.write(sendmsg) received = ser.readline() print "Recv: " + received program = input_file.read(16) ser.write("DONE\r\n") received = ser.readline() print "Recv: " + received received = ser.readline() print "Recv: " + received elif mode == Send_mode['Read_program']: print mode + '\r' ser.write(mode + '\r') elif mode == Send_mode['Delete_program']: ser.write(mode + '\r') elif mode == Send_mode['Test_program']: ser.write(mode) elif mode == Send_mode['Play_program']: ser.write(mode) elif mode == Send_mode['Set_color']: self.timer = QtCore.QTimer() self.timer.setSingleShot(False) self.timer.timeout.connect(self.OTF_msg) self.timer.start(100) def OTF_msg(self): global ser QtCore.QTimer.interval R_color = str(self.ui.R_color_spinbox.value()) G_color = str(self.ui.G_color_spinbox.value()) B_color = str(self.ui.B_color_spinbox.value()) mode = Send_mode['Set_color'] sendmsg = mode + '{:>3}'.format(R_color) + '{:>3}'.format(G_color) + '{:>3}'.format(B_color) + '\r' print sendmsg ser.write(sendmsg) def clear_textedit(self): self.ui.textEdit.clear() def connect_serial(self): global ser, port, commthread print "Connect..." port = str(self.ui.Serial_port.currentText()) speed = str(self.ui.Serial_speed.currentText()) ser = serial.Serial(port = None, baudrate = speed, timeout = 5) ser.port = port try: ser.open() print "Connected" commthread = BackgroundThread(target=self.recvMsg) commthread.start() self.connect(commthread, QtCore.SIGNAL("MsgReceived(QString)"), self.showMsgInWindow ) except Exception, err: print err