通过pyserial库连接云台,对云台进行方向控制
效果图如下(使用Qt designer软件进行界面绘图):
逻辑控制代码如下:
from Ui_ptzui import *
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
import serial
class MyMainForm(QMainWindow,Ui_Form):
def __init__(self,parent=None):
super(MyMainForm,self).__init__(parent)
self.setupUi(self)
self.up = 'FF 01 00 08 00 27 30'
self.down = 'FF 01 00 10 00 27 38'
self.right = 'FF 01 00 02 27 00 2A'
self.left = 'FF 01 00 04 27 00 2C'
self.lefup = 'FF 01 00 0C 27 27 5B'
self.rightup = 'FF 01 00 0A 27 27 59'
self.leftdown = 'FF 01 00 14 27 27 63'
self.rightdown = 'FF 01 00 12 27 27 61'
self.stop = 'FF 01 00 00 00 00 01'
self.yuzhiwei1 = 'FF 01 00 07 00 01 09'
self.yuzhiwei2 = 'FF 01 00 07 00 02 0A'
self.yuzhiwei3 = 'FF 01 00 07 00 03 0B'
self.ser = serial.Serial()
self.pushButton_duankai.setEnabled(False)
#打开关闭串口
self.pushButton_start.clicked.connect(self.OpenCom)
self.pushButton_duankai.clicked.connect(self.CloseCom)
#上
self.pushButton_up.pressed.connect(self.Up)
self.pushButton_up.released.connect(self.Stop)
#下
self.pushButton_down.pressed.connect(self.Down)
self.pushButton_down.released.connect(self.Stop)
#右
self.pushButton_right.pressed.connect(self.Right)
self.pushButton_right.released.connect(self.Stop)
#左
self.pushButton_left.pressed.connect(self.Left)
self.pushButton_left.released.connect(self.Stop)
#左上
self.pushButton_leftup.pressed.connect(self.LeftUp)
self.pushButton_leftup.released.connect(self.Stop)
#右上
self.pushButton_rightup.pressed.connect(self.RightUp)
self.pushButton_rightup.released.connect(self.Stop)
#左下
self.pushButton_leftdown.pressed.connect(self.LeftDown)
self.pushButton_leftdown.released.connect(self.Stop)
#右下
self.pushButton_rightdown.pressed.connect(self.RightDown)
self.pushButton_rightdown.released.connect(self.Stop)
#调用预置位1
self.pushButton_preset1.clicked.connect(self.Yuzhiwei1)
#调用预置位2
self.pushButton_preset2.clicked.connect(self.Yuzhiwei2)
#调用预置位3
self.pushButton_preset3.clicked.connect(self.Yuzhiwei3)
def OpenCom(self):
"打开串口"
self.ser.port=self.lineEdit_comport.text()
self.ser.baudrate=int(self.lineEdit_baudrate.text())
self.ser.bytesize=int(self.lineEdit_bytesize.text())
self.ser.stopbits=int(self.lineEdit_stopbits.text())
self.ser.setDTR = 1
self.ser.open()
if (self.ser.isOpen()):
self.pushButton_start.setEnabled(False)
self.pushButton_duankai.setEnabled(True)
def CloseCom(self):
"关闭串口"
self.ser.close()
if(self.ser.isOpen()==False):
self.pushButton_start.setEnabled(True)
self.pushButton_duankai.setEnabled(False)
def Stop(self):
"停止"
self.ser.write(bytes.fromhex(self.stop))
def Up(self):
"上"
self.ser.write(bytes.fromhex(self.up))
def Down(self):
"下"
self.ser.write(bytes.fromhex(self.down))
def Right(self):
"右"
self.ser.write(bytes.fromhex(self.right))
def Left(self):
"左"
self.ser.write(bytes.fromhex(self.left))
def LeftUp(self):
"左上"
self.ser.write(bytes.fromhex(self.lefup))
def RightUp(self):
"右上"
self.ser.write(bytes.fromhex(self.rightup))
def LeftDown(self):
"左下"
self.ser.write(bytes.fromhex(self.leftdown))
def RightDown(self):
"右下"
self.ser.write(bytes.fromhex(self.rightdown))
def Yuzhiwei1(self):
"预置位1"
self.ser.write(bytes.fromhex(self.yuzhiwei1))
def Yuzhiwei2(self):
"预置位2"
self.ser.write(bytes.fromhex(self.yuzhiwei2))
def Yuzhiwei3(self):
"预置位3"
self.ser.write(bytes.fromhex(self.yuzhiwei3))
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())