示例#1
0
        public override bool InitDriver(IO_SERVER server, IO_COMMUNICATION communication, List <IO_DEVICE> ioDevices, SCADA_DRIVER driver)
        {
            try
            {
                base.InitDriver(server, communication, ioDevices, driver);
                if (IsCreateControl)
                {
                    CommunicationControl = new Modbus_Serial_Ctrl();
                    CommunicationControl.SetUIParameter(communication.IO_COMM_PARASTRING);
                }
                //构造获取数据命令的字节数组,Modbus
                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.SlaveId = this.IODevices[i].IO_DEVICE_ADDRESS;
                        //数据库中系统编号
                        mRealData.DEVICEID = this.IODevices[i].IO_DEVICE_ID;

                        //获取下发命令的参数
                        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
            {
                return(false);
            }
            return(true);
        }
示例#2
0
        protected override void Start()
        {
            if (master != null)
            {
                master.Dispose();
            }
            if (_serialPort != null)
            {
                _serialPort.Close();
                _serialPort.Dispose();
                _serialPort = null;
            }

            try
            {
                Serial_PARA = new Modbus_Serial_PARA();
                byte[] array = new byte[8];
                if (_serialPort == null)
                {
                    _serialPort = new SerialPort(Serial_PARA.SerialPort);
                }
                #region
                {
                    _serialPort.BaudRate = Serial_PARA.BaudRate;
                    _serialPort.DataBits = Serial_PARA.DataBits;
                    switch (Serial_PARA.SerialCheck)
                    {
                    case SerialCheck.无:
                    {
                        _serialPort.Parity = Parity.None;
                    }
                    break;

                    case SerialCheck.奇校验:
                    {
                        _serialPort.Parity = Parity.Odd;
                    }
                    break;

                    case SerialCheck.偶校验:
                    {
                        _serialPort.Parity = Parity.Even;
                    }
                    break;

                    case SerialCheck.常0:
                    {
                        _serialPort.Parity = Parity.Space;
                    }
                    break;

                    case SerialCheck.常1:
                    {
                        _serialPort.Parity = Parity.Mark;
                    }
                    break;
                    }

                    _serialPort.StopBits     = Serial_PARA.StopBits;
                    _serialPort.ReadTimeout  = 1000;
                    _serialPort.WriteTimeout = 1000;
                    _serialPort.RtsEnable    = Serial_PARA.RTSEnable;
                    _serialPort.NewLine      = NewLine;
                    _serialPort.Open();
                    master = ModbusSerialMaster.CreateRtu(_serialPort);
                    Thread.Sleep(800);
                    //此处采用多线程技术创建一个实时读取数据的任务
                    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)
                                {
                                    for (int c = 0; c < mRealData.ReadSendByte.Count; c++)
                                    {
                                        try
                                        {
                                            //发送获取数据的命令
                                            string error = "";
                                            if (!this.Send(mRealData.Device, mRealData.ReadSendByte[c], out error))
                                            {
                                                this.DeviceException("ERROR=Modbus_Serial_10001," + error);
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            this.DeviceException("ERROR=Modbus_Serial_10002," + e.Message);
                                        }
                                    }
                                }
                                //更新周期
                                Thread.Sleep(mRealData.Device.IO_DEVICE_UPDATECYCLE);
                            }
                        });
                    }

                    this.CommunctionStartChanged(this.IOServer, this.IOServer.SERVER_IP + " " + this.IOServer.SERVER_NAME + "启动服务");
                    //实时接收数据
                    Task.Run(() => {
                        while (true)
                        {
                            try
                            {
                                //判断串口是否处于打开状态,如果不在打开状态,则重新打开
                                if (!_serialPort.IsOpen)
                                {
                                    _serialPort.Open();
                                }
                                //读取数据
                                if (_serialPort.BytesToRead > 0)
                                {
                                }
                            }
                            catch (Exception emx)
                            {
                                //处理异常
                                this.DeviceException("ERROR=Modbus_Serial_10006," + emx.Message);
                                //等待10秒
                                Thread.Sleep(10000);
                                //重新初始化
                                if (Serial_PARA.Counting >= Serial_PARA.CollectFaultsNumber)
                                {
                                    Start();
                                    return;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    });
                }
                #endregion
            }
            catch (Exception emx)
            {
            }
        }