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 { /// /// PLC通讯模型 /// public class HslModel : NotificationObject { public int ID { get; set; } #region 名称 private string nameStr; /// /// 名称 /// public string NameStr { get { return nameStr; } set { nameStr = value; RaisePropertyChanged("NameStr"); } } #endregion #region IP地址 private string ip; /// /// IP地址 /// public string IP { get { return ip; } set { ip = value; RaisePropertyChanged("IP"); } } #endregion #region 端口号 private int port; /// /// 端口号 /// public int Port { get { return port; } set { port = value; RaisePropertyChanged("Port"); } } #endregion #region 品牌 private int brand; /// /// 品牌 /// public int Brand { get { return brand; } set { brand = value; RaisePropertyChanged("Brand"); } } #endregion #region 连接方式 private int connectionType; /// /// 连接方式 1:TCP/IP 2:串口 /// public int ConnectionType { get { return connectionType; } set { connectionType = value; RaisePropertyChanged("ConnectionType"); } } #endregion #region 串口号 private string portName; /// /// 串口号 /// public string PortName { get { return portName; } set { portName = value; this.RaisePropertyChanged("PortName"); } } #endregion #region 波特率 private int baudRate; /// /// 波特率 /// public int BaudRate { get { return baudRate; } set { baudRate = value; this.RaisePropertyChanged("BaudRate"); } } #endregion #region 校验位 private int parityNumber; /// /// 校验位 /// public int ParityNumber { get { return parityNumber; } set { parityNumber = value; this.RaisePropertyChanged("ParityNumber"); } } #endregion #region 数据位 private int dataBits; /// /// 数据位 /// public int DataBits { get { return dataBits; } set { dataBits = value; this.RaisePropertyChanged("DataBits"); } } #endregion #region 停止位 private int stopBitsNumber; /// /// 停止位 /// public int StopBitsNumber { get { return stopBitsNumber; } set { stopBitsNumber = value; this.RaisePropertyChanged("StopBitsNumber"); } } #endregion private string notes; /// /// 备注 /// public string Notes { get { return notes; } set { notes = value; RaisePropertyChanged("Notes"); } } /// /// PLC网口连接对象 /// //public MelsecMcNet Melsec_Net { get; set; } public PanasonicMewtocolOverTcp Melsec_Net { get; set; } /// /// PLC串口连接对象 /// public MelsecFxSerial Melsec_Serial { get; set; } private ShowInfo plcStatus; /// /// PLC连接状态 /// public ShowInfo PlcStatus { get { return plcStatus; } set { plcStatus = value; RaisePropertyChanged("PlcStatus"); } } private ShowInfo plcRunStatus; /// /// PLC运行状态 /// public ShowInfo PlcRunStatus { get { return plcRunStatus; } set { plcRunStatus = value; RaisePropertyChanged("PlcRunStatus"); } } /// /// 构造函数 /// 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 /// /// PLC连接状态 /// public bool ConnectionState { get; set; } /// /// 连接PLC /// /// 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 断开连接 /// /// 断开连接 /// 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 单数读取 /// /// 读取字符串 /// /// 读取地址 /// 读取字符串长度 /// public string ReadString(string address, ushort Strlength) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadString(address, Strlength) : Melsec_Serial.ReadString(address, Strlength); if (res.IsSuccess) { return res.Content; } else { return ""; } } /// /// 读取int16 /// /// /// public short ReadShort(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadInt16(address) : Melsec_Serial.ReadInt16(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Int32 /// /// 读取地址 /// public int ReadInt32(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadInt32(address) : Melsec_Serial.ReadInt32(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Long /// /// /// public long ReadLong(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadInt64(address) : Melsec_Serial.ReadInt64(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Bool /// /// 读取地址 /// public bool ReadBool(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadBool(address) : Melsec_Serial.ReadBool(address); if (res.IsSuccess) { return res.Content; } else { return false; } } /// /// 读取flost(单精度浮点数) /// /// /// public float ReadFloat(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadFloat(address) : Melsec_Serial.ReadFloat(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取ushort /// /// 读取地址 /// public ushort ReadUint16(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadUInt16(address) : Melsec_Serial.ReadUInt16(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取uint /// /// 读取地址 /// public uint ReadUint(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadUInt32(address) : Melsec_Serial.ReadUInt32(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Ulong /// /// public ulong ReadUlong(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadUInt64(address) : Melsec_Serial.ReadUInt64(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Double /// /// /// public double ReadDouble(string address) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadDouble(address) : Melsec_Serial.ReadDouble(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取字节 /// /// /// /// public byte[] ReadByte(string address, ushort length) { OperateResult 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 批量读取 /// /// 读取int16 /// /// /// /// public short[] ReadShort(string address, ushort length) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadInt16(address, length) : Melsec_Serial.ReadInt16(address, length); if (res.IsSuccess) { return res.Content; } else { return new short[0]; } } /// /// 批量读取Int /// /// 读取地址 /// /// public int[] ReadInt32(string address, ushort length) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadInt32(address, length) : Melsec_Serial.ReadInt32(address, length); if (res.IsSuccess) { return res.Content; } else { return new int[0]; } } /// /// 读取Long /// /// /// /// public long[] ReadLong(string address, ushort length) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadInt64(address, length) : Melsec_Serial.ReadInt64(address, length); if (res.IsSuccess) { return res.Content; } else { return new long[0]; } } /// /// 批量读取Bool /// /// 读取地址 /// /// public bool[] ReadBool(string address, ushort length) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadBool(address, length) : Melsec_Serial.ReadBool(address, length); if (res.IsSuccess) { return res.Content; } else { return new bool[0]; } } /// /// 批量读取flost(单精度浮点数) /// /// /// /// public float[] ReadFloat(string address, ushort length) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadFloat(address, length) : Melsec_Serial.ReadFloat(address, length); if (res.IsSuccess) { return res.Content; } else { return new float[0]; } } /// /// 读取uint16 /// /// 读取地址 /// /// public ushort[] ReadUint16(string address, ushort length) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadUInt16(address, length) : Melsec_Serial.ReadUInt16(address, length); if (res.IsSuccess) { return res.Content; } else { return new ushort[0]; } } /// /// 批量读取uint /// /// 读取地址 /// /// public uint[] ReadUint(string address, ushort length) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadUInt32(address, length) : Melsec_Serial.ReadUInt32(address, length); if (res.IsSuccess) { return res.Content; } else { return new uint[0]; } } /// /// 批量读取Ulong /// /// /// /// public ulong[] ReadUlong(string address, ushort length) { OperateResult res = ConnectionType == 1 ? Melsec_Net.ReadUInt64(address, length) : Melsec_Serial.ReadUInt64(address, length); if (res.IsSuccess) { return res.Content; } else { return new ulong[0]; } } /// /// 批量读取Double /// /// /// /// public double[] ReadDouble(string address, ushort length) { OperateResult 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 /// /// 写入ushow /// /// /// /// public bool WriteShort(string address, ushort val) { return Melsec_Net.Write(address, val).IsSuccess; } /// /// 写入 bool /// /// /// /// public bool SetSwitch_StartOrStop(string address, bool val) { return Melsec_Net.Write(address, new bool[] { val }).IsSuccess; } /// /// 写入字符串 /// /// /// /// public bool WriteString(string address, string val) { return Melsec_Net.Write(address, val).IsSuccess; } } }