using HslCommunication; 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.HSL { /// /// 松下PLC /// public class PanasonicHsl : HslParent { /// /// PLC网口连接对象 /// public PanasonicMewtocolOverTcp NetworkConn { get; set; } /// /// PLC串口连接对象 /// public PanasonicMewtocolServer SerialConn { get; set; } #region TCP连接构造函数 /// /// TCP连接构造函数 /// /// /// public PanasonicHsl(string ip, int port) { PlcStatus = new ShowInfo() { StatuInfo = "等待连接", StatusBackground = ColorStr.Yellow }; PlcRunStatus = new ShowInfo() { StatuInfo = "未知", StatusBackground = ColorStr.Blue }; ConnectionType = 1; this.IP = ip; this.Port = port; NetworkConn = new PanasonicMewtocolOverTcp(IP, Port, 0xEE); } #endregion #region 串口连接构造函数 /// /// 串口连接构造函数 /// /// /// /// /// /// public PanasonicHsl(string name, int baudRate, int dataBits, int stopBitsNumber, int parityNumber) { ConnectionType = 2; PlcStatus = new ShowInfo() { StatuInfo = "等待连接", StatusBackground = ColorStr.Yellow }; PlcRunStatus = new ShowInfo() { StatuInfo = "未知", StatusBackground = ColorStr.Blue }; this.PortName = name; this.BaudRate = baudRate; this.DataBits = dataBits; this.StopBitsNumber = stopBitsNumber; this.ParityNumber = parityNumber; //这里要查一下 松下PLC串口的连接方式 SerialConn?.Dispose(); SerialConn = new PanasonicMewtocolServer(); //SerialConn.IsStarted= true; SerialConn.StartSerialSlave(sp => { sp.PortName = PortName; sp.BaudRate = BaudRate; sp.DataBits = DataBits; sp.StopBits = (StopBits)StopBitsNumber;//StopBits.One;//设置当前停止位 sp.Parity = (Parity)ParityNumber; //Parity.None;//设置当前校验位 }); } #endregion #region 连接PLC /// /// 连接PLC /// /// public override bool Conneced() { if (ConnectionType == 1) { PlcStatus.StatuInfo = "NotConnected"; NetworkConn.ConnectTimeOut = 2000; //melsec_net.NetworkNumber = 0x00; //melsec_net.NetworkStationNumber = 0x00; OperateResult connected_Et = NetworkConn.ConnectServer(); if (connected_Et.IsSuccess) { NetworkConn.SetPersistentConnection(); //设置一个长连接 掉线自动连 PlcStatus.StatuInfo = "连接成功"; PlcStatus.StatusBackground = ColorStr.Green; return true; } else { PlcStatus.StatuInfo = "连接失败" + connected_Et.Message; PlcStatus.StatusBackground = ColorStr.Red; return false; } } else { //OperateResult connected_Et = SerialConn.; //if (connected_Et.IsSuccess) //{ // PlcStatus.StatuInfo = "连接成功"; // PlcStatus.StatusBackground = ColorStr.Green; // return true; //} //else //{ // PlcStatus.StatuInfo = "连接失败" + connected_Et.Message; // PlcStatus.StatusBackground = ColorStr.Red; // return false; //} return true; } } #endregion #region 断开连接 /// /// 断开连接 /// public override void Disconnect() { if (NetworkConn != null && ConnectionType == 1) { NetworkConn.ConnectClose(); NetworkConn.Dispose(); NetworkConn = null; PlcStatus.StatuInfo = "断开成功"; PlcStatus.StatusBackground = ColorStr.Red; } //else if (SerialConn != null && ConnectionType == 2) //{ // SerialConn.Close(); // SerialConn.Dispose(); // PlcStatus.StatuInfo = "断开成功"; // PlcStatus.StatusBackground = ColorStr.Red; //} } #endregion #region 单数读取 /// /// 读取字符串 /// /// 读取地址 /// 读取字符串长度 /// public override string ReadString(string address, ushort Strlength) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadString(address, Strlength) : SerialConn.ReadString(address, Strlength); if (res.IsSuccess) { return res.Content; } else { return ""; } } /// /// 读取int16 /// /// /// public override short ReadShort(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadInt16(address) : SerialConn.ReadInt16(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Int32 /// /// 读取地址 /// public override int ReadInt32(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadInt32(address) : SerialConn.ReadInt32(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Long /// /// /// public override long ReadLong(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadInt64(address) : SerialConn.ReadInt64(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Bool /// /// 读取地址 /// public override bool ReadBool(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadBool(address) : SerialConn.ReadBool(address); if (res.IsSuccess) { return res.Content; } else { return false; } } /// /// 读取flost(单精度浮点数) /// /// /// public override float ReadFloat(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadFloat(address) : SerialConn.ReadFloat(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取ushort /// /// 读取地址 /// public override ushort ReadUint16(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadUInt16(address) : SerialConn.ReadUInt16(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取uint /// /// 读取地址 /// public override uint ReadUint(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadUInt32(address) : SerialConn.ReadUInt32(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Ulong /// /// public override ulong ReadUlong(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadUInt64(address) : SerialConn.ReadUInt64(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取Double /// /// /// public override double ReadDouble(string address) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadDouble(address) : SerialConn.ReadDouble(address); if (res.IsSuccess) { return res.Content; } else { return 0; } } /// /// 读取字节 /// /// /// /// public override byte[] ReadByte(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.Read(address, length) : SerialConn.Read(address, length); if (res.IsSuccess) { return res.Content; } else { return new byte[0]; } } #endregion #region 批量读取 /// /// 读取int16 /// /// /// /// public override short[] ReadShort(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadInt16(address, length) : SerialConn.ReadInt16(address, length); if (res.IsSuccess) { return res.Content; } else { return new short[0]; } } /// /// 批量读取Int /// /// 读取地址 /// /// public override int[] ReadInt32(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadInt32(address, length) : SerialConn.ReadInt32(address, length); if (res.IsSuccess) { return res.Content; } else { return new int[0]; } } /// /// 读取Long /// /// /// /// public override long[] ReadLong(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadInt64(address, length) : SerialConn.ReadInt64(address, length); if (res.IsSuccess) { return res.Content; } else { return new long[0]; } } /// /// 批量读取Bool /// /// 读取地址 /// /// public override bool[] ReadBool(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadBool(address, length) : SerialConn.ReadBool(address, length); if (res.IsSuccess) { return res.Content; } else { return new bool[0]; } } /// /// 批量读取flost(单精度浮点数) /// /// /// /// public override float[] ReadFloat(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadFloat(address, length) : SerialConn.ReadFloat(address, length); if (res.IsSuccess) { return res.Content; } else { return new float[0]; } } /// /// 读取uint16 /// /// 读取地址 /// /// public override ushort[] ReadUint16(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadUInt16(address, length) : SerialConn.ReadUInt16(address, length); if (res.IsSuccess) { return res.Content; } else { return new ushort[0]; } } /// /// 批量读取uint /// /// 读取地址 /// /// public override uint[] ReadUint(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadUInt32(address, length) : SerialConn.ReadUInt32(address, length); if (res.IsSuccess) { return res.Content; } else { return new uint[0]; } } /// /// 批量读取Ulong /// /// /// /// public override ulong[] ReadUlong(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadUInt64(address, length) : SerialConn.ReadUInt64(address, length); if (res.IsSuccess) { return res.Content; } else { return new ulong[0]; } } /// /// 批量读取Double /// /// /// /// public override double[] ReadDouble(string address, ushort length) { OperateResult res = ConnectionType == 1 ? NetworkConn.ReadDouble(address, length) : SerialConn.ReadDouble(address, length); if (res.IsSuccess) { return res.Content; } else { return new double[0]; } } #endregion #region 写入ushow /// /// 写入ushow /// /// /// /// public override bool Write(string address, params ushort[] val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion #region 写入short /// /// 写入short /// /// /// /// public override bool Write(string address, params short[] val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion #region 写入 int /// /// 写入 int /// /// /// /// public override bool Write(string address, params int[] val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion #region 写入 long /// /// 写入 long /// /// /// /// public override bool Write(string address, params long[] val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion #region 写入 float /// /// 写入 float /// /// /// /// public override bool Write(string address, params float[] val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion #region 写入 byte /// /// 写入 byte /// /// /// /// public override bool Write(string address, params byte[] val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion #region 写入 double /// /// 写入 double /// /// /// /// public override bool Write(string address, params double[] val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion #region 写入 bool /// /// 写入 bool /// /// /// /// public override bool Write(string address, bool val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion #region 写入字符串 /// /// 写入字符串 /// /// /// /// public override bool Write(string address, string val) { return ConnectionType == 1 ? NetworkConn.Write(address, val).IsSuccess : SerialConn.Write(address, val).IsSuccess; } #endregion } }