当前位置: 首页 > article >正文

c# 和python封装起保停

1.python对电机类封装


class Motor:
    def __init__(self, id:int):
        '''
        :param id: 设备编号
        '''
        self.id = id
        self._start = False
        self._stop = True
        self._out = False

    @property
    def start(self):
        return self._start

    @start.setter
    def start(self, value):
        self._start = value
        self.update_output()

    @property
    def stop(self):
        return self._stop

    @stop.setter
    def stop(self, value):
        self._stop = value
        self.update_output()

    @property
    def out(self):
        return self._out

    def update_output(self):
        self._out = (self._out | self._start) & self._stop

运行截图:

测试代码:c#


class Form(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent,geometry=QRect(350,30,680,600))
        self.init_ui()

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.on_timer_trick)
        self.timer.start(100)

        self.motor = Motor(3)
        self.plc = client.Client()
        self.plc.set_connection_type(3)
        self.plc.connect("192.168.5.20",0,1)
        self.bytes = bytearray(1)
        self.start = False
        self.stop = True

    def init_ui(self):
        self.btn_start1 = QPushButton("启动按钮1",self,geometry=QRect(50,30,80,30))
        self.btn_start2 = QPushButton("启动按钮2",self,geometry=QRect(50,80,80,30))
        self.btn_start3 = QPushButton("点动",self,geometry=QRect(50,280,80,30))
        self.lab1 = QLabel("这是什么",self,geometry=QRect(50,130,120,30))

        self.btn_start1.pressed.connect(self.btn_start1_press)
        self.btn_start1.released.connect(self.btn_start1_release)

        self.btn_start2.pressed.connect(self.btn_start2_press)
        self.btn_start2.released.connect(self.btn_start2_release)


    def btn_start1_press(self):
        self.start=True
    def btn_start1_release(self):
        self.start = False

    def btn_start2_press(self):
        self.stop = False

    def btn_start2_release(self):
        self.stop = True

    def on_timer_trick(self):
        self.motor.start=self.start
        self.motor.stop = self.stop

        util.set_bool(self.bytes,0,self.motor.id,self.motor.out)
        self.plc.db_write(1,0,self.bytes)
        if self.motor.out:
            self.lab1.setStyleSheet("background-color:green")
            self.lab1.setText("电机已经启动")
        else:
            self.lab1.setStyleSheet("background-color:red")
            self.lab1.setText("电机已经停止")


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win = Form()
    win.show()
    sys.exit(app.exec())

c# 对电机类封装

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication11
{
    class MontorControl
    {
        public UInt16 ID; // 设备编号
        private bool _start;
        private bool _stop;
        private bool _out;
        public bool Start { get { return _start; } set { _start = value; fun(); } }
        public bool Stop { get { return _stop; } set { _stop = value;fun(); } }
        public bool Out { get { return _out; } } 
        public void fun(){
            _out = (_start || _out) && _stop;

            }
    }    
}

测试代码,使用Sharp7 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sharp7;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        private bool start = false;
        private bool stop = true;
        MontorControl motor;
        Sharp7.S7Client plc = new Sharp7.S7Client();
        byte[] data = new byte[1];

        public Form1()
        {
            InitializeComponent();
            //设置类型
            plc.SetConnectionType(3);
            int a =plc.ConnectTo("192.168.5.20", 0, 1);

            motor = new MontorControl() { ID=3};
            timer1.Start();

           
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            start = true;
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            start = false;
        }

        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            stop = false;
        }

        private void button2_MouseUp(object sender, MouseEventArgs e)
        {
            stop = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            motor.Start = start;
            motor.Stop = stop;

            Sharp7.S7.SetBitAt(data, 0, motor.ID, motor.Out); // 对位设置
            plc.DBWrite(1, 0, 1, data);  //写入到plc


            if (motor.Out)
            {
                label1.BackColor = Color.Green;
                label1.Text = "电机启动";
            }else
            {
                label1.BackColor = Color.Blue;
                label1.Text = motor.ID+"停止";
            }
        }
    }
}

运行效果:


http://www.kler.cn/a/501739.html

相关文章:

  • 嵌入式系统中的 OpenCV 与 OpenGLES 协同应用
  • python检测gitlab中某个标签在一个月内添加和移除了多少次
  • poi处理多选框进行勾选操作下载word以及多word文件压缩
  • IntelliJ IDEA 主题插件
  • Blazor中Syncfusion Word组件使用方法
  • 理解Spark中运行程序时数据被分区的过程
  • 功能篇:mybatis中实现缓存
  • JSON头文件调用
  • Fastapi0.115.6之Tortoise ORM0.23.0基本增删改查大全【亲测可用,仅供参考】
  • AIDD-人工智能药物设计-通过组合生物合成产生新的类似物的抗真菌费尔南型三萜多聚类素的生物合成表征
  • AI多模态论文解读:LLaVA-CoT:让视觉语言模型逐步推理
  • uni-app持久化登录简单实现
  • 八、系统托盘与配置面板
  • springmvc前端传参,后端接收
  • StarRocks Awards 2024 年度贡献人物
  • 《解锁鸿蒙系统AI能力,开启智能应用开发新时代》
  • 【GoLand】无法debug 无法运行
  • 【Orca】Orca - Graphlet 和 Orbit 计数算法
  • 《解锁计算机视觉智慧:编程实现图片场景文字描述的开源宝藏》
  • mysql-行溢出处理原理
  • 【HTML+CSS+JS+VUE】web前端教程-21-字体属性
  • 智能化文档开发(DI)
  • el-table 合并单元格
  • Redis解决热key问题
  • sql server cdc漏扫数据
  • C# 虚方法和抽象方法的区别,重写和重载的区别,参数修饰符(ref、out、in、params)--09