using Aitex.Core.RT.Log; using Aitex.Core.Util; using DocumentFormat.OpenXml.Wordprocessing; using MECF.Framework.Common.Device.PowerSupplier; 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.Festo { public class FestoModbusDevice : JetMessageTcpClient { #region 内部变量 /// /// 名称 /// private string _name; /// /// 上一次错误信息 /// private string _lastErrorMsg = ""; /// /// do的数量 /// private ushort _addressCount = 6; /// /// di开始地址 /// private ushort _diStartAddress = 45395; /// /// 通道 /// private byte _channel = 1; /// /// IP地址 /// private string _ip = ""; /// /// 端口号 /// private int _port = 502; /// /// 定时器 /// private PeriodicJob _periodicJob; /// /// 共享锁 /// private object _locker = new object(); /// /// 共享锁时长 /// private int _lockerTime = 2000; #endregion /// /// 构造函数 /// /// /// public FestoModbusDevice(string name, string ip, int port,ushort diStartAddress,byte channel) : base(ip, port) { ReceiveTimeout = 1000; SendTimeout = 1000; ConnectTimeout = 1000; _name = name; _diStartAddress=diStartAddress; _ip = ip; _port = port; _channel = channel; _periodicJob = new PeriodicJob(200, OnTimer, $"festo {name} timer", false,true); } /// /// 定时器执行 /// /// private bool OnTimer() { if(Monitor.TryEnter(_locker, _lockerTime)) { ApplyAllDatas(); Monitor.Exit(_locker); } return true; } /// /// 更新地址数量 /// /// public void InitializeAddressCount(ushort addressCount) { _addressCount = addressCount; NetResult result = Connect(); if (result.IsSuccess) { ApplyAllDatas(); LOG.WriteLog(eEvent.INFO_FESTO, _name, $"connect {_ip}:{_port} success"); } else { LOG.WriteLog(eEvent.INFO_FESTO, _name, $"connect {_ip}:{_port} failed"); } _periodicJob.Start(); } /// /// 设置Festo数值 /// /// /// /// public bool SetFestoValue(ushort address,byte value) { FestoCommand command = new FestoCommand(); command.Channel = _channel; command.Address=address; command.CommandCode = 0x06; command.Datas = new byte[1] { value }; if (Monitor.TryEnter(_locker, _lockerTime)) { bool result= SetOperation(command); Monitor.Exit(_locker); return result; } else { WriteErrMsg($"Write apply locker over {_lockerTime}"); return false; } } /// /// 设置操作 /// /// /// private bool SetOperation(FestoCommand command) { if (Connected) { NetResult netResult = SetData(command); if (!netResult.IsSuccess) { WriteErrMsg($"write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}"); return false; } else { LOG.WriteLog(eEvent.INFO_FESTO, _name, $"write {command.Address.ToString("X2")} value {command.Datas[0]} success"); } return true; } else { NetResult netResult = Connect(); if (netResult.IsSuccess) { netResult = SetData(command); if (!netResult.IsSuccess) { WriteErrMsg($"write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}"); return false; } return true; } else { WriteErrMsg("connect failed"); return false; } } } /// /// 申请所有数据 /// public void ApplyAllDatas() { FestoCommand command = new FestoCommand(); command.Address = _diStartAddress; command.RegisterCount = (ushort)(_addressCount*2); command.CommandCode = 0x03; command.Channel = _channel; ApplyDataOperation(command); } /// /// 申请数据操作 /// /// private void ApplyDataOperation(FestoCommand command) { if (!Connected) { NetResult connectResult = Connect(); if (!connectResult.IsSuccess) { WriteErrMsg("connect failed"); return; } } NetResult netResult = ApplyData(command); if (!netResult.IsSuccess) { WriteErrMsg($"apply {command.Address.ToString("X2")} failed,{netResult.Message}"); return; } if (netResult.Data.Datas != null) { //bit0--高8位,bit1-低8位,bit2-下一地址高8位,bit3--下一地址低8位,只取bit1 byte[] bytes = new byte[_addressCount]; for(int i = 0; i < netResult.Data.Datas.Length; i += 4) { if (i + 1 < netResult.Data.Datas.Length&&i/4<_addressCount) { byte data = netResult.Data.Datas[i + 1]; bytes[i / 4] = data; } } FestoControllerCfgManager.Instance.UpdateFestoData(_name, bytes); } } /// /// 写错误日志 /// /// private void WriteErrMsg(string msg) { if (msg != _lastErrorMsg) { _lastErrorMsg = msg; LOG.WriteLog(eEvent.ERR_FESTO, _name, msg); } } } }