TFLH/Model/HslModel.cs
2026-08-06 09:50:46 +08:00

825 lines
22 KiB
C#

using Community;
using HslCommunication;
using HslCommunication.Profinet.Melsec;
using HslCommunication.Profinet.Panasonic;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
/// <summary>
/// PLC通讯模型
/// </summary>
public class HslModel : NotificationObject
{
public int ID { get; set; }
#region 名称
private string nameStr;
/// <summary>
/// 名称
/// </summary>
public string NameStr
{
get { return nameStr; }
set
{
nameStr = value;
RaisePropertyChanged("NameStr");
}
}
#endregion
#region IP地址
private string ip;
/// <summary>
/// IP地址
/// </summary>
public string IP
{
get { return ip; }
set
{
ip = value;
RaisePropertyChanged("IP");
}
}
#endregion
#region 端口号
private int port;
/// <summary>
/// 端口号
/// </summary>
public int Port
{
get { return port; }
set
{
port = value;
RaisePropertyChanged("Port");
}
}
#endregion
#region 品牌
private int brand;
/// <summary>
/// 品牌
/// </summary>
public int Brand
{
get { return brand; }
set
{
brand = value;
RaisePropertyChanged("Brand");
}
}
#endregion
#region 连接方式
private int connectionType;
/// <summary>
/// 连接方式 1:TCP/IP 2:串口
/// </summary>
public int ConnectionType
{
get { return connectionType; }
set
{
connectionType = value;
RaisePropertyChanged("ConnectionType");
}
}
#endregion
#region 串口号
private string portName;
/// <summary>
/// 串口号
/// </summary>
public string PortName
{
get { return portName; }
set
{
portName = value;
this.RaisePropertyChanged("PortName");
}
}
#endregion
#region 波特率
private int baudRate;
/// <summary>
/// 波特率
/// </summary>
public int BaudRate
{
get { return baudRate; }
set
{
baudRate = value;
this.RaisePropertyChanged("BaudRate");
}
}
#endregion
#region 校验位
private int parityNumber;
/// <summary>
/// 校验位
/// </summary>
public int ParityNumber
{
get { return parityNumber; }
set
{
parityNumber = value;
this.RaisePropertyChanged("ParityNumber");
}
}
#endregion
#region 数据位
private int dataBits;
/// <summary>
/// 数据位
/// </summary>
public int DataBits
{
get { return dataBits; }
set
{
dataBits = value;
this.RaisePropertyChanged("DataBits");
}
}
#endregion
#region 停止位
private int stopBitsNumber;
/// <summary>
/// 停止位
/// </summary>
public int StopBitsNumber
{
get { return stopBitsNumber; }
set
{
stopBitsNumber = value;
this.RaisePropertyChanged("StopBitsNumber");
}
}
#endregion
private string notes;
/// <summary>
/// 备注
/// </summary>
public string Notes
{
get { return notes; }
set
{
notes = value;
RaisePropertyChanged("Notes");
}
}
/// <summary>
/// PLC网口连接对象
/// </summary>
//public MelsecMcNet Melsec_Net { get; set; }
public PanasonicMewtocolOverTcp Melsec_Net { get; set; }
/// <summary>
/// PLC串口连接对象
/// </summary>
public MelsecFxSerial Melsec_Serial { get; set; }
private ShowInfo plcStatus;
/// <summary>
/// PLC连接状态
/// </summary>
public ShowInfo PlcStatus
{
get { return plcStatus; }
set
{
plcStatus = value;
RaisePropertyChanged("PlcStatus");
}
}
private ShowInfo plcRunStatus;
/// <summary>
/// PLC运行状态
/// </summary>
public ShowInfo PlcRunStatus
{
get { return plcRunStatus; }
set
{
plcRunStatus = value;
RaisePropertyChanged("PlcRunStatus");
}
}
/// <summary>
/// 构造函数
/// </summary>
public HslModel()
{
PlcStatus = new ShowInfo() { StatuInfo = "等待连接" };
PlcStatus.StatusBackground = ColorStr.Yellow;
PlcRunStatus = new ShowInfo() { StatuInfo = "未知" };
// softAuthorize = new HslCommunication.BasicFramework.SoftAuthorize();
string str = "e3e27344-1a9f-4d8b-b4fc-da5cae4b99bf"; //"24742e53-a574-43e3-bde9-c835a89690c6"; //激活码
if (!HslCommunication.Authorization.SetAuthorizationCode(str))
{
Console.WriteLine("Authorization failed! The current program can only be used for 8 hours!");
//return; // 激活失败应该退出系统
}
}
// private HslCommunication.BasicFramework.SoftAuthorize softAuthorize = null;
#region 连接PLC
/// <summary>
/// PLC连接状态
/// </summary>
public bool ConnectionState { get; set; }
/// <summary>
/// 连接PLC
/// </summary>
/// <returns></returns>
public bool Conneced()
{
try
{
if (ConnectionType == 1)
{
switch (Brand)
{
case 1:
//Melsec_Net = new MelsecMcNet(IP, Port);
break;
case 2:
Melsec_Net =new PanasonicMewtocolOverTcp(IP, Port,0xEE);//new PanasonicMcNet(IP, Port);
break;
}
PlcStatus.StatuInfo = "NotConnected";
Melsec_Net.ConnectTimeOut = 2000;
//melsec_net.NetworkNumber = 0x00;
//melsec_net.NetworkStationNumber = 0x00;
OperateResult connected_Et = Melsec_Net.ConnectServer();
if (connected_Et.IsSuccess)
{
Melsec_Net.SetPersistentConnection(); //设置一个长连接 掉线自动连
PlcStatus.StatuInfo = "连接成功";
PlcStatus.StatusBackground = ColorStr.Green;
ConnectionState = true;
return true;
}
else
{
PlcStatus.StatuInfo = "连接失败" + connected_Et.Message;
PlcStatus.StatusBackground = ColorStr.Red;
ConnectionState = false;
return false;
}
}
else
{
Melsec_Serial?.Close();
Melsec_Serial = new MelsecFxSerial();
Melsec_Serial.IsNewVersion = true;
Melsec_Serial.SerialPortInni(sp =>
{
sp.PortName = PortName;
sp.BaudRate = BaudRate;
sp.DataBits = DataBits;
sp.StopBits = (StopBits)StopBitsNumber;//StopBits.One;//设置当前停止位
sp.Parity = (Parity)ParityNumber; //Parity.None;//设置当前校验位
});
OperateResult connected_Et = Melsec_Serial.Open();
if (connected_Et.IsSuccess)
{
PlcStatus.StatuInfo = "连接成功";
PlcStatus.StatusBackground = ColorStr.Green;
ConnectionState = true;
return true;
}
else
{
PlcStatus.StatuInfo = "连接失败" + connected_Et.Message;
PlcStatus.StatusBackground = ColorStr.Red;
ConnectionState = false;
return false;
}
}
}
catch
{
PlcStatus.StatuInfo = "连接异常";
PlcStatus.StatusBackground = ColorStr.Red;
return false;
}
}
#endregion
#region 断开连接
/// <summary>
/// 断开连接
/// </summary>
public void Disconnect()
{
if (Melsec_Net != null && ConnectionType == 1)
{
Melsec_Net.ConnectClose();
Melsec_Net.Dispose();
Melsec_Net = null;
PlcStatus.StatuInfo = "断开成功";
PlcStatus.StatusBackground = ColorStr.Red;
}
else if (Melsec_Serial != null && ConnectionType == 2)
{
Melsec_Serial.Close();
Melsec_Serial.Dispose();
PlcStatus.StatuInfo = "断开成功";
PlcStatus.StatusBackground = ColorStr.Red;
}
}
#endregion
#region 单数读取
/// <summary>
/// 读取字符串
/// </summary>
/// <param name="address">读取地址</param>
/// <param name="Strlength">读取字符串长度</param>
/// <returns></returns>
public string ReadString(string address, ushort Strlength)
{
OperateResult<string> res = ConnectionType == 1 ? Melsec_Net.ReadString(address, Strlength) : Melsec_Serial.ReadString(address, Strlength);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return "";
}
}
/// <summary>
/// 读取int16
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public short ReadShort(string address)
{
OperateResult<short> res = ConnectionType == 1 ? Melsec_Net.ReadInt16(address) : Melsec_Serial.ReadInt16(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return 0;
}
}
/// <summary>
/// 读取Int32
/// </summary>
/// <param name="address">读取地址</param>
/// <returns></returns>
public int ReadInt32(string address)
{
OperateResult<int> res = ConnectionType == 1 ? Melsec_Net.ReadInt32(address) : Melsec_Serial.ReadInt32(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return 0;
}
}
/// <summary>
/// 读取Long
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public long ReadLong(string address)
{
OperateResult<long> res = ConnectionType == 1 ? Melsec_Net.ReadInt64(address) : Melsec_Serial.ReadInt64(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return 0;
}
}
/// <summary>
/// 读取Bool
/// </summary>
/// <param name="address">读取地址</param>
/// <returns></returns>
public bool ReadBool(string address)
{
OperateResult<bool> res = ConnectionType == 1 ? Melsec_Net.ReadBool(address) : Melsec_Serial.ReadBool(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return false;
}
}
/// <summary>
/// 读取flost(单精度浮点数)
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public float ReadFloat(string address)
{
OperateResult<float> res = ConnectionType == 1 ? Melsec_Net.ReadFloat(address) : Melsec_Serial.ReadFloat(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return 0;
}
}
/// <summary>
/// 读取ushort
/// </summary>
/// <param name="address">读取地址</param>
/// <returns></returns>
public ushort ReadUint16(string address)
{
OperateResult<ushort> res = ConnectionType == 1 ? Melsec_Net.ReadUInt16(address) : Melsec_Serial.ReadUInt16(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return 0;
}
}
/// <summary>
/// 读取uint
/// </summary>
/// <param name="address">读取地址</param>
/// <returns></returns>
public uint ReadUint(string address)
{
OperateResult<UInt32> res = ConnectionType == 1 ? Melsec_Net.ReadUInt32(address) : Melsec_Serial.ReadUInt32(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return 0;
}
}
/// <summary>
/// 读取Ulong
/// </summary>
/// <returns></returns>
public ulong ReadUlong(string address)
{
OperateResult<ulong> res = ConnectionType == 1 ? Melsec_Net.ReadUInt64(address) : Melsec_Serial.ReadUInt64(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return 0;
}
}
/// <summary>
/// 读取Double
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public double ReadDouble(string address)
{
OperateResult<double> res = ConnectionType == 1 ? Melsec_Net.ReadDouble(address) : Melsec_Serial.ReadDouble(address);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return 0;
}
}
/// <summary>
/// 读取字节
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
public byte[] ReadByte(string address, ushort length)
{
OperateResult<byte[]> res = ConnectionType == 1 ? Melsec_Net.Read(address, length) : Melsec_Serial.Read(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new byte[0];
}
}
#endregion
#region 批量读取
/// <summary>
/// 读取int16
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
public short[] ReadShort(string address, ushort length)
{
OperateResult<short[]> res = ConnectionType == 1 ? Melsec_Net.ReadInt16(address, length) : Melsec_Serial.ReadInt16(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new short[0];
}
}
/// <summary>
/// 批量读取Int
/// </summary>
/// <param name="address">读取地址</param>
/// <param name="length"></param>
/// <returns></returns>
public int[] ReadInt32(string address, ushort length)
{
OperateResult<int[]> res = ConnectionType == 1 ? Melsec_Net.ReadInt32(address, length) : Melsec_Serial.ReadInt32(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new int[0];
}
}
/// <summary>
/// 读取Long
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
public long[] ReadLong(string address, ushort length)
{
OperateResult<long[]> res = ConnectionType == 1 ? Melsec_Net.ReadInt64(address, length) : Melsec_Serial.ReadInt64(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new long[0];
}
}
/// <summary>
/// 批量读取Bool
/// </summary>
/// <param name="address">读取地址</param>
/// <param name="length"></param>
/// <returns></returns>
public bool[] ReadBool(string address, ushort length)
{
OperateResult<bool[]> res = ConnectionType == 1 ? Melsec_Net.ReadBool(address, length) : Melsec_Serial.ReadBool(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new bool[0];
}
}
/// <summary>
/// 批量读取flost(单精度浮点数)
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
public float[] ReadFloat(string address, ushort length)
{
OperateResult<float[]> res = ConnectionType == 1 ? Melsec_Net.ReadFloat(address, length) : Melsec_Serial.ReadFloat(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new float[0];
}
}
/// <summary>
/// 读取uint16
/// </summary>
/// <param name="address">读取地址</param>
/// <param name="length"></param>
/// <returns></returns>
public ushort[] ReadUint16(string address, ushort length)
{
OperateResult<ushort[]> res = ConnectionType == 1 ? Melsec_Net.ReadUInt16(address, length) : Melsec_Serial.ReadUInt16(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new ushort[0];
}
}
/// <summary>
/// 批量读取uint
/// </summary>
/// <param name="address">读取地址</param>
/// <param name="length"></param>
/// <returns></returns>
public uint[] ReadUint(string address, ushort length)
{
OperateResult<uint[]> res = ConnectionType == 1 ? Melsec_Net.ReadUInt32(address, length) : Melsec_Serial.ReadUInt32(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new uint[0];
}
}
/// <summary>
/// 批量读取Ulong
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
public ulong[] ReadUlong(string address, ushort length)
{
OperateResult<ulong[]> res = ConnectionType == 1 ? Melsec_Net.ReadUInt64(address, length) : Melsec_Serial.ReadUInt64(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new ulong[0];
}
}
/// <summary>
/// 批量读取Double
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
public double[] ReadDouble(string address, ushort length)
{
OperateResult<double[]> res = ConnectionType == 1 ? Melsec_Net.ReadDouble(address, length) : Melsec_Serial.ReadDouble(address, length);
if (res.IsSuccess)
{
return res.Content;
}
else
{
return new double[0];
}
}
#endregion
/// <summary>
/// 写入ushow
/// </summary>
/// <param name="address"></param>
/// <param name="val"></param>
/// <returns></returns>
public bool WriteShort(string address, ushort val)
{
return Melsec_Net.Write(address, val).IsSuccess;
}
/// <summary>
/// 写入 bool
/// </summary>
/// <param name="address"></param>
/// <param name="val"></param>
/// <returns></returns>
public bool SetSwitch_StartOrStop(string address, bool val)
{
return Melsec_Net.Write(address, new bool[] { val }).IsSuccess;
}
/// <summary>
/// 写入字符串
/// </summary>
/// <param name="address"></param>
/// <param name="val"></param>
/// <returns></returns>
public bool WriteString(string address, string val)
{
return Melsec_Net.Write(address, val).IsSuccess;
}
}
}