123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- 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<GalilMessage, GalilCommand>
- {
- #region 内部变量
- /// <summary>
- /// 名称
- /// </summary>
- private string _module;
- /// <summary>
- /// 上一次错误信息
- /// </summary>
- private string _lastErrorMsg = "";
- /// <summary>
- /// IP地址
- /// </summary>
- private string _ip = "";
- /// <summary>
- /// 端口号
- /// </summary>
- private int _port = 502;
- /// <summary>
- /// 定时器
- /// </summary>
- private PeriodicJob _periodicJob;
- /// <summary>
- /// 共享锁
- /// </summary>
- private object _locker = new object();
- /// <summary>
- /// 共享锁时长
- /// </summary>
- private int _lockerTime = 2000;
- #endregion
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="ip"></param>
- /// <param name="port"></param>
- 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);
- }
- /// <summary>
- /// 定时器执行
- /// </summary>
- /// <returns></returns>
- private bool OnTimer()
- {
- if (Monitor.TryEnter(_locker, _lockerTime))
- {
- ApplyAllDatas();
- Monitor.Exit(_locker);
- }
- return true;
- }
- /// <summary>
- /// 更新地址数量
- /// </summary>
- /// <param name="addressCount"></param>
- 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();
- }
- /// <summary>
- /// 设置Galil数值
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 设置操作
- /// </summary>
- /// <param name="command"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
- /// <summary>
- /// 写指令
- /// </summary>
- /// <param name="command"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 申请所有数据
- /// </summary>
- public void ApplyAllDatas()
- {
- GalilCommand command = new GalilCommand();
- command.CommandCode = 0x03;
- command.SetData = "QR;";
- ApplyDataOperation(command);
- }
- /// <summary>
- /// 申请数据操作
- /// </summary>
- /// <param name="command"></param>
- private void ApplyDataOperation(GalilCommand command)
- {
- if (!Connected)
- {
- NetResult connectResult = Connect();
- if (!connectResult.IsSuccess)
- {
- WriteErrMsg("connect failed");
- return;
- }
- }
- NetResult<GalilCommand> netResult = ApplyData(command);
- if (!netResult.IsSuccess)
- {
- WriteErrMsg($"apply data failed,{netResult.Message}");
- return;
- }
- else
- {
- GalilControllerCfgManager.Instance.UpdateModuleData(_module, netResult.Data.ControllerData);
- }
- }
- /// <summary>
- /// 写错误日志
- /// </summary>
- /// <param name="msg"></param>
- private void WriteErrMsg(string msg)
- {
- if (msg != _lastErrorMsg)
- {
- _lastErrorMsg = msg;
- LOG.WriteLog(eEvent.ERR_GALIL, _module, msg);
- }
- }
- }
- }
|