当我尝试从函数发送和接收列表时 PyQt5 界面崩溃
PyQt5 Interface Crashes When I Try to Send&Receive List From Function
我是 Python 的初学者。
我正在尝试为串行通信创建接口。在我的应用程序中,有 4 个变量,每个 2 个字节。我有启动器、命令、源和目标字节。所以,我总共有 4*2+4= 12 个字节的列表。
我正在尝试发送此列表并收回。
没有控制台错误,但是当我尝试发送列表时,它崩溃了。
ui.SendAll_Button.clicked.connect(lambda: com.send(self.newPackage))
如果我发送正常列表,没有功能,它可以工作。但是newPackage在ValuetoHex函数里面,是不行的。
我在这里留下我的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import Max2828_Window as myWindow
import Max2828_WindowEdit as EditModule
import serial
from time import sleep
import struct
con = serial.Serial('COM3', 9600 , timeout = 1)
error_message = [0X65, 0X72, 0X72, 0X6F, 0X72]
starter_byte = 0XAA
source_byte = 0X01
max2828_byte = 0X02
max5866_byte = 0X03
command_byte = 0XBB
byte_1 = 0X00
byte_2 = 0X00
byte_3 = 0X00
byte_4 = 0X00
byte_5 = 0X00
byte_6 = 0X00
byte_7 = 0X00
byte_8 = 0X00
class Communication():
Package = [starter_byte, source_byte, max2828_byte, command_byte,
byte_1, byte_2, byte_3, byte_4,
byte_5, byte_6, byte_7, byte_8]
def send(self, PackageList):
print(str(PackageList))
con.write(PackageList)
def receive(self,PackageList):
our_buffer = []
counter = 1
while counter <= len(PackageList):
msg = con.read()
our_buffer.append(msg)
print ("received " + str("".join( chr(x) for x in msg)))
print (str(our_buffer))
if len(our_buffer) >= len(PackageList):
our_buffer = None
counter = counter + 1
## sleep(1)
def main(self, PackageList):
self.send(PackageList)
self.receive(PackageList)
##com = Communication()
##com.main()
这也是主页:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import Max2828_Window as myWindow
import Max2828_Communication as myCommunication
import serial
from time import sleep
import struct
import os
starter_byte = 0XAA
source_byte = 0X01
max2828_byte = 0X02
max5866_byte = 0X03
command_byte = 0XBB
class WindowEdit(object):
def mainloop(self):
app = QtWidgets.QApplication(sys.argv)
ui = myWindow.Ui_MainWindow()
MainWindow = ui.callMain()
ui.setupUi(MainWindow)
MainWindow.show()
self.ValuetoHex(ui)
##########
com = myCommunication.Communication()
########## Menu Side
ui.actionExit.triggered.connect(ui.close_application)
########## ScrollBar Side
ui.RFFrequency_ScrollBar.valueChanged.connect(lambda: ui.ScrollBarValue(ui.RFFrequency_ScrollBar, ui.RFFrequency_Line))
ui.PADACOutputBias_ScrollBar.valueChanged.connect(lambda: ui.ScrollBarValue(ui.PADACOutputBias_ScrollBar, ui.PADACOutputBias_Line))
ui.RXVGAGain_ScrollBar.valueChanged.connect(lambda: ui.ScrollBarValue(ui.RXVGAGain_ScrollBar, ui.RXVGAGain_Line))
ui.TXVGAGain_ScrollBar.valueChanged.connect(lambda: ui.ScrollBarValue(ui.TXVGAGain_ScrollBar, ui.TXVGAGain_Line))
ui.RFFrequency_Line.editingFinished.connect(lambda: ui.RFFrequency_ScrollBar.setSliderPosition(int(ui.RFFrequency_Line.text())))
ui.PADACOutputBias_Line.editingFinished.connect(lambda: ui.PADACOutputBias_ScrollBar.setSliderPosition(int(ui.PADACOutputBias_Line.text())))
ui.RXVGAGain_Line.editingFinished.connect(lambda: ui.RXVGAGain_ScrollBar.setSliderPosition(int(ui.RXVGAGain_Line.text())))
ui.TXVGAGain_Line.editingFinished.connect(lambda: ui.TXVGAGain_ScrollBar.setSliderPosition(int(ui.TXVGAGain_Line.text())))
########## Button Side
def removeCombobox():
if ( ui.Setups_ComboBox.currentText() == 'Evaluation Defaults' or ui.Setups_ComboBox.currentText() == "LastPowerDown" ):
pass
else:
ui.Setups_ComboBox.removeItem(ui.Setups_ComboBox.currentIndex())
ui.Del_Button.clicked.connect(lambda: removeCombobox())
## ui.SendAll_Button.clicked.connect(lambda: com.main(myCommunication.Package))
########## Line Edit Side
ui.RFFrequency_Line.setText(str( ui.RFFrequency_ScrollBar.value()))
ui.PADACOutputBias_Line.setText(str( ui.PADACOutputBias_ScrollBar.value()))
ui.RXVGAGain_Line.setText(str( ui.RXVGAGain_ScrollBar.value()))
ui.TXVGAGain_Line.setText(str( ui.TXVGAGain_ScrollBar.value()))
ui.RFFrequency_Line.editingFinished.connect(lambda: ui.CheckingLines(ui.RFFrequency_ScrollBar, ui.RFFrequency_Line))
ui.PADACOutputBias_Line.editingFinished.connect(lambda: ui.CheckingLines(ui.PADACOutputBias_ScrollBar, ui.PADACOutputBias_Line))
ui.RXVGAGain_Line.editingFinished.connect(lambda: ui.CheckingLines(ui.RXVGAGain_ScrollBar, ui.RXVGAGain_Line))
ui.TXVGAGain_Line.editingFinished.connect(lambda: ui.CheckingLines(ui.TXVGAGain_ScrollBar, ui.TXVGAGain_Line))
########## Communication Side
ui.RFFrequency_ScrollBar.valueChanged.connect(lambda: self.ValuetoHex(ui))
ui.PADACOutputBias_ScrollBar.valueChanged.connect(lambda: self.ValuetoHex(ui))
ui.RXVGAGain_ScrollBar.valueChanged.connect(lambda: self.ValuetoHex(ui))
ui.TXVGAGain_ScrollBar.valueChanged.connect(lambda: self.ValuetoHex(ui))
print("hello")
x = []
for i in self.newPackage:
x.append(i)
print (i)
print("hello")
for i in x:
print (i)
ui.SendAll_Button.clicked.connect(lambda: com.send(self.newPackage))
print("How are you")
##############
sys.exit(app.exec_())
def ValuetoHex(self,ui):
ValueofRFFrequency = ui.RFFrequency_ScrollBar.value()
ValueofPADACOutputBias = ui.PADACOutputBias_ScrollBar.value()
ValueofRXVGAGain = ui.RXVGAGain_ScrollBar.value()
ValueofTXVGAGain = ui.TXVGAGain_ScrollBar.value()
RF = struct.pack('>h', ValueofRFFrequency)
RF = RF.hex()[:]
PADA = struct.pack('>h', ValueofPADACOutputBias)
PADA = PADA.hex()[:]
RX = struct.pack('>h', ValueofRXVGAGain)
RX = RX.hex()[:]
TX = struct.pack('>h', ValueofTXVGAGain)
TX = TX.hex()[:]
## part_1 = format(value, '#X')[:4]
## part_2 = format(value, '#X')[-2:]
## global newPackage
self.newPackage = [format(starter_byte, 'X'), format(source_byte, 'X')[:2].zfill(2),
format(max2828_byte, 'X')[:2].zfill(2), format(command_byte, 'X'),
RF[:2], RF[-2:], PADA[:2], PADA[-2:], RX[:2], RX[-2:], TX[:2], TX[-2:] ]
for i in self.newPackage:
print (i)
## com.main( s )
## RFValue = ui.RFFrequency_ScrollBar.value()
## RFValue_2 = int(RFValue % 100)
## RFValue_1 = int((RFValue - RFValue_2) / 100)
##
## byte_1 = hex(RFValue_1)
## myCommunication.byte_2 = hex(RFValue_2)
##
## print(RFValue_1.__format__('#04X') + " " + RFValue_2.__format__('#04X'))
## print(str(RFValue_1) + " " + str(RFValue_2))
if __name__ == "__main__":
Edit = WindowEdit()
Edit.mainloop()
还有另一个页面,由 PyQt 创建。如果需要,我可以上传。
没有错误消息或控制台错误。单击发送按钮时只有应用程序崩溃。如果您想对我的代码提出任何建议,请不要犹豫。我是初学者,我想学习。谢谢你。
(这是我的第一条消息,从现在开始谢谢你)
我在代码中发现了我的问题。
foo.hex() 函数返回 "string" 类型。但是,serial.write 函数需要 "int" 类型。这就是我的应用程序崩溃的原因。
而且,我还将 sqlite3 DB 添加到我的应用程序中。我也不再使用 Max2828_Communication 库了。
这是我的最新代码,
https://github.com/mucahitkayadan/Python
我是 Python 的初学者。 我正在尝试为串行通信创建接口。在我的应用程序中,有 4 个变量,每个 2 个字节。我有启动器、命令、源和目标字节。所以,我总共有 4*2+4= 12 个字节的列表。 我正在尝试发送此列表并收回。 没有控制台错误,但是当我尝试发送列表时,它崩溃了。
ui.SendAll_Button.clicked.connect(lambda: com.send(self.newPackage))
如果我发送正常列表,没有功能,它可以工作。但是newPackage在ValuetoHex函数里面,是不行的。 我在这里留下我的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import Max2828_Window as myWindow
import Max2828_WindowEdit as EditModule
import serial
from time import sleep
import struct
con = serial.Serial('COM3', 9600 , timeout = 1)
error_message = [0X65, 0X72, 0X72, 0X6F, 0X72]
starter_byte = 0XAA
source_byte = 0X01
max2828_byte = 0X02
max5866_byte = 0X03
command_byte = 0XBB
byte_1 = 0X00
byte_2 = 0X00
byte_3 = 0X00
byte_4 = 0X00
byte_5 = 0X00
byte_6 = 0X00
byte_7 = 0X00
byte_8 = 0X00
class Communication():
Package = [starter_byte, source_byte, max2828_byte, command_byte,
byte_1, byte_2, byte_3, byte_4,
byte_5, byte_6, byte_7, byte_8]
def send(self, PackageList):
print(str(PackageList))
con.write(PackageList)
def receive(self,PackageList):
our_buffer = []
counter = 1
while counter <= len(PackageList):
msg = con.read()
our_buffer.append(msg)
print ("received " + str("".join( chr(x) for x in msg)))
print (str(our_buffer))
if len(our_buffer) >= len(PackageList):
our_buffer = None
counter = counter + 1
## sleep(1)
def main(self, PackageList):
self.send(PackageList)
self.receive(PackageList)
##com = Communication()
##com.main()
这也是主页:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import Max2828_Window as myWindow
import Max2828_Communication as myCommunication
import serial
from time import sleep
import struct
import os
starter_byte = 0XAA
source_byte = 0X01
max2828_byte = 0X02
max5866_byte = 0X03
command_byte = 0XBB
class WindowEdit(object):
def mainloop(self):
app = QtWidgets.QApplication(sys.argv)
ui = myWindow.Ui_MainWindow()
MainWindow = ui.callMain()
ui.setupUi(MainWindow)
MainWindow.show()
self.ValuetoHex(ui)
##########
com = myCommunication.Communication()
########## Menu Side
ui.actionExit.triggered.connect(ui.close_application)
########## ScrollBar Side
ui.RFFrequency_ScrollBar.valueChanged.connect(lambda: ui.ScrollBarValue(ui.RFFrequency_ScrollBar, ui.RFFrequency_Line))
ui.PADACOutputBias_ScrollBar.valueChanged.connect(lambda: ui.ScrollBarValue(ui.PADACOutputBias_ScrollBar, ui.PADACOutputBias_Line))
ui.RXVGAGain_ScrollBar.valueChanged.connect(lambda: ui.ScrollBarValue(ui.RXVGAGain_ScrollBar, ui.RXVGAGain_Line))
ui.TXVGAGain_ScrollBar.valueChanged.connect(lambda: ui.ScrollBarValue(ui.TXVGAGain_ScrollBar, ui.TXVGAGain_Line))
ui.RFFrequency_Line.editingFinished.connect(lambda: ui.RFFrequency_ScrollBar.setSliderPosition(int(ui.RFFrequency_Line.text())))
ui.PADACOutputBias_Line.editingFinished.connect(lambda: ui.PADACOutputBias_ScrollBar.setSliderPosition(int(ui.PADACOutputBias_Line.text())))
ui.RXVGAGain_Line.editingFinished.connect(lambda: ui.RXVGAGain_ScrollBar.setSliderPosition(int(ui.RXVGAGain_Line.text())))
ui.TXVGAGain_Line.editingFinished.connect(lambda: ui.TXVGAGain_ScrollBar.setSliderPosition(int(ui.TXVGAGain_Line.text())))
########## Button Side
def removeCombobox():
if ( ui.Setups_ComboBox.currentText() == 'Evaluation Defaults' or ui.Setups_ComboBox.currentText() == "LastPowerDown" ):
pass
else:
ui.Setups_ComboBox.removeItem(ui.Setups_ComboBox.currentIndex())
ui.Del_Button.clicked.connect(lambda: removeCombobox())
## ui.SendAll_Button.clicked.connect(lambda: com.main(myCommunication.Package))
########## Line Edit Side
ui.RFFrequency_Line.setText(str( ui.RFFrequency_ScrollBar.value()))
ui.PADACOutputBias_Line.setText(str( ui.PADACOutputBias_ScrollBar.value()))
ui.RXVGAGain_Line.setText(str( ui.RXVGAGain_ScrollBar.value()))
ui.TXVGAGain_Line.setText(str( ui.TXVGAGain_ScrollBar.value()))
ui.RFFrequency_Line.editingFinished.connect(lambda: ui.CheckingLines(ui.RFFrequency_ScrollBar, ui.RFFrequency_Line))
ui.PADACOutputBias_Line.editingFinished.connect(lambda: ui.CheckingLines(ui.PADACOutputBias_ScrollBar, ui.PADACOutputBias_Line))
ui.RXVGAGain_Line.editingFinished.connect(lambda: ui.CheckingLines(ui.RXVGAGain_ScrollBar, ui.RXVGAGain_Line))
ui.TXVGAGain_Line.editingFinished.connect(lambda: ui.CheckingLines(ui.TXVGAGain_ScrollBar, ui.TXVGAGain_Line))
########## Communication Side
ui.RFFrequency_ScrollBar.valueChanged.connect(lambda: self.ValuetoHex(ui))
ui.PADACOutputBias_ScrollBar.valueChanged.connect(lambda: self.ValuetoHex(ui))
ui.RXVGAGain_ScrollBar.valueChanged.connect(lambda: self.ValuetoHex(ui))
ui.TXVGAGain_ScrollBar.valueChanged.connect(lambda: self.ValuetoHex(ui))
print("hello")
x = []
for i in self.newPackage:
x.append(i)
print (i)
print("hello")
for i in x:
print (i)
ui.SendAll_Button.clicked.connect(lambda: com.send(self.newPackage))
print("How are you")
##############
sys.exit(app.exec_())
def ValuetoHex(self,ui):
ValueofRFFrequency = ui.RFFrequency_ScrollBar.value()
ValueofPADACOutputBias = ui.PADACOutputBias_ScrollBar.value()
ValueofRXVGAGain = ui.RXVGAGain_ScrollBar.value()
ValueofTXVGAGain = ui.TXVGAGain_ScrollBar.value()
RF = struct.pack('>h', ValueofRFFrequency)
RF = RF.hex()[:]
PADA = struct.pack('>h', ValueofPADACOutputBias)
PADA = PADA.hex()[:]
RX = struct.pack('>h', ValueofRXVGAGain)
RX = RX.hex()[:]
TX = struct.pack('>h', ValueofTXVGAGain)
TX = TX.hex()[:]
## part_1 = format(value, '#X')[:4]
## part_2 = format(value, '#X')[-2:]
## global newPackage
self.newPackage = [format(starter_byte, 'X'), format(source_byte, 'X')[:2].zfill(2),
format(max2828_byte, 'X')[:2].zfill(2), format(command_byte, 'X'),
RF[:2], RF[-2:], PADA[:2], PADA[-2:], RX[:2], RX[-2:], TX[:2], TX[-2:] ]
for i in self.newPackage:
print (i)
## com.main( s )
## RFValue = ui.RFFrequency_ScrollBar.value()
## RFValue_2 = int(RFValue % 100)
## RFValue_1 = int((RFValue - RFValue_2) / 100)
##
## byte_1 = hex(RFValue_1)
## myCommunication.byte_2 = hex(RFValue_2)
##
## print(RFValue_1.__format__('#04X') + " " + RFValue_2.__format__('#04X'))
## print(str(RFValue_1) + " " + str(RFValue_2))
if __name__ == "__main__":
Edit = WindowEdit()
Edit.mainloop()
还有另一个页面,由 PyQt 创建。如果需要,我可以上传。 没有错误消息或控制台错误。单击发送按钮时只有应用程序崩溃。如果您想对我的代码提出任何建议,请不要犹豫。我是初学者,我想学习。谢谢你。 (这是我的第一条消息,从现在开始谢谢你)
我在代码中发现了我的问题。 foo.hex() 函数返回 "string" 类型。但是,serial.write 函数需要 "int" 类型。这就是我的应用程序崩溃的原因。 而且,我还将 sqlite3 DB 添加到我的应用程序中。我也不再使用 Max2828_Communication 库了。
这是我的最新代码, https://github.com/mucahitkayadan/Python