Files
kou_zhaoxielou_shandong/口罩泄露定制款/Data/Function.cs
2026-01-16 20:53:33 +08:00

103 lines
3.3 KiB
C#

using Modbus.Device;
using Sunny.UI;
using System;
using System.Threading;
using System.Windows.Forms;
namespace
{
public class Function
{
ModbusMaster master;
DataChange dc = new DataChange();
public enum ButtonType
{
,
,
,
}
public enum DataType
{
,
}
public Function(ModbusMaster master_in)
{
this.master = master_in;
}
public void BtnClickFunction(ButtonType buttonType, ushort address)
{
try
{
switch (buttonType)
{
case ButtonType.:
master?.WriteSingleCoil(1, address, true);
Thread.Sleep(100);
master?.WriteSingleCoil(1, address, false);
Thread.Sleep(100);
break;
case ButtonType.:
if (master.ReadCoils(1, address, 1)[0])
{
master?.WriteSingleCoil(1, address, false);Thread.Sleep(100); }
else
{ master?.WriteSingleCoil(1, address, true); Thread.Sleep(100); }
break;
case ButtonType.:
master?.WriteSingleCoil(1, address, true);
Thread.Sleep(100);
break;
case ButtonType.:
master?.WriteSingleCoil(1, address, false);
Thread.Sleep(100);
break;
default:
break;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void WriteToPLC(string inPutValue, ushort address, DataType dataType)
{
try
{
switch (dataType)
{
case DataType.:
double value = inPutValue.ToDouble();
if (UIInputDialog.ShowInputDoubleDialog(ref value, UIStyle.Inherited, desc: "请输入值", showMask: false))
{
master.WriteMultipleRegisters(1, address, dc.SplitFloatToUShortArray((float)value));
}
break;
case DataType.:
int value_int = inPutValue.ToInt();
if (UIInputDialog.ShowInputIntegerDialog(ref value_int, UIStyle.Inherited, desc: "请输入数据:"))
{
master.WriteMultipleRegisters(1, address, dc.intToushorts(value_int));
}
break;
default:
break;
}
}
catch (Exception ex)
{
MessageBox.Show("操作失败!" + "\n" + "\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}