def rings(self): self.serial_t.write(bytes.fromhex('77 68 06 00 02 3D 03 02 0A'))
时间: 2024-04-22 13:25:01 浏览: 21
这段代码定义了一个名为`rings`的方法,用于发送一个特定的字节序列给智能汽车。
该方法通过串口对象`self.serial_t`将一个十六进制字节序列发送给智能汽车。具体的字节序列为`77 68 06 00 02 3D 03 02 0A`。
这段代码可能是用于向智能汽车发送某种指令或控制信号。如果您需要更多上下文或相关代码来理解该方法的具体作用,请提供更多信息。
相关问题
class SerialThread(threading.Thread): def __init__(self, serial_port, parent=None): super().__init__(parent) self.serial_port = serial_port self.running = True def run(self): while self.running: if self.serial_port.in_waiting: # 判断是否有数据可读 data = self.serial_port.read_all() # 读取所有可读数据 hex_data = binascii.b2a_hex(data).decode() # 将数据转换为十六进制字符串 hex_list = hex_data.split(' ') # 按空格拆分字符串并保存到列表中 my_string = hex_list[0] statue = my_string[14:16] if statue == '02': self.label.setPixmap(QPixmap("usart/image/cyan.png")) print(statue) elif statue == '03': # self.curren_image = "usart/image/white.png" self.label.setPixmap(QPixmap("usart/image/white.png")) print(statue) elif statue == '05': self.label.setPixmap(QPixmap("usart/image/yellow.png")) print(statue) else: self.label.setPixmap(QPixmap("usart/image/nor.png")) print(statue)在本段代码中出现了以下错误:self.label.setPixmap(QPixmap("usart/image/nor.png")) AttributeError: 'SerialThread' object has no attribute 'label'
根据你提供的代码,SerialThread类中确实没有名为“label”的属性,因此在SerialThread对象中调用“self.label”将会出现AttributeError错误。
为了解决这个问题,你需要在SerialThread类中添加一个名为“label”的属性,以便在run()方法中访问它。你可以在SerialThread类的__init__()方法中添加以下代码来创建一个QLabel对象并将其赋值给“label”属性:
```
from PyQt5.QtWidgets import QLabel
class SerialThread(threading.Thread):
def __init__(self, serial_port, parent=None):
super().__init__(parent)
self.serial_port = serial_port
self.running = True
self.label = QLabel() # 创建一个QLabel对象并赋值给label属性
```
这样,在run()方法中就可以使用“self.label”访问该QLabel对象,并调用setPixmap()方法来设置标签的图像。
注意,由于这个标签对象是在SerialThread类中创建的,因此需要将其传递给主窗口或其他需要使用该标签的对象。你可以将该标签对象作为SerialThread对象的参数传递给主窗口或其他对象,或者使用信号和槽机制来在不同对象之间传递该标签对象。
self.main_ui.pushButton_2.clicked.connect(self.thing1) 没反应是为什么 class Worker(QtCore.QThread): sinOut = pyqtSignal(str) def __init__(self, parent=None): super(Worker, self).__init__(parent) # 设置工作状态与初始num数值 self.working = True self.num = 0 #def __del__(self): # 线程状态改变与线程终止 #self.working = False #self.wait() def stop(self): #线程状态改变与线程终止 self.working = False self.wait() def run(self): self.working = True while self.working == True: #file_str = 'File index{0}'.format(self.num) self.num += 1 # 发射信号 #self.sinOut.emit(file_str) self.sinOut.emit('1') # 线程休眠2秒 self.msleep(5) class parentWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.main_ui = JQ.Ui_MainWindow() self.main_ui.setupUi(self) self.thread1 = Worker() self.main_ui.pushButton_2.clicked.connect(self.thing1) self.thread1.sinOut.connect(self.printt) def thing1(self): #self.main_ui.pushButton.setEnabled(False) print('9999999999') self.thread1.start() print('123') #self.sleep(2) def printt(self): print('7777') def ok(): print('ok') # def hourstest(): # thread1 = Worker() # thread1.start() # com = JQ.Ui_MainWindow().comboBox_2.currentText() # ser = serial.Serial('com3', 1200, timeout=1) # data = bytes.fromhex( # '68 01 00 20 00 00 00 00 00 34 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 F4 16 ') # ser.write(data) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) widget = QtWidgets.QMainWindow() widget = parentWindow() ui = JQ.Ui_MainWindow() # 这是原py中的类,因人而异哦 ui.setupUi(widget) ui.discoverSerial() #串口发现 #ui.pushButton_2.clicked.connect(widget.thing1) widget.show() sys.exit(app.exec_())
首先,你的连接语句应该是正确的。但是,你的槽函数`thing1`中启动了一个线程并且没有等待该线程完成。因此,当你再次点击`pushButton_2`时,会创建一个新的线程,导致出现问题。
你需要在`thing1`函数中添加`self.thread1.wait()`语句来等待线程完成。这样,当你再次点击`pushButton_2`时,它会等待线程完成后再启动一个新的线程。
另外,你在`ok`函数中定义了一个serial对象,但是没有使用它。如果你想使用该对象,你需要在该函数中添加相关代码。
以下是修改后的代码:
```
class parentWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main_ui = JQ.Ui_MainWindow()
self.main_ui.setupUi(self)
self.thread1 = Worker()
self.thread1.sinOut.connect(self.printt)
self.main_ui.pushButton_2.clicked.connect(self.thing1)
def thing1(self):
print('9999999999')
self.thread1.start()
self.thread1.wait() # 等待线程完成
print('123')
def printt(self):
print('7777')
def ok():
print('ok')
com = JQ.Ui_MainWindow().comboBox_2.currentText()
ser = serial.Serial('com3', 1200, timeout=1)
data = bytes.fromhex('68 01 00 20 00 00 00 00 00 34 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 F4 16 ')
ser.write(data)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)