Example #1
0
        protected IModbusMaster GetOrAddConnection(ConnectionType ctype, string conn)
        {
            switch (ctype)
            {
            case ConnectionType.SERIALRTU:
                return(connections.GetOrAdd(conn, (arg) =>
                {
                    return ModbusSerialMaster.CreateRtu(new SerialPort(conn));
                }));

            case ConnectionType.SERIALASCII:
                return(connections.GetOrAdd(conn, (arg) =>
                {
                    return ModbusSerialMaster.CreateAscii(new SerialPort(conn));
                }));

            case ConnectionType.TCP:
                int port; IPAddress ip;
                if (!verifyIPConnectionString(conn, out ip, out port))
                {
                    return(null);
                }

                return(connections.GetOrAdd(conn, (arg) =>
                {
                    return ModbusIpMaster.CreateIp(new TcpClient(ip.ToString(), port));
                }));
            }
            return(null);
        }
Example #2
0
        public bool openComm()
        {
            LogClass.GetInstance().WriteLogFile("OpenComm Called " + port.PortName);
            lock (locker)
            {
                if (port != null && !port.IsOpen)
                {
                    try
                    {
                        port.Open();
                        LogClass.GetInstance().WriteLogFile("port opened");
                        // create modbus master
                        if (InputModbusType == "RTU")
                        {
                            master = ModbusSerialMaster.CreateRtu(port);
                        }
                        else
                        {
                            master = ModbusSerialMaster.CreateAscii(port);
                        }

                        return(true);
                    }
                    catch (Exception ex)
                    {
                        LogClass.GetInstance().WriteExceptionLog(ex);
                        //MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return(false);
                    }
                }
            }

            return(true);
        }
Example #3
0
        private float ReadFloatRegister(ushort startAddress)
        {
            CheckPort();
            try
            {
                if (!port.IsOpen)
                {
                    port.Open();
                }
                IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
                //master.Transport.ReadTimeout = timeout;

                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, 2);
                return
                    (ModbusUtility.GetSingle(registers[1], registers[0]));
            }
            catch (TimeoutException)
            {
                timerGraphUpdate.Stop();
                MessageBox.Show("Порт не отвечает");
                return(0);
            }
            catch (Exception ex)
            {
                timerGraphUpdate.Stop();
                MessageBox.Show(ex.Message);
                return(0);
            }
        }
Example #4
0
        public ModbusASCII(string portName, int baudRate, string parity, int dataBit, string stBit)
        {
            Parity masterParity = Parity.None;

            if (parity == "Odd")
            {
                masterParity = Parity.Odd;
            }
            if (parity == "Even")
            {
                masterParity = Parity.Even;
            }

            StopBits stopBit = StopBits.None;

            if (stBit == "One")
            {
                stopBit = StopBits.One;
            }
            if (stBit == "Two")
            {
                stopBit = StopBits.Two;
            }

            serialPort = new SerialPort(portName, baudRate, masterParity, dataBit, stopBit);
            master     = ModbusSerialMaster.CreateAscii(serialPort);
            master.Transport.ReadTimeout = 500;
        }
    public NModbusSerialAsciiMasterJamodSerialAsciiSlaveFixture()
    {
        StartJamodSlave(program);

        MasterSerialPort = CreateAndOpenSerialPort(DefaultMasterSerialPortName);
        Master           = ModbusSerialMaster.CreateAscii(MasterSerialPort);
    }
Example #6
0
        private ushort ReadIntRegister(ushort startAddress)
        {
            CheckPort();
            try
            {
                if (!port.IsOpen)
                {
                    port.Open();
                }
                IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);


                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, 1);

                return(registers[0]);
            }
            catch (TimeoutException)
            {
                timerGraphUpdate.Stop();
                MessageBox.Show("Порт не отвечает");
                return(0);
            }
            catch (Exception ex)
            {
                timerGraphUpdate.Stop();
                MessageBox.Show(ex.Message);
                return(0);
            }
        }
Example #7
0
        public void ReConnect()
        {
            m_mutex.WaitOne();

            try
            {
                Disconnect();

                m_port = new SerialPort(m_settings.COM)
                {
                    BaudRate     = m_settings.Baudrate,
                    DataBits     = 7,
                    Parity       = Parity.None,
                    StopBits     = StopBits.Two,
                    ReadTimeout  = 300,
                    WriteTimeout = 300
                };
                m_port.Open();

                m_master = ModbusSerialMaster.CreateAscii(m_port);
            }
            catch
            {
            };

            m_mutex.ReleaseMutex();
        }
Example #8
0
        /// <summary>
        /// Simple Modbus serial ASCII master read holding registers example.
        /// </summary>
        public static void ModbusSerialAsciiMasterReadRegisters()
        {
            using (SerialPort port = new SerialPort("COM1"))
            {
                // configure serial port
                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity   = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

                // create modbus master
                IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);

                byte   slaveId      = 1;
                ushort startAddress = 1;
                ushort numRegisters = 5;

                // read five registers
                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);

                for (int i = 0; i < numRegisters; i++)
                {
                    Console.WriteLine("Register {0}={1}", startAddress + i, registers[i]);
                }
            }

            // output:
            // Register 1=0
            // Register 2=0
            // Register 3=0
            // Register 4=0
            // Register 5=0
        }
Example #9
0
        public void CreateAscii_SerialPortFactoryMethod()
        {
            IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(new SerialPort());

            Assert.AreEqual(Modbus.DefaultTimeout, master.Transport.StreamResource.ReadTimeout);
            Assert.AreEqual(Modbus.DefaultTimeout, master.Transport.StreamResource.ReadTimeout);
        }
Example #10
0
        public bool EstablishConnection(MasterConnectionData connectionData)
        {
            ResetConnection();
            _serialPort          = new SerialPort();
            _serialPort.BaudRate = (int)connectionData.BaudRate;
            _serialPort.DataBits = (int)connectionData.DataBits;
            _serialPort.Parity   = connectionData.Parity;
            _serialPort.StopBits = connectionData.StopBits;
            _serialPort.PortName = connectionData.PortName;
            try
            {
                _serialPort.Open();
                _master = ModbusSerialMaster.CreateAscii(_serialPort);
                _master.Transport.ReadTimeout             = connectionData.TimeoutInMs;
                _master.Transport.Retries                 = connectionData.RetransmissionCount;
                _master.Transport.SlaveBusyUsesRetryCount = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Could not establish connection.", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(false);
            }

            return(true);
        }
 public void NModbusUsbAsciiMaster_ReadTimeout()
 {
     using (FtdUsbPort port = ModbusMasterFixture.CreateAndOpenUsbPort(ModbusMasterFixture.DefaultMasterUsbPortId))
     {
         IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
         master.ReadCoils(100, 1, 1);
     }
 }
Example #12
0
 public void NModbusAsciiMaster_ReadTimeout()
 {
     using (SerialPort port = ModbusMasterFixture.CreateAndOpenSerialPort(ModbusMasterFixture.DefaultMasterSerialPortName))
     {
         IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
         master.ReadCoils(100, 1, 1);
     }
 }
    public void NModbusAsciiMaster_ReadTimeout()
    {
        SerialPort port = ModbusMasterFixture.CreateAndOpenSerialPort(ModbusMasterFixture.DefaultMasterSerialPortName);

        using IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
        master.Transport.ReadTimeout     = master.Transport.WriteTimeout = 1000;
        Assert.Throws <TimeoutException>(() => master.ReadCoils(100, 1, 1));
    }
    public NModbusSerialAsciiMasterNModbusSerialAsciiSlaveFixture()
    {
        MasterSerialPort = CreateAndOpenSerialPort(DefaultMasterSerialPortName);
        Master           = ModbusSerialMaster.CreateAscii(MasterSerialPort);
        SetupSlaveSerialPort();
        Slave = ModbusSerialSlave.CreateAscii(SlaveAddress, SlaveSerialPort);

        StartSlave();
    }
Example #15
0
        public override void Init()
        {
            base.Init();

            StartJamodSlave(program);

            MasterSerialPort = CreateAndOpenSerialPort(DefaultMasterSerialPortName);
            Master           = ModbusSerialMaster.CreateAscii(MasterSerialPort);
        }
Example #16
0
 internal Controller(string port)
 {
     //создаем порт
     ComPort = new SerialPort(port, 115200, Parity.Even, 7, StopBits.One);
     //открываем его
     ComPort.Open();
     //создаем modbus-устройство
     PLC = ModbusSerialMaster.CreateAscii(ComPort);
 }
Example #17
0
        public void NModbusAsciiMaster_ReadTimeout()
        {
            SerialPort port = ModbusMasterFixture.CreateAndOpenSerialPort(ModbusMasterFixture.DefaultMasterSerialPortName);

            using (IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port))
            {
                master.Transport.ReadTimeout = master.Transport.WriteTimeout = 1000;
                master.ReadCoils(100, 1, 1);
            }
        }
        public void NModbusUsbAsciiMaster_ReadTimeout()
        {
            var port = ModbusMasterFixture.CreateAndOpenUsbPort(ModbusMasterFixture.DefaultMasterUsbPortId);

            using (var master = ModbusSerialMaster.CreateAscii(port))
            {
                master.Transport.ReadTimeout = master.Transport.WriteTimeout = 1000;
                master.ReadCoils(100, 1, 1);
            }
        }
        public override void Init()
        {
            base.Init();

            MasterSerialPort = CreateAndOpenSerialPort(DefaultMasterSerialPortName);
            Master           = ModbusSerialMaster.CreateAscii(MasterSerialPort);
            SetupSlaveSerialPort();
            Slave = ModbusSerialSlave.CreateAscii(SlaveAddress, SlaveSerialPort);

            StartSlave();
        }
Example #20
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            serialPort = new SerialPort();
            try
            {
                if (serialPort.IsOpen)
                {
                    timerGetData.Enabled = false;
                    master.Dispose();
                    serialPort.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            if (serialPort.IsOpen == false)
            {
                try
                {
                    serialPort.PortName = comName;
                    serialPort.BaudRate = serialPortConfig.BaudRateFromIndex;
                    serialPort.DataBits = serialPortConfig.DataBitsFromIndex;
                    serialPort.Parity   = serialPortConfig.ParityFromIndex;
                    serialPort.StopBits = serialPortConfig.StopBitsFromIndex;//一般是1位停止位
                    serialPort.Open();
                    if (protocolComboBox.SelectedIndex == 0)
                    {
                        master = ModbusSerialMaster.CreateRtu(serialPort);
                    }
                    else
                    {
                        master = ModbusSerialMaster.CreateAscii(serialPort);
                    }
                    master.Transport.Retries     = 0;
                    master.Transport.ReadTimeout = 300; //milliseconds
                    timerGetData.Enabled         = true;
                    labelStatus.Text             = "端口打开成功!";
                    labelConnect.Text            = "已连接";
                    pictureBoxConnect.BackColor  = Color.Lime;
                    serialPortConfig.PortIsOpen  = true;
                    //timerDraw.Enabled = true;
                    //timerSave.Enabled = true;
                    MessageBox.Show("串口打开成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"串口打开失败!\r\n可能原因:{ex.Message}");
                }
            }
            btnConnect.Text = serialPort.IsOpen ? "关闭" : "打开";
        }
Example #21
0
        public void CreateAscii_UsbPortFactoryMethod()
        {
            MockRepository  mocks = new MockRepository();
            IStreamResource mockSerialResource = mocks.StrictMock <IStreamResource>();

            Expect.Call(mockSerialResource.WriteTimeout).Return(0);
            Expect.Call(mockSerialResource.InfiniteTimeout).Return(0);
            mockSerialResource.WriteTimeout = Modbus.DefaultTimeout;
            Expect.Call(mockSerialResource.ReadTimeout).Return(0);
            Expect.Call(mockSerialResource.InfiniteTimeout).Return(0);
            mockSerialResource.ReadTimeout = Modbus.DefaultTimeout;

            mocks.ReplayAll();
            ModbusSerialMaster.CreateAscii(mockSerialResource);
            mocks.VerifyAll();
        }
Example #22
0
        private void WriteRegisters(ushort startAddress, ushort registerValue)
        {
            CheckPort();

            try
            {
                if (!port.IsOpen)
                {
                    port.Open();
                }
                IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
                master.Transport.WriteTimeout = timeout;
                master.WriteSingleRegister(slaveId, startAddress, registerValue);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #23
0
        /// <summary>
        ///     Modbus serial ASCII master and slave example.
        /// </summary>
        public static void ModbusSerialAsciiMasterReadRegistersFromModbusSlave()
        {
            using (SerialPort masterPort = new SerialPort("COM1"))
                using (SerialPort slavePort = new SerialPort("COM2"))
                {
                    // configure serial ports
                    masterPort.BaudRate = slavePort.BaudRate = 9600;
                    masterPort.DataBits = slavePort.DataBits = 8;
                    masterPort.Parity   = slavePort.Parity = Parity.None;
                    masterPort.StopBits = slavePort.StopBits = StopBits.One;
                    masterPort.Open();
                    slavePort.Open();

                    var slaveAdapter = new SerialPortAdapter(slavePort);
                    // create modbus slave on seperate thread
                    byte        slaveId    = 1;
                    ModbusSlave slave      = ModbusSerialSlave.CreateAscii(slaveId, slaveAdapter);
                    var         listenTask = slave.ListenAsync();

                    var masterAdapter = new SerialPortAdapter(masterPort);
                    // create modbus master
                    ModbusSerialMaster master = ModbusSerialMaster.CreateAscii(masterAdapter);

                    master.Transport.Retries = 5;
                    ushort startAddress = 100;
                    ushort numRegisters = 5;

                    // read five register values
                    ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);

                    for (int i = 0; i < numRegisters; i++)
                    {
                        Console.WriteLine($"Register {(startAddress + i)}={registers[i]}");
                    }
                }

            // output
            // Register 100=0
            // Register 101=0
            // Register 102=0
            // Register 103=0
            // Register 104=0
        }
Example #24
0
        private void uartportcontrl_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)uartportcontrl.IsChecked)
            {
                try
                {
                    serialPort.PortName = uartname.SelectedItem.ToString();
                    serialPort.BaudRate = 115200;
                    serialPort.DataBits = 8;
                    serialPort.Parity   = Parity.None;
                    serialPort.StopBits = StopBits.One;
                    serialPort.Open();

                    imodbusserialmaster = ModbusSerialMaster.CreateAscii(serialPort);

                    uartname.IsEnabled  = false;
                    buteRaate.IsEnabled = false;
                    addmsg($">> 打开串口成功!");
                }
                catch (Exception ex)
                {
                    uartportcontrl.IsChecked = false;
                    uartname.IsEnabled       = true;
                    buteRaate.IsEnabled      = true;
                    addmsg($"ERROR:打开串口失败!{ex.ToString()}");
                }
            }
            else
            {
                try
                {
                    serialPort.Close();
                    imodbusserialmaster.Dispose();
                    uartname.IsEnabled  = true;
                    buteRaate.IsEnabled = true;
                    addmsg($">> 关闭串口成功!");
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show($"关闭串口失败,请插拔串口,并重启软件! {ex.ToString()}");
                }
            }
        }
        public void openComm()
        {
            lock (locker)
            {
                if (port != null && !port.IsOpen)
                {
                    port.Open();

                    // create modbus master
                    if (OutputModbusType == "RTU")
                    {
                        master = ModbusSerialMaster.CreateRtu(port);
                    }
                    else
                    {
                        master = ModbusSerialMaster.CreateAscii(port);
                    }
                }
            }
        }
Example #26
0
        /// <summary>
        /// Simple Modbus serial USB ASCII master write multiple coils example.
        /// </summary>
        public static void ModbusSerialUsbAsciiMasterWriteCoils()
        {
            using (FtdUsbPort port = new FtdUsbPort())
            {
                // configure usb port
                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity   = FtdParity.None;
                port.StopBits = FtdStopBits.One;
                port.OpenByIndex(0);

                // create modbus master
                IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);

                byte   slaveId      = 1;
                ushort startAddress = 1;

                // write three coils
                master.WriteMultipleCoils(slaveId, startAddress, new bool[] { true, false, true });
            }
        }
Example #27
0
        private void WriteRegisters(ushort startAddress, float registerValue)
        {
            CheckPort();

            try
            {
                if (!port.IsOpen)
                {
                    port.Open();
                }
                IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
                var    registers           = new ushort[4];
                byte[] bytereg             = BitConverter.GetBytes(registerValue);
                registers[0] = BitConverter.ToUInt16(bytereg, 0);
                registers[1] = BitConverter.ToUInt16(bytereg, 2);

                master.WriteMultipleRegisters(slaveId, startAddress, registers);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #28
0
        public bool Open()
        {
            if (mPort != null)
            {
                return(false);
            }

            try
            {
                mPort = new SerialPort(mPortName, mBaudrate, Parity.None, 8, StopBits.One)
                {
                    ReadTimeout = ReadTimeout
                };
                mPort.Open();

                if (mPort.IsOpen)
                {
                    mConnection = mIsRtu ? ModbusSerialMaster.CreateRtu(mPort) : ModbusSerialMaster.CreateAscii(mPort);
                    mConnection.Transport.Retries     = Retries; // 0,  was 1
                    mConnection.Transport.ReadTimeout = ReadTimeout;

                    mIsFirstError = true;
                    return(true);
                }
            }
            catch (IOException)
            {
                if (mIsFirstError)
                {
                    mJournal.Error("Не могу открыть посл. порт " + mPortName, MessageLevel.System);
                }

                if (mPort != null)
                {
                    if (mPort.IsOpen)
                    {
                        mPort.Close();
                    }
                }

                mPort         = null;
                mIsFirstError = false;
            }
            catch (ArgumentException)
            {
                if (mIsFirstError)
                {
                    mJournal.Error("Не могу открыть посл. порт " + mPortName, MessageLevel.System);
                }

                if (mPort != null)
                {
                    if (mPort.IsOpen)
                    {
                        mPort.Close();
                    }
                }

                mPort         = null;
                mIsFirstError = false;
            }
            catch (UnauthorizedAccessException)
            {
                if (mIsFirstError)
                {
                    mJournal.Error("Не хватает прав для записи в посл. порт " + mPortName, MessageLevel.System);
                }

                if (mPort != null)
                {
                    if (mPort.IsOpen)
                    {
                        mPort.Close();
                    }
                }

                mPort         = null;
                mIsFirstError = false;
            }
            catch (NullReferenceException)
            {
                if (mIsFirstError)
                {
                    mJournal.Error("Не указано имя посл. порта", MessageLevel.System);
                }

                if (mPort != null)
                {
                    if (mPort.IsOpen)
                    {
                        mPort.Close();
                    }
                }

                mPort         = null;
                mIsFirstError = false;
            }

            return(false);
        }
Example #29
0
        private bool transResultToServer(byte[] ascii)
        {
            try
            {
                Configure cfg     = null;
                string    cfgfile = System.IO.Path.Combine(Application.StartupPath, "cfg.json");
                if (File.Exists(cfgfile))
                {
                    cfg = JsonConvert.DeserializeObject <Configure>(File.ReadAllText(cfgfile));
                    if (cfg != null)
                    {
                        if (cfg.OutoutMethod == "串口")
                        {
                            if (cfg.OutputSerialPortName.Length == 0)
                            {
                                return(true);
                            }

                            if (cfg.bGetDataOnload && inputCommPortSingleton.GetInstance().checkSerialPort(cfg.OutputSerialPortName))
                            {
                                using (SerialPort masterPort = new SerialPort(cfg.OutputSerialPortName))
                                {
                                    // configure serial ports

                                    masterPort.BaudRate = (int)GetNumber(cfg.OutputSerialPortBaud);
                                    masterPort.DataBits = (int)GetNumber(cfg.OutputSerialPortDataBit);
                                    if (cfg.OutputSerialPortParity == "None Parity")
                                    {
                                        masterPort.Parity = Parity.None;
                                    }
                                    else if (cfg.OutputSerialPortParity == "Odd Parity")
                                    {
                                        masterPort.Parity = Parity.Odd;
                                    }
                                    else
                                    {
                                        masterPort.Parity = Parity.Even;
                                    }

                                    if (cfg.OutputSerialPortStopBit == "1 Stop Bit")
                                    {
                                        masterPort.StopBits = StopBits.One;
                                    }
                                    else
                                    {
                                        masterPort.StopBits = StopBits.Two;
                                    }

                                    masterPort.ReadTimeout  = 1000;
                                    masterPort.WriteTimeout = 1000;

                                    masterPort.Open();

                                    // create modbus master
                                    ModbusSerialMaster master = ModbusSerialMaster.CreateAscii(masterPort);

                                    master.Transport.Retries = 5;
                                    ushort   startAddress = 0x08;
                                    ushort[] data         = new ushort[ascii.Length / sizeof(short)];
                                    Buffer.BlockCopy(ascii, 0, data, 0, data.Length * sizeof(short));

                                    master.WriteMultipleRegisters(1, startAddress, data);
                                    // read five register values
                                    //ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
                                }
                            }
                        }
                        else
                        {
                            using (TcpClient client = new TcpClient(cfg.ServerIp, (int)GetNumber(cfg.ServerPort)))
                            {
                                ModbusSerialMaster master = ModbusSerialMaster.CreateAscii(client);

                                master.Transport.Retries = 5;
                                ushort   startAddress = 0x08;
                                ushort[] data         = new ushort[ascii.Length / sizeof(short)];
                                Buffer.BlockCopy(ascii, 0, data, 0, data.Length * sizeof(short));
                                client.SendTimeout    = 1000;
                                client.ReceiveTimeout = 1000;
                                master.WriteMultipleRegisters(1, startAddress, data);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogClass.GetInstance().WriteExceptionLog(ex);
                //MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(true);
        }
Example #30
0
        protected override void Start()
        {
            if (master != null)
            {
                master.Dispose();
            }
            if (_serialPort != null)
            {
                _serialPort.Close();
                _serialPort.Dispose();
                _serialPort = null;
            }

            try
            {
                byte[] array = new byte[8];

                #region
                {
                    InitSerialPort(ref _serialPort);



                    if (Serial_PARA.ModbusType == ModbusType.ASCII)
                    {
                        master = ModbusSerialMaster.CreateAscii(_serialPort);
                    }
                    else
                    {
                        master = ModbusSerialMaster.CreateRtu(_serialPort);
                    }
                    //通用部分设置
                    master.Transport.ReadTimeout             = Serial_PARA.ReadTimeout;           //读取数据超时1000ms
                    master.Transport.WriteTimeout            = Serial_PARA.WriteTimeout;          //写入数据超时1000ms
                    master.Transport.Retries                 = Serial_PARA.CollectFaultsNumber;   //重试次数
                    master.Transport.WaitToRetryMilliseconds = Serial_PARA.CollectFaultsInternal; //重试间隔

                    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;
                                }

                                try
                                {
                                    Task.Run(() =>
                                    {
                                        //发送获取数据的命令
                                        string error = "";
                                        if (!this.RequestData(mRealData.Device, mRealData, out error, mRealData.Fragment))
                                        {
                                            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 * 1000);
                            }
                        });
                    }

                    this.CommunctionStartChanged(this.IOServer, this.IOServer.SERVER_IP + " " + this.IOServer.SERVER_NAME + "启动服务");
                }
                #endregion
            }
            catch
            {
            }
        }