using System; using System.Collections.Generic; using System.Linq; using System.Text; using Mesnac.Equips; namespace Mesnac.Equip.OMRON.FINS.GroupNet { public class Equip : BaseEquip { private TcpFactory _socketFactory = null;//TcpFactory.GetInstance("53", "53", "192.168.1.83", "9600"); private bool _isOpen = false; //是否打开连接 private Mesnac.Equips.Connection.GroupNet.ConnType connType = null; private Dictionary _dicIP = null; #region 辅助方法 - 获取设备组IP字典 public Dictionary GetIPS() { this.connType = (Mesnac.Equips.Connection.GroupNet.ConnType)this.Main.ConnType; this._dicIP = new Dictionary(); try { if (!String.IsNullOrEmpty(this.connType.IPList)) { string[] strItems = this.connType.IPList.Split(new char[] { ';' }); if (null != strItems && strItems.Length > 0) { foreach (string strItem in strItems) { if (!String.IsNullOrEmpty(strItem)) { string[] items = strItem.Split(new char[] { ':' }); if (null != items && items.Length > 0) { this._dicIP.Add(items[0], items[1]); } } } } } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("打开设备失败:解析设备配置错误-" + ex.Message); } return this._dicIP; } #endregion /// /// 获取串口工厂 /// /// private TcpFactory GetTcpFactory() { //Mesnac.Equips.Connection.COM.ConnType connType = (Mesnac.Equips.Connection.COM.ConnType)this.Main.ConnType; string IP = this._dicIP[this.Name]; ; this._socketFactory = TcpFactory.GetInstance("", "", IP, "9600"); return this._socketFactory; } public override bool Open() { try { if (this._dicIP == null) { this._dicIP = GetIPS(); } if (this._isOpen == true && (_socketFactory != null)) { return true; } this.State = false; this._socketFactory = this.GetTcpFactory(); this._socketFactory.creatConn(); if (!this._socketFactory.MainTCPSocket.Connected) { //ICSharpCode.Core.LoggingService.Warn("PLC连接失败:串口未打开失败!"); this.State = false; return this.State; } else { this.State = true; this._isOpen = true; Console.WriteLine("连接成功!"); return this.State; } } catch (Exception ex) { this.State = false; this._isOpen = false; Console.WriteLine(ex.Message); return this.State; } } public override bool Read(string block, int start, int len, out object[] buff) { buff = new object[len]; try { if (len > 256) { for (int i = 0; i < len; i++) { buff[i] = 0; } base.State = false; return false; } int maxOneLen = 50; //单次允许读取的最大长度,欧姆龙限制为50个字 int count = len / maxOneLen; //要读取的次数 int mod = len % maxOneLen; //剩余的长度 bool flag = true; //保存读取标志 for (int i = 0; i < count; i++) { object[] _buff = new object[maxOneLen]; flag = this.ReadByLen(block, start + i * maxOneLen, maxOneLen, out _buff); if (flag == false) { base.State = flag; return false; } for (int k = i * maxOneLen; k < (i + 1) * maxOneLen; k++) { buff[k] = _buff[k - i * maxOneLen]; } } if (mod > 0) { object[] _buff = new object[mod]; flag = this.ReadByLen(block, start + count * maxOneLen, mod, out _buff); if (flag == false) { base.State = flag; return false; } for (int k = count * maxOneLen; k < count * maxOneLen + mod; k++) { buff[k] = _buff[k - count * maxOneLen]; } } base.State = flag; return flag; } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("读取PLC(OMRON)设备失败-({0})!", ex.Message)); base.State = false; return false; } } /// /// 单次读取最长20个字的方法 /// /// 块号 /// 起始字 /// 长度,最长不超过100 /// 数据缓冲区,存放读取的数据 /// 读取成功返回true,读取失败返回false private bool ReadByLen(string block, int start, int len, out object[] buff) { lock (this) { buff = new object[len]; if (!this.Open()) { return false; } int state = len; object[] _buff = new object[len]; int iblock = Convert.ToInt32(block); //iblock = iblock + start; bool result = this._socketFactory.Read(iblock, start, len, out _buff); if (!result) { //ICSharpCode.Core.LoggingService.Warn("PLC读取失败:" + this.GetErrInfo(result)); this.State = false; return false; } else { this.State = true; } int iReadLen = len; if (iReadLen > state) { iReadLen = state; } for (int i = 0; i < iReadLen; i++) { int value = 0; int.TryParse(_buff[i].ToString(), out value); if (value > ushort.MaxValue) { value = ushort.MaxValue - value; } buff[i] = value; } return true; } } public override bool Write(int block, int start, object[] buff) { lock (this) { try { if (buff.Length > 256) { return false; } int len = buff.Length; int maxOneLen = 25; //单次允许读取的最大长度,OMRON限制为25个字 int count = len / maxOneLen; //要读取的次数 int mod = len % maxOneLen; //剩余的长度 bool flag = true; //保存写入标志 int[] _buff = new int[buff.Length]; for (int i = 0; i < buff.Length; i++) { //object[] _buff = new object[len]; //for (int k = i * maxOneLen; k < (i + 1) * maxOneLen; k++) //{ // _buff[k - i * maxOneLen] = buff[k]; //} int value = 0; int.TryParse(buff[i].ToString(), out value); _buff[i] = value; } flag = this._socketFactory.Write(block, start, _buff); if (flag == false) { return false; } return flag; } catch (Exception ex) { //ICSharpCode.Core.LoggingService.Error(String.Format("写入PLC(OMRON)设备失败-({0})!", ex.Message)); //Console.WriteLine(ex.Message); return false; } } } public override void Close() { lock (this) { try { this._socketFactory.MainTCPSocket.Close(); if (this._socketFactory.MainTCPSocket.Connected) { //ICSharpCode.Core.LoggingService.Warn("PLC【欧姆龙】关闭失败:端口处于打开状态"); } else { this.State = false; this._isOpen = false; //Console.WriteLine("关闭成功!"); } } catch (Exception ex) { //ICSharpCode.Core.LoggingService.Error("PLC【欧姆龙】关闭失败:" + ex.Message); } } } } }