Exemplo n.º 1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">Type of serial protocol</param>
        /// <param name="port">Serial port name</param>
        /// <param name="baudrate">Baudrate</param>
        /// <param name="databits">Data bits</param>
        /// <param name="parity">Parity</param>
        /// <param name="stopbits">Stop bits</param>
        /// <param name="handshake">Handshake</param>
        public ModbusMasterSerial(ModbusSerialType type, string port, int baudrate, int databits, Parity parity, StopBits stopbits, Handshake handshake)
        {
            // Set device states
            switch (type)
            {
            case ModbusSerialType.RTU:
                _connectionType = ConnectionType.SerialRTU;
                break;

            case ModbusSerialType.ASCII:
                _connectionType = ConnectionType.SerialASCII;
                break;
            }

            // Set serial port instance.
            _serialPort = new SerialPort(port, baudrate, parity, databits, stopbits)
            {
                Handshake = handshake
            };

            // Get interframe delay.
            _interframeDelay = GetInterframeDelay(_serialPort);

            // Get interchar delay
            _intercharDelay = GetIntercharDelay(_serialPort);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="modbus_db">Modbus database</param>
        /// <param name="type">Type of serial modbus protocol (RTU or ASCII)</param>
        /// <param name="port">Serial port name</param>
        /// <param name="baudrate">Baudrate</param>
        /// <param name="databits">Data bits</param>
        /// <param name="parity">Parity</param>
        /// <param name="stopbits">Stop bits</param>
        /// <param name="handshake">Control flux</param>
        public ModbusSlaveSerial(Datastore[] modbus_db, ModbusSerialType type, string port, int baudrate, int databits, Parity parity, StopBits stopbits, Handshake handshake)
            : base(modbus_db)
        {
            // Set modbus serial protocol type
            switch (type)
            {
            case ModbusSerialType.ASCII:
                _connectionType = ConnectionType.SERIAL_ASCII;
                break;

            case ModbusSerialType.RTU:
                _connectionType = ConnectionType.SERIAL_RTU;
                break;
            }
            // Set serial port instance
            _serialPort           = new SerialPort(port, baudrate, parity, databits, stopbits);
            _serialPort.Handshake = handshake;
            // Calc interframe delay
            _interframeDelay = GetInterframeDelay(_serialPort);
            // Calc interchar delay
            _intercharDelay = GetIntercharDelay(_serialPort);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">Type of serial protocol</param>
        /// <param name="port">Serial port name</param>
        /// <param name="baudrate">Baudrate</param>
        /// <param name="databits">Data bits</param>
        /// <param name="parity">Parity</param>
        /// <param name="stopbits">Stop bits</param>
        /// <param name="handshake">Handshake</param>
        public ModbusMasterSerial(ModbusSerialType type, string port, int baudrate, int databits, Parity parity, StopBits stopbits, Handshake handshake)
        {
            // Set device states
            switch (type)
            {
                case ModbusSerialType.RTU:
                    connection_type = ConnectionType.SERIAL_RTU;
                    break;

                case ModbusSerialType.ASCII:
                    connection_type = ConnectionType.SERIAL_ASCII;
                    break;
            }
            // Set serial port instance
            sp = new SerialPort(port, baudrate, parity, databits, stopbits);
            sp.Handshake = handshake;
            // Get interframe delay
            interframe_delay = GetInterframeDelay(sp);
            // Get interchar delay
            interchar_delay = GetIntercharDelay(sp);
        }
Exemplo n.º 4
0
        /// <summary>
        /// When the user selects a radio button, open or close the serial port.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            // Do stuff only if the radio button is checked.
            // (Otherwise the actions will run twice.)
            if (((RadioButton)sender).Checked)
            {
                try
                {
                    // If the "Open" radio button has been checked...
                    if (((RadioButton)sender) == radioButtonOpen)
                    {
                        // Alert the user.
                        toolStripStatusLabel1.Text = "Opening serial port...";

                        // Fetch the selected serial port.
                        string portName = comboBoxSerialPort.SelectedItem.ToString();
                        if (string.IsNullOrEmpty(portName))
                        {
                            throw new ArgumentException("Serial Port not selected.");
                        }

                        // Fetch the selected baud rate.
                        if (int.TryParse(comboBoxBaudRate.SelectedItem.ToString(), out int baudRate) == false)
                        {
                            throw new ArgumentException("Invalid baud rate.");
                        }

                        // Get the selected parity mode.
                        string parityName = comboBoxParity.SelectedItem.ToString();
                        if (string.IsNullOrEmpty(parityName))
                        {
                            throw new ArgumentException("Parity type not selected");
                        }
                        Parity parity = Parity.Even;
                        if (parityName == "None")
                        {
                            parity = Parity.None;
                        }
                        else if (parityName == "Odd")
                        {
                            parity = Parity.Odd;
                        }

                        // Get the selected MODBUS mode (ASCII or RTU).
                        string modeString = comboBoxMode.SelectedItem.ToString();
                        if (modeString == null)
                        {
                            throw new ArgumentException("Modbus Mode not selected");
                        }
                        ModbusSerialType mode = ModbusSerialType.RTU;
                        if (modeString == "ASCII")
                        {
                            mode = ModbusSerialType.ASCII;
                        }

                        // Select the MODBUS packet size based on the selected mode.
                        int datasize = 8;
                        if (mode == ModbusSerialType.ASCII)
                        {
                            datasize = 7;
                        }

                        // Get the selected number of stop bits.
                        object stopBits = comboBoxStopBits.SelectedItem;
                        if (stopBits == null)
                        {
                            throw new ArgumentException("Stop Bits not selected");
                        }

                        // Convert stop bits string to comm port setting.
                        StopBits stopbits = StopBits.One;
                        if (stopBits.ToString() == "2")
                        {
                            stopbits = StopBits.Two;
                        }

                        // Create and open serial port.
                        _mbMaster = new ModbusMasterSerial(mode, portName.ToString(), baudRate, datasize, parity, stopbits, Handshake.None);
                        _mbMaster.Connect();

                        // Disable the serial port controls.
                        buttonPortRefresh.Enabled  = false;
                        comboBoxSerialPort.Enabled = false;
                        comboBoxBaudRate.Enabled   = false;
                        comboBoxParity.Enabled     = false;
                        comboBoxMode.Enabled       = false;
                        comboBoxStopBits.Enabled   = false;

                        // Enable the MODBUS communication controls.
                        groupBoxAddress.Enabled = true;
                        groupBoxData.Enabled    = true;

                        // Update the status bar.
                        toolStripStatusLabel1.Text = "Port open.";
                    }
                    else if (((RadioButton)sender) == radioButtonClosed)
                    {
                        // Alert the user.
                        toolStripStatusLabel1.Text = "Closing serial port...";

                        // Dispose of the MODBUS class.
                        _mbMaster?.Disconnect();

                        // Enable the serial port controls.
                        buttonPortRefresh.Enabled  = true;
                        comboBoxSerialPort.Enabled = true;
                        comboBoxBaudRate.Enabled   = true;
                        comboBoxParity.Enabled     = true;
                        comboBoxMode.Enabled       = true;
                        comboBoxStopBits.Enabled   = true;

                        // Disable the MODBUS communication controls.
                        groupBoxAddress.Enabled = false;
                        groupBoxData.Enabled    = false;

                        // Update the status bar.
                        toolStripStatusLabel1.Text = "Port closed.";
                    }
                }
                // If an error occurs...
                catch (Exception ex)
                {
                    // Alert the user.
                    MessageBox.Show(ex.Message, ex.GetType().Name.ToString());

                    // Undo the user action.
                    radioButtonClosed.Checked = true;
                }
            }
        }
Exemplo n.º 5
-1
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="modbus_db">Modbus database</param>
        /// <param name="type">Type of serial modbus protocol (RTU or ASCII)</param>
        /// <param name="port">Serial port name</param>
        /// <param name="baudrate">Baudrate</param>
        /// <param name="databits">Data bits</param>
        /// <param name="parity">Parity</param>
        /// <param name="stopbits">Stop bits</param>
        /// <param name="handshake">Control flux</param>
        public ModbusSlaveSerial(Datastore[] modbus_db, ModbusSerialType type, string port, int baudrate, int databits, Parity parity, StopBits stopbits, Handshake handshake)
            : base(modbus_db)
        {
            // Set modbus serial protocol type
            switch (type)
            {
                case ModbusSerialType.ASCII:
                    connection_type = ConnectionType.SERIAL_ASCII;
                    break;

                case ModbusSerialType.RTU:
                    connection_type = ConnectionType.SERIAL_RTU;
                    break;
            }
            // Set serial port instance
            sp = new SerialPort(port, baudrate, parity, databits, stopbits);
            sp.Handshake = handshake;
            // Calc interframe delay
            interframe_delay = GetInterframeDelay(sp);
            // Calc interchar delay
            interchar_delay = GetIntercharDelay(sp);
        }