Exemple #1
0
        public override bool InitDriver(IO_SERVER server, IO_COMMUNICATION communication, List <IO_DEVICE> ioDevices, COMM_DRIVER driver)
        {
            if (!base.InitDriver(server, communication, ioDevices, driver))
            {
                return(false);
            }
            try
            {
                ///初始化驱动控件
                if (IsCreateControl)//主要是为了节省内存,在该用的地方创建
                {
                    this.CommunicationControl = new CMDTUControl();
                }

                //构造获取数据命令的字节数组
                for (int i = 0; i < this.IODevices.Count; i++)
                {
                    RealData mRealData = new RealData();
                    mRealData.Device = this.IODevices[i];
                    DeviceDrive driverDll = DeviceDrives.Find(x => x.DeviceDriverID == this.IODevices[i].DEVICE_DRIVER_ID);
                    if (driverDll != null)
                    {
                        driverDll.InitDrive(IOServer, IOCommunication, this.IODevices[i], null, this.IODevices[i].DriverInfo);
                        //IO_DEVICE_ADDRESS中存储的是DTU编号
                        mRealData.DTUID = this.IODevices[i].IO_DEVICE_ADDRESS;
                        //获取下发命令的参数
                        mRealData.ReadSendByte = driverDll.GetDataCommandBytes(this.IOServer, this.IOCommunication, this.IODevices[i], this.IODevices[i].IOParas, null);
                    }
                    if (mRealData.ReadSendByte != null && mRealData.ReadSendByte.Count > 0)
                    {
                        RealDevices.Add(mRealData);
                    }
                }
            }
            catch (Exception ex)
            {
                this.DeviceException("ERROR=10023," + ex.Message);
                return(false);
            }
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// 启动通道
        /// </summary>
        protected override void Start()
        {
            try
            {
                int    Port       = 8888;
                int    Timeout    = 120;
                string parastring = this.CommunicationControl.ParameterString;
                char[] split      = new char[2] {
                    ':', ','
                };
                string[] strs = parastring.Split(split);
                if (strs.Length != 8)
                {
                    this.DeviceException("ERROR=10001, DTU 驱动参数不正确");
                    return;
                }
                int.TryParse(strs[1], out Port);
                //初始化DTU服务
                svr = new Server(Port, Timeout, mode);
                //数据接收事件
                svr.ReceiveData += Svr_ReceiveData;
                //DTU连接事件
                svr.ClientConnect += Svr_ClientConnect;
                //DTU退出连接后的事件
                svr.ClientClose += Svr_ClientClose;


                svr.Start();
                //此处采用多线程技术
                for (int i = 0; i < this.RealDevices.Count; i++)
                {
                    RealData mRealData = this.RealDevices[i];

                    //创建一个子任务
                    Task.Run(() =>
                    {
                        while (true && this.ServerIsRun)
                        {
                            if (this.ServerIsSuspend)
                            {
                                continue;
                            }
                            if (mRealData.ReadSendByte.Count > 0 && svr.Count > 0)
                            {
                                for (int c = 0; c < mRealData.ReadSendByte.Count; c++)
                                {
                                    try
                                    {
                                        //发送获取数据的命令
                                        string error = "";
                                        if (!svr.Send(mRealData.DTUID, mRealData.ReadSendByte[c], out error, true))
                                        {
                                            this.DeviceException("ERROR=10004," + error);
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        this.DeviceException("ERROR=10002," + e.Message);
                                    }
                                }
                            }
                            //更新周期
                            Thread.Sleep(mRealData.Device.IO_DEVICE_UPDATECYCLE);
                        }
                    });
                }

                this.CommunctionStartChanged(this.IOServer, this.IOServer.SERVER_IP + " " + this.IOServer.SERVER_NAME + "启动服务");
            }
            catch (Exception emx)
            {
                this.DeviceException("ERROR=10003," + emx.Message);
                this.CommunctionStartChanged(this.IOServer, this.IOServer.SERVER_IP + " " + this.IOServer.SERVER_NAME + "启动服务失败");
            }
            finally
            {
            }
        }