Python3实现三菱PLC串口通讯(附源码和运行图)

基于PyQt5通过串口通信控制三菱PLC

废话不多说,直接上源码

python 复制代码
"""
# -*- coding:utf-8 -*-
@Project : Mitsubishi
@File : Main_Run.pyw
@Author : Administrator
@Time : 2024/05/09 下午 04:10
@Description : PyQt5界面主逻辑
@Software:PyCharm
"""
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from MainUI import Ui_MainWindow
import qtawesome as qta
import serial.tools.list_ports
import Mitsubishi_API


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(QMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.label_onOff.setHidden(True)
        self.comboBox_onOff.setHidden(True)
        self.comboBox_mode.currentIndexChanged.connect(self.changeMode)
        self.refreshSerialPorts()
        self.pushButton_refresh.clicked.connect(self.refreshSerialPorts)
        self.pushButton_execute.clicked.connect(self.execute)
        self.pushButton_refresh.setIcon(qta.icon("ei.refresh"))
        self.pushButton_execute.setIcon(qta.icon("ei.hand-up"))

    def changeMode(self):
        """
        切换模式
        :return:
        """
        if self.comboBox_mode.currentText() == "读取":
            self.label_number.setHidden(False)
            self.comboBox_number.setHidden(False)
            self.label_onOff.setHidden(True)
            self.comboBox_onOff.setHidden(True)
        else:
            self.label_number.setHidden(True)
            self.comboBox_number.setHidden(True)
            self.label_onOff.setHidden(False)
            self.comboBox_onOff.setHidden(False)

    def refreshSerialPorts(self):
        """
        绑定串口列表至控件
        :return:
        """
        # 清除当前下拉列表中的项目
        self.comboBox_serialPort.clear()

        # 获取可用串口列表
        ports = serial.tools.list_ports.comports()

        # 将串口名称添加到下拉列表中
        for port in ports:
            self.comboBox_serialPort.addItem(port.device)

    def execute(self):
        """
        执行操作
        :return:
        """
        serial_port = self.comboBox_serialPort.currentText()  # 串口
        mode = self.comboBox_mode.currentText()  # 模式
        soft = self.comboBox_soft.currentText()  # 输入输出
        soft_num = self.lineEdit_soft_num.text()  # 软元件数值
        if soft_num:
            softComponent = soft + soft_num.strip()
            if mode == "读取":
                read_num = self.comboBox_number.currentText()
                recv = Mitsubishi_API.PLC_Read(serial_port, softComponent, read_num)
                self.textBrowser.append("<span style='color:#00FFA9'>读取PLC返回值:{}</span>".format(recv))
            else:
                onOff = self.comboBox_onOff.currentText()
                Mitsubishi_API.PLC_FORCE(serial_port, softComponent, onOff)
                self.textBrowser.append("<span style='color:#4848E9'>写入PLC命令完毕!</span>")
        else:
            QMessageBox.warning(self, '警告', '请填写完整软元件!', QMessageBox.Yes | QMessageBox.No,
                                QMessageBox.Yes)


if __name__ == '__main__':
    QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

这里直通资源下载点这里下载源码以及打包exe资源




相关推荐
抠头专注python环境配置9 分钟前
基于Python与深度学习的智能垃圾分类系统设计与实现
pytorch·python·深度学习·分类·垃圾分类·vgg·densenet
愈努力俞幸运26 分钟前
flask 入门 token, headers,cookie
后端·python·flask
梦想是成为算法高手39 分钟前
带你从入门到精通——知识图谱(一. 知识图谱入门)
人工智能·pytorch·python·深度学习·神经网络·知识图谱
用什么都重名40 分钟前
Conda 虚拟环境安装配置路径详解
windows·python·conda
阿也在北京42 分钟前
基于Neo4j和TuGraph的知识图谱与问答系统搭建——胡歌的导演演员人际圈
python·阿里云·知识图谱·neo4j
计算机徐师兄44 分钟前
Python基于知识图谱的胆囊炎医疗问答系统(附源码,文档说明)
python·知识图谱·胆囊炎医疗问答系统·python胆囊炎医疗问答系统·知识图谱的胆囊炎医疗问答系统·python知识图谱·医疗问答系统
北冥码鲲44 分钟前
【保姆级教程】从零入手:Python + Neo4j 构建你的第一个知识图谱
python·知识图谱·neo4j
B站计算机毕业设计超人1 小时前
计算机毕业设计Python+大模型音乐推荐系统 音乐数据分析 音乐可视化 音乐爬虫 知识图谱 大数据毕业设计
人工智能·hadoop·爬虫·python·数据分析·知识图谱·课程设计
喵手1 小时前
Python爬虫零基础入门【第三章:Requests 静态爬取入门·第5节】限速与礼貌爬取:并发、延迟、频率控制!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·requests静态爬取·限速与爬取