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+"停止";
}
}
}
}
运行效果: