写一段在ros端进行鼠标点击控制发送串口信息的UI界面代码
时间: 2024-05-04 19:16:44 浏览: 11
以下是一个简单的ROS界面代码,可以通过鼠标点击来控制串口信息的发送:
```python
#!/usr/bin/env python
import rospy
import serial
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QMouseEvent
# Serial Port Configuration
ser = serial.Serial('/dev/ttyACM0', 9600)
class ControlUI(QWidget):
def __init__(self):
super(ControlUI, self).__init__()
# UI Components
self.label = QLabel('Left click to send "Hello!" to serial port')
self.label.mousePressEvent = self.send_serial_data
# Layout
layout = QVBoxLayout()
layout.addWidget(self.label)
# Set layout
self.setLayout(layout)
def send_serial_data(self, event):
if isinstance(event, QMouseEvent) and event.button() == 1:
ser.write(b'Hello!')
if __name__ == '__main__':
# ROS Node Initialization
rospy.init_node('control_ui_node')
# PyQt5 Application Initialization
app = QApplication([])
control_ui = ControlUI()
control_ui.show()
# Start Application
app.exec_()
```
这个代码使用了 PyQt5 作为 UI 框架,当鼠标左键点击 Label 组件时,会向串口发送 "Hello!" 的数据。