Ejemplo n.º 1
0
        /// <summary>
        /// Create&amp;Return a Rtu Master host;
        /// </summary>
        /// <returns></returns>
        public override IModbusMaster CreateRtuMaster(string portName, int baudrate = 115200, int dataBits = 8, StopBits stopbits = StopBits.One, Parity parity = Parity.None)
        {
            port = new SerialPort
            {
                BaudRate = baudrate,
                DataBits = 8,
                Parity   = parity,
                StopBits = StopBits.One,
                PortName = portName
            };
            port.Open();

            Master = ModbusSerialMaster.CreateRtu(port);
            return(Master);
        }
Ejemplo n.º 2
0
    public MeasureForm(int _devno, SerialPort _sp, TcpClient _tc, Sensor _ts)
    {
        InitializeComponent();
        SettingButton.Click  += SettingButton_Click;
        ExecButton.Click     += ExecButton_Click;
        UpdateButton.Click   += UpdateButton_Click;
        CoilsButton.Click    += CoilsButton_Click;
        DescriteButton.Click += DescriteButton_Click;
        UptimeButton.Click   += UptimeButton_Click;
        RawButton.Click      += RawButton_Click;
        CyclicPeriod.Minimum  = 100m;
        CyclicPeriod.Maximum  = 10000m;
        CyclicPeriod.Value    = 1000m;
        fs = null;
        sw = null;
        DataGridView dataGridView = dataGridView1;

        dataGridView.AutoSize              = true;
        dataGridView.Anchor                = (AnchorStyles.Top | AnchorStyles.Left);
        dataGridView.RowHeadersVisible     = false;
        dataGridView.AllowUserToAddRows    = false;
        dataGridView.MultiSelect           = false;
        dataGridView.SelectionMode         = DataGridViewSelectionMode.CellSelect;
        dataGridView.ColumnCount           = 2;
        dataGridView.Columns[0].HeaderText = "Item";
        dataGridView.Columns[0].SortMode   = DataGridViewColumnSortMode.NotSortable;
        dataGridView.Columns[1].HeaderText = "Value";
        dataGridView.Columns[1].SortMode   = DataGridViewColumnSortMode.NotSortable;
        TargetSensor = _ts;
        TargetSensor.SensorComponent(dataGridView);
        devno = _devno;
        if (_sp != null)
        {
            Tc = null;
            Sp = _sp;
            Sp.Open();
            Master = ModbusSerialMaster.CreateRtu(_sp);
        }
        else
        {
            Sp     = null;
            Tc     = _tc;
            Master = ModbusIpMaster.CreateIp(Tc);
        }
        Master.Transport.ReadTimeout       = 300;
        dataGridView.CurrentCell           = dataGridView[1, 0];
        dataGridView.AllowUserToResizeRows = false;
    }
Ejemplo n.º 3
0
 private void buttonConnect_Click(object sender, EventArgs e)
 {
     serialPortModbus.PortName = comboBoxSerial.Text;
     if (!serialPortModbus.IsOpen)
     {
         serialPortModbus.Open();
         buttonConnect.Enabled    = false;
         buttonDisconnect.Enabled = true;
         modbusMasterRTU          = ModbusSerialMaster.CreateRtu(serialPortModbus);
         timerGetData.Enabled     = true;
     }
     else
     {
         MessageBox.Show("Failed To Open Port");
     }
 }
Ejemplo n.º 4
0
        private IModbusSerialMaster GetMaster()
        {
            var port = ((KeyValuePair <string, SerialPort>)cbxSerialPort.SelectedItem).Value;

            port.BaudRate = Convert.ToInt16(cbxComBaudRate.Text);
            port.Parity   = Parity.None;
            port.DataBits = 8;
            port.StopBits = StopBits.One;
            if (!port.IsOpen)
            {
                port.Open();
            }
            IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);

            return(master);
        }
Ejemplo n.º 5
0
        public static int InitRs485(string PortName, int BaudRate = 9600)
        {
            if (serialPort == null)
            {
                return(-1);
            }

            //open port
            if (!serialPort.IsOpen)
            {
                if (!PortName.ToUpper().Contains("COM") || BaudRate == 0)
                {
                    return(-2);
                }
                try
                {
                    serialPort.PortName = PortName;
                    serialPort.BaudRate = BaudRate;
                    serialPort.StopBits = StopBits.One;
                    serialPort.Parity   = Parity.Even;
                    serialPort.Open();
                }
                catch (Exception ex)
                {
                    //throw ex;
                    return(-1);
                }
            }
            //create rtu
            if (serialPort.IsOpen && (master == null || master.Transport == null))
            {
                try
                {
                    master = ModbusSerialMaster.CreateRtu(serialPort);
                    master.Transport.ReadTimeout             = 50;
                    master.Transport.WriteTimeout            = 50;
                    master.Transport.Retries                 = 3;
                    master.Transport.WaitToRetryMilliseconds = 10;
                }
                catch (Exception ex)
                {
                    return(-2);
                    //throw ex;
                }
            }
            return(0);
        }
        /// <summary>
        /// RTU master create connection to serial port
        ///     (<paramref name="serialPort"/>,<paramref name="slaveAddress"/>).
        /// </summary>
        /// <param name="serialPort">A serial port resource.</param>
        /// <param name="slaveAddress">Address of device to read value from.</param>
        public ModbusPortCommunication(string serialPortName = "COM13", byte slaveAddress = 1)
        {
            InitializeSerialPort(serialPortName);

            master = ModbusSerialMaster.CreateRtu(serialPort);

            // Number of times to retry sending message after encountering a failure
            // such as an IOException, TimeoutException, or a corrupt message.
            // Default Value is Retries = 3.
            master.Transport.Retries = 0;

            // Gets or sets the number of milliseconds before a timeout occurs
            // when a read operation does not finis recommended value is ReadTimeout = 300).
            master.Transport.ReadTimeout = 100;

            this.slaveAddress = slaveAddress;
        }
Ejemplo n.º 7
0
        public void CreateRtu_UsbPortFactoryMethod()
        {
            MockRepository  mocks = new MockRepository();
            IStreamResource mockStreamResource = mocks.StrictMock <IStreamResource>();

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

            mocks.ReplayAll();
            var master = ModbusSerialMaster.CreateRtu(mockStreamResource);

            mocks.VerifyAll();
        }