一、开发环境搭建
软件:Python3.6+Eric6+PyQt5+CCS3.3
硬件:TI的F2812+仿真器+超声电机+编码器+超声电机驱动
二、功能需求及任务安排
前几届师兄,做DSP SCI串口通讯使用的是MATLAB GUI开发的。如图1所示界面
图1 matlab gui 界面
1、需求改进:a)可操作性:需要装matlab软件才能打开串口通讯界面,之前也将m文件转化成可执行程序exe文件,但是问题很多,慢、根本打不开。
b)保存数据:存取数据的人工操作流程太复杂,先要选择特定的文件存取,然后在运行程序改变文件类型即数据格式,在运行matlab程序画图。
c)界面风格:界面部分设计不合理,文本框较大,且没有应用任何功能,部分按钮、复选框也出现类似情况。
d)功能测试:无串口判断功能以及提示信息,发送数据显示格式问题,没有实时显示日期、时间功能。
2、基于以上需求,采用PyQt5开发类似功能的串口通讯软件要实现一下功能:
a)基本功能:通过串口通讯软件实现对超声电机的控制,启动、停止、施加各种控制率的情况下运动,并作出俯仰、沉浮、控制面转角的控制曲线。
b)辅助功能:以10进制和16进制保存采集的运动数据,直接显示可编辑的运动曲线图,实时显示时间日期,串口数据的设定以及串口的检测、提醒。
3、开发进度计划
a)查找资料安装必要的软件、包、插件,配置开发环境
b)学习PyQt5,开发简单的串口通讯界面
c)开发能与DSP实现通讯的串口软件
d)测试软件功能,完善软件
三、实践开发
1、安装软件:在网上找资料关于用python+Qt开发的并不多,大部分是用c++开发Qt的,不过依然可以借鉴。网上有Anaconda3
2、设计基本的串口通讯界面。创建新的工程,选择自己放置工程的文件夹。创建后__init__文件会自动生成在Project-view的Sources一栏中,然后点击Sourcesl栏旁边的Forms栏,在下方右键后选择new forms。在选择窗口的类型为MainWindow,而不是Dialog。否则无法实现最小化功能。添加空间时要注意使用布局,把同一类型功能放在一起、添加标题。保持对其。其控件的属性基本不用设计。点击保存按钮关掉。在forms栏中多了个xxx.ui文件。
3、程序框架搭建。右键选择xxx.ui文件,然后选择compile form。编译成功后,在sources栏中会出现UI_xxx.py文件。如下是我生成的文件,但这不是初始版本,后期增加了很多槽函数和一些功能。槽函数很简单你可以自己在设计见面时添加,也可生成程序后自己编程添加。可以参考博客:http://blog.youkuaiyun.com/liuyukuan/article/details/50859722
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'E:\study_python\sec_step\serial_gui_A1\SerialGuiA.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
# edited by Rex.Rui
# date : 17.12.20
# versions: V_A_1
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class Ui_Dialog(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Dialog")
MainWindow.setEnabled(True)
MainWindow.resize(900, 500)
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(224, 255, 243))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(224, 255, 243))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(224, 255, 243))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(224, 255, 243))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
MainWindow.setPalette(palette)
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
MainWindow.setFont(font)
MainWindow.setAutoFillBackground(False)
# MainWindow.setSizeGripEnabled(False)
self.groupBox_2 = QtWidgets.QGroupBox(MainWindow)
self.groupBox_2.setGeometry(QtCore.QRect(180, 110, 141, 121))
self.groupBox_2.setAutoFillBackground(True)
self.groupBox_2.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox_2.setFlat(False)
self.groupBox_2.setCheckable(False)
self.groupBox_2.setObjectName("groupBox_2")
self.groupBox_5 = QtWidgets.QGroupBox(MainWindow)
self.groupBox_5.setGeometry(QtCore.QRect(30, 430, 281, 61))
self.groupBox_5.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox_5.setObjectName("groupBox_5")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox_2)
self.pushButton_2.setGeometry(QtCore.QRect(20, 20, 111, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.checkBox = QtWidgets.QCheckBox(self.groupBox_2)
self.checkBox.setGeometry(QtCore.QRect(30, 50, 101, 31))
self.checkBox.setObjectName("checkBox")
self.checkBox_2 = QtWidgets.QCheckBox(self.groupBox_2)
self.checkBox_2.setGeometry(QtCore.QRect(30, 80, 71, 16))
self.checkBox_2.setObjectName("checkBox_2")
self.groupBox = QtWidgets.QGroupBox(MainWindow)
self.groupBox.setGeometry(QtCore.QRect(30, 10, 151, 221))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(215, 255, 169))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Dark, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(215, 255, 169))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Dark, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush)
brush = QtGui.QBrush(QtGui.QColor(215, 255, 169))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(215, 255, 169))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Dark, brush)
brush = QtGui.QBrush(QtGui.QColor(215, 255, 169))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(215, 255, 169))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush)
self.groupBox.setPalette(palette)
self.groupBox.setLayoutDirection(QtCore.Qt.LeftToRight)
self.groupBox.setAutoFillBackground(True)
sel