コード例 #1
0
        /// <summary>
        /// 运行调度设备
        /// </summary>
        /// <param name="dev"></param>
        private void RunDevice(IRunDevice dev)
        {
            ISocketSession io = null;

            try
            {
                io = (ISocketSession)this.Server.ChannelManager.GetChannel(dev.DeviceParameter.NET.RemoteIP, CommunicateType.NET);
                if (io != null)
                {
                    dev.Run(io);
                }
                else
                {
                    dev.Run((IChannel)null);   //如果没有找到连接,则传递空值
                    //没有找到可用的设备,加延时,局免循环过快。
                    System.Threading.Thread.Sleep(1000);
                }
            }
            catch (SocketException ex)
            {
                this.Server.Logger.Error(true, "网络控制器", ex);
            }
            catch (Exception ex)
            {
                this.Server.Logger.Error(true, "网络控制器", ex);
            }
        }
コード例 #2
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="data"></param>
        public void Send(IRunDevice dev, byte[] data)
        {
            int counter = this.Server.DeviceManager.GetCounter(dev.DeviceParameter.DeviceID);

            ISocketSession socketSession = (ISocketSession)this.Server.ChannelManager.GetChannel(dev.DeviceParameter.NET.RemoteIP, CommunicateType.NET);

            if (socketSession != null)
            {
                int sendNum = 0;
                lock (socketSession.SyncLock)
                {
                    sendNum = socketSession.Write(data);
                }

                if (sendNum == data.Length && sendNum != 0)
                {
                    Interlocked.Increment(ref counter);
                    this.Server.Logger.Info(false, dev.DeviceParameter.DeviceName + ">>发送请求数据");
                }
                else
                {
                    Interlocked.Increment(ref counter);
                    this.Server.Logger.Info(false, dev.DeviceParameter.DeviceName + ">>尝试发送数据失败");
                }

                dev.ShowMonitorData(data, "发送");

                if (counter >= 3)
                {
                    try
                    {
                        dev.Run(dev.DeviceParameter.NET.RemoteIP, null, new byte[] { });
                    }
                    catch (Exception ex)
                    {
                        this.Server.Logger.Info(true, dev.DeviceParameter.DeviceName + "," + ex.Message);
                    }

                    Interlocked.Exchange(ref counter, 0);
                }

                this.Server.DeviceManager.SetCounter(dev.DeviceParameter.DeviceID, counter);
            }
            else
            {
                try
                {
                    dev.Run((IChannel)null);   //如果没有找到连接,则传递空值
                }
                catch (Exception ex)
                {
                    this.Server.Logger.Error(true, "网络控制器", ex);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// 运行调度设备
        /// </summary>
        /// <param name="dev"></param>
        private void RunDevice(IRunDevice dev)
        {
            try
            {
                if (!_Com.IsDisposed)
                {
                    if (!_Com.IsOpen)
                    {
                        lock (_Com.SyncLock)
                        {
                            _Com.Open();
                        }
                    }

                    if (_Com.IsOpen)
                    {
                        dev.Run((IChannel)_Com); //驱动设备运行接口
                    }
                    else
                    {
                        dev.Run((IChannel)null);
                    }
                }
                else
                {
                    dev.Run((IChannel)null);
                }

                if (_Com == null || !_Com.IsOpen)
                {
                    System.Threading.Thread.Sleep(1000);
                }
                //------------------------------------------------//
            }
            catch (Exception ex)
            {
                this.Server.Logger.Error(true, "串口控制器", ex);
            }
        }
コード例 #4
0
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="socketSession"></param>
        /// <param name="dataPackage"></param>
        public void Receive(ISocketSession socketSession, IReceivePackage dataPackage)
        {
            if (dataPackage.ListBytes == null || dataPackage.ListBytes.Count <= 0)
            {
                return;
            }

            IRunDevice[] list = this.Server.DeviceManager.GetDevices(CommunicateType.NET);
            if (list != null && list.Length > 0)
            {
                if (this.Server.ServerConfig.ControlMode == ControlMode.Loop ||
                    this.Server.ServerConfig.ControlMode == ControlMode.Self ||
                    this.Server.ServerConfig.ControlMode == ControlMode.Parallel)
                {
                    #region
                    IRunDevice dev = GetDeliveryDevice(list, dataPackage);
                    if (dev != null)
                    {
                        lock (dev.SyncLock)
                        {
                            foreach (byte[] data in dataPackage.ListBytes)
                            {
                                #region
                                try
                                {
                                    if (data == null)
                                    {
                                        dev.Run(socketSession.Key, socketSession.Channel, new byte[] { });
                                    }
                                    else
                                    {
                                        dev.Run(socketSession.Key, socketSession.Channel, data);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Server.Logger.Error(true, "", ex);
                                }

                                int counter = this.Server.DeviceManager.GetCounter(dev.DeviceParameter.DeviceID);
                                Interlocked.Decrement(ref counter);
                                if (counter < 0)
                                {
                                    Interlocked.Exchange(ref counter, 0);
                                }
                                this.Server.DeviceManager.SetCounter(dev.DeviceParameter.DeviceID, counter);
                                #endregion
                            }
                        }
                    }
                    #endregion
                }
                else if (this.Server.ServerConfig.ControlMode == ControlMode.Singleton)
                {
                    #region
                    try
                    {
                        lock (list[0].SyncLock)
                        {
                            foreach (byte[] data in dataPackage.ListBytes)
                            {
                                list[0].Run(socketSession.Key, (IChannel)socketSession, data);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Server.Logger.Error(true, "", ex);
                    }
                    #endregion
                }
            }
        }