我是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创建。如果有必要,我可以上传它。 没有错误消息或控制台错误。单击发送按钮时,只有应用程序崩溃。我想对我的代码提出任何建议,请不要犹豫。我是初学者,我想学习。谢谢。 (这是我的第一条消息,从现在开始谢谢您)
答案 0 :(得分:0)
我在代码中发现了我的问题。 foo.hex()函数返回“字符串”类型。但是,serial.write函数需要“ int”类型。这就是为什么我的应用程序崩溃的原因。 而且,我还将sqlite3 DB添加到我的应用程序中。另外,我不再使用Max2828_Communication lib。
这是我最新的代码, https://github.com/mucahitkayadan/Python