/// <summary> /// 启动 /// </summary> public async void OpenAsync() { try { if (opcClient?.Connected == false) { await Task.Run(() => { try { opcClient.Connent(); Log.Information("OPC已异步连接"); } catch (Exception ex) { Log.Error(ex, "OPC通讯连接异常"); } }); NetStatus = opcClient.Connected; NetChangedAction?.Invoke(this, opcClient.Connected); dicItems = Config.Items.ToDictionary(x => x.Misc?.ToString(), x => x); if (dicItems.ContainsKey("Heartbeat")) { timer.Start(); } } } catch (Exception e) { Log.Error(e, "opc启动异常"); } }
public OpcController(OpcConfig config) { timer.Interval = 5 * 1000; timer.Elapsed += Timer_Elapsed; Config = config; opcClient = new OpcDaClient(x => { x.ProgId = config.ProgId; x.Tags = config.Items.Select(xx => new OpcTagItem() { Id = xx.Id, ItemId = xx.ItemId, Misc = xx.Misc }).ToArray(); x.DefaultGroupUpdateRate = 100; }) { Shutdown = (s, client) => { if (NetStatus) { NetStatus = false; NetChangedAction?.Invoke(this, false); } Log.Warning("OPC Shutdown,{s}", s); }, DataChange = OpcDataChanged }; }
public bool Open() { if (opcClient == null) { throw new Exception("OPC客户端程序没有初始化."); } try { dicItems = Config.Items.ToDictionary(x => x.Misc?.ToString(), x => x); opcClient.Connent(); Log.Information("OPC已连接"); NetStatus = opcClient.Connected; heartTime = DateTime.Now; NetChangedAction?.BeginInvoke(this, true, null, null); return(true); } catch (Exception ex) { Log.Error(ex, "OPC通讯连接异常"); return(false); } finally { if (dicItems.ContainsKey("Heartbeat")) { timer.Start(); } } }
/// <summary> /// 创建通讯实例 /// </summary> private void CreateHost() { CmdHostFactory factory = new CmdHostFactory(); host = factory.CreateTcpClientHost( new TcpClientOptions(rfid.Host, rfid.RemotePort) { OnDisconnected = () => { Log.Warning("rfid断开连接。"); NetStatus = false; NetChangedAction?.Invoke(this, false); }, OnTryConnecting = (i) => Log.Information("rfid正在重连。"), OnConnected = () => { Log.Information("rfid已连接。"); NetStatus = true; NetChangedAction?.Invoke(this, true); } }, new CmdClientOptions(rfid.ProtocolType) { AutoOutputType = rfid.OutputType, AutoOutput = rfid.OutputOptions, OutputHandler = (b, t) => RfidOutPutHandler(b, t) } ); }
/// <summary> /// 建立通讯 /// 调用同步方法连接时,会报MianForm已经Disposed的异常 /// </summary> /// <returns></returns> public bool Connect() { try { if (TightenToolClient == null) { AE2DeviceFactory fac = new AE2DeviceFactory(); TightenToolClient = fac.CreateTightenDevice(tdConfig); TightenToolClient.NetChangedAction = TdNetChanged; TightenToolClient.OnLastTightenData = ReadLastTightenData; TightenToolClient.OnVehicleNumberAction = ReadVinNumber; } var start = DateTime.Now; if (TightenToolClient.Connect()) { NetStatus = true; //Subscribe(); } var cost = DateTime.Now - start; int connectCostTime = cost.Seconds + 2; Debug.WriteLine($"拧紧连接耗时{connectCostTime - 2}s"); Log.Information($"拧紧机{tdConfig.Host}通讯连接{(NetStatus ? "成功" : "失败")}。"); return(NetStatus); } catch (Exception ex) { Log.Error("拧紧通讯初始化失败", ex); NetChangedAction?.Invoke(tdConfig.ToolId, false); return(false); } }
/// <summary> /// opc消息处理 /// </summary> /// <param name="items"></param> private void ProcessOpcData(IEnumerable <OpcTagItem> items) { foreach (var item in items) { if (item == null) { continue; } try { if (item.Value == null) { continue; } bool value = false; switch (item.Misc) { case "Heartbeat": value = Convert.ToBoolean(item.Value); if (!value) { heartTime = DateTime.Now; if (NetStatus == false) { NetStatus = true; NetChangedAction?.Invoke(this, true); } } break; case "ReadShieldSystem": IsPLCShield = Convert.ToBoolean(item.Value); ShieldChangedAction?.Invoke(IsPLCShield); break; case "NoPass": //这里可以做信号校验 LineStatus = Convert.ToBoolean(item.Value); LineStopChangedAction?.Invoke(LineStatus); break; default: break; } } catch (Exception ex) { Log.Error(ex, "读取opc节点[{Misc}]信息异常", item.Misc); } } }
/// <summary> /// 网络连接状态改变 /// </summary> /// <param name="state"></param> private void TdNetChanged(bool state) { Debug.WriteLine($"MyTightenContrller -> TdNetChanged -> state : {state}"); if (state) { //连接成功后注册事件 Log.Information($"拧紧机已连接,{tdConfig.NickName} ({tdConfig.Host}:{tdConfig.Port})"); } try { //触发外界订阅的事件 NetChangedAction?.Invoke(tdConfig.ToolId, state); } catch (Exception ex) { Log.Error($"拧紧机{tdConfig.NickName} ({tdConfig.Host})在处理连接委托时异常。", ex); } }
/// <summary> /// 心跳定时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { if ((DateTime.Now - heartTime).TotalSeconds > 15) { NetStatus = false; NetChangedAction?.Invoke(this, false); heartTime = DateTime.Now; } if (opcClient != null && opcClient.Connected) { Heartbeat(); } } catch (Exception ex) { Log.Error(ex, "OPC心跳程序异常"); } }