using Aitex.Core.RT.Log; using Aitex.Core.Util; using MECF.Framework.Common.Device.Festo; using MECF.Framework.Common.Net; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace MECF.Framework.Common.Device.Galil { public class GalilTcpDevice : JetMessageTcpClient { #region 内部变量 /// /// 名称 /// private string _module; /// /// 上一次错误信息 /// private string _lastErrorMsg = ""; /// /// IP地址 /// private string _ip = ""; /// /// 端口号 /// private int _port = 502; /// /// 定时器 /// private PeriodicJob _periodicJob; /// /// 共享锁 /// private object _locker = new object(); /// /// 共享锁时长 /// private int _lockerTime = 2000; #endregion /// /// 构造函数 /// /// /// public GalilTcpDevice(string module, string ip, int port) : base(ip, port) { ReceiveTimeout = 1000; SendTimeout = 1000; ConnectTimeout = 1000; _module = module; _ip = ip; _port = port; _periodicJob = new PeriodicJob(20, OnTimer, $"Galil {_module} timer", false, true); } /// /// 定时器执行 /// /// private bool OnTimer() { if (Monitor.TryEnter(_locker, _lockerTime)) { ApplyAllDatas(); Monitor.Exit(_locker); } return true; } /// /// 更新地址数量 /// /// public void Initialize() { NetResult result = Connect(); if (result.IsSuccess) { ApplyAllDatas(); LOG.WriteLog(eEvent.INFO_GALIL, _module, $"connect {_ip}:{_port} success"); } else { LOG.WriteLog(eEvent.INFO_GALIL, _module, $"connect {_ip}:{_port} failed"); } _periodicJob.Start(); } /// /// 设置Galil数值 /// /// /// /// public bool SetGalilCommand(string command) { GalilCommand galiCommand = new GalilCommand(); galiCommand.CommandCode = 0x06; galiCommand.SetData = command; if (Monitor.TryEnter(_locker, _lockerTime)) { bool result = SetOperation(galiCommand); Monitor.Exit(_locker); return result; } else { WriteErrMsg($"Write apply locker over {_lockerTime}"); return false; } } /// /// 设置操作 /// /// /// private bool SetOperation(GalilCommand command) { if (Connected) { return WriteCommand(command); } else { NetResult netResult = Connect(); if (netResult.IsSuccess) { return WriteCommand(command); } else { WriteErrMsg("connect failed"); return false; } } } /// /// 写指令 /// /// /// private bool WriteCommand(GalilCommand command) { NetResult netResult = SetData(command); if (!netResult.IsSuccess) { WriteErrMsg($"write value {command.SetData} failed,{netResult.Message}"); return false; } else { LOG.WriteLog(eEvent.INFO_GALIL, _module, $"write value {command.SetData} success"); } return true; } /// /// 申请所有数据 /// public void ApplyAllDatas() { GalilCommand command = new GalilCommand(); command.CommandCode = 0x03; command.SetData = "QR;"; ApplyDataOperation(command); } /// /// 申请数据操作 /// /// private void ApplyDataOperation(GalilCommand command) { if (!Connected) { NetResult connectResult = Connect(); if (!connectResult.IsSuccess) { WriteErrMsg("connect failed"); return; } } NetResult netResult = ApplyData(command); if (!netResult.IsSuccess) { WriteErrMsg($"apply data failed,{netResult.Message}"); return; } else { GalilControllerCfgManager.Instance.UpdateModuleData(_module, netResult.Data.ControllerData); } } /// /// 写错误日志 /// /// private void WriteErrMsg(string msg) { if (msg != _lastErrorMsg) { _lastErrorMsg = msg; LOG.WriteLog(eEvent.ERR_GALIL, _module, msg); } } } }