Beispiel #1
0
        /// <summary>
        /// Test modbus RTU master function on a slave RTU id = 5
        /// </summary>
        private static void Test_ModbusRTUMaster()
        {
            byte unit_id = 5;
            // Create instance of modbus serial RTU (replace COMx with a free serial port - ex. COM5).
            ModbusMasterSerial mm = new ModbusMasterSerial(ModbusSerialType.RTU, "COM20", 9600, 8, Parity.Even, StopBits.One, Handshake.None);

            // Initialize the MODBUS connection.
            mm.Connect();

            // Read and write some registers on RTU n. 5.
            Random rnd = new Random();

            while (true)
            {
                // Print some nice formatting to the console.
                Console.WriteLine("---------------------- READING ----------------------");

                // Read from a register.
                ushort inputReg1 = mm.ReadHoldingRegisters(unit_id, 0, 1).First();

                // Print the result to the console.
                Console.WriteLine("Holding register n.1  : " + inputReg1.ToString("D5"));

                // Read from another register.
                ushort inputReg41 = mm.ReadInputRegisters(unit_id, 40, 1).First();

                // Print the result to the console.
                Console.WriteLine("Input register   n.41 : " + inputReg41.ToString("D5"));

                // Read from a coil.
                bool coil23 = mm.ReadCoils(unit_id, 22, 1).First();

                // Print the results to the console.
                Console.WriteLine("Coil register    n 23 : " + coil23.ToString());

                // Print some nice formatting to the console.
                Console.WriteLine("---------------------- WRITING ----------------------");

                // Write to a register.
                mm.WriteSingleRegister(unit_id, 4, (ushort)rnd.Next(ushort.MinValue, ushort.MaxValue));

                // ...then read it back.
                ushort holdingReg4 = mm.ReadHoldingRegisters(unit_id, 4, 1).First();

                // Print the result to the console.
                Console.WriteLine("Holding register n.5  : " + holdingReg4.ToString("D5") + Environment.NewLine);

                // Write to another register.
                mm.WriteSingleCoil(unit_id, 2, Convert.ToBoolean(rnd.Next(0, 1)));

                // ...then read it back.
                bool holdingReg3 = mm.ReadCoils(unit_id, 2, 1).First();

                // Print the result to the console.
                Console.WriteLine("Coil register    n.3  : " + holdingReg3.ToString() + Environment.NewLine);

                // Repeat every 2 seconds.
                Thread.Sleep(2000);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Test modbus RTU master function on a slave RTU id = 5
        /// </summary>
        static void Test_ModbusRTUMaster()
        {
            byte unit_id = 5;
            // Crete instance of modbus serial RTU (replace COMx with a free serial port - ex. COM5)
            ModbusMasterSerial mm = new ModbusMasterSerial(ModbusSerialType.RTU, "COM1", 9600, 8, Parity.Even, StopBits.One, Handshake.None);

            // Exec the connection
            mm.Connect();
            // Read and write some registers on RTU n. 5
            Random rnd = new Random();

            while (true)
            {
                Console.Write(
                    "---------------------- READING ----------------------" + Environment.NewLine +
                    "Holding register n.1  : " + mm.ReadHoldingRegisters(unit_id, 0, 1).First().ToString("D5") + Environment.NewLine +
                    "Input register   n.41 : " + mm.ReadInputRegisters(unit_id, 40, 1).First().ToString("D5") + Environment.NewLine +
                    "Coil register    n 23 : " + mm.ReadCoils(unit_id, 22, 1).First().ToString() + Environment.NewLine +
                    "---------------------- WRITING ----------------------" + Environment.NewLine);
                mm.WriteSingleRegister(unit_id, 4, (ushort)rnd.Next(ushort.MinValue, ushort.MaxValue));
                Console.WriteLine(
                    "Holding register n.5  : " + mm.ReadHoldingRegisters(unit_id, 4, 1).First().ToString("D5") + Environment.NewLine);
                mm.WriteSingleCoil(unit_id, 2, Convert.ToBoolean(rnd.Next(0, 1)));
                Console.WriteLine(
                    "Coil register    n.3  : " + mm.ReadCoils(unit_id, 2, 1).First().ToString() + Environment.NewLine);
                // Exec the cicle each 2 seconds
                Thread.Sleep(2000);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Read Input Registers.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonInputRegisterRead_Click(object sender, EventArgs e)
        {
            try
            {
                // Get the selected device address.
                byte slaveAddr = GetDeviceAddress();

                // If the register address text box is empty...
                if (string.IsNullOrWhiteSpace(textBoxInputRegisterAddressDecimal.Text))
                {
                    // Convert from hex.
                    TextBoxInputRegisterAddressHex_Leave(null, null);
                }

                // Get the selected register address.
                ushort regAddr = GetRegisterAddress(textBoxInputRegisterAddressDecimal.Text);

                // Get the selected data format.
                DataType dataType = GetDataFormat(comboBoxInputRegisterFormat);

                // Calculate the number of registers to read.
                ushort numRegs = 2;
                if (dataType == DataType.String)
                {
                    numRegs = GetNumRegisters(textBoxInputRegisterStringLength.Text);
                }

                // Read from the device.
                ushort[] regValues = _mbMaster.ReadInputRegisters(slaveAddr, regAddr, numRegs);

                // Format and display the data.
                textBoxInputRegisterData.Text = RegistersToString(dataType, regValues);

                // Update the status.
                toolStripStatusLabel1.Text = "Input Register Read Successfully";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().Name.ToString());
            }
        }
Beispiel #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            byte unit_id = 1;
            // Crete instance of modbus serial RTU (replace COMx with a free serial port - ex. COM5)
            ModbusMasterSerial mm = new ModbusMasterSerial(ModbusSerialType.RTU, "COM1", 9600, 8, Parity.Even, StopBits.One, Handshake.None);

            // Exec the connection
            mm.Connect();
            // Read and write some registers on RTU n. 5
            Random rnd = new Random();

            while (true)
            {
                textBox1.Text = "Holding register n.1  : " + mm.ReadHoldingRegisters(unit_id, 0, 1).First().ToString("D5") +
                                "Input register   n.41 : " + mm.ReadInputRegisters(unit_id, 40, 1).First().ToString("D5") +
                                "Coil register    n 23 : " + mm.ReadCoils(unit_id, 22, 1).First().ToString();
                mm.WriteSingleRegister(unit_id, 4, (ushort)rnd.Next(ushort.MinValue, ushort.MaxValue));
                textBox2.Text = "Holding register n.5  : " + mm.ReadHoldingRegisters(unit_id, 4, 1).First().ToString("D5");
                mm.WriteSingleCoil(unit_id, 2, Convert.ToBoolean(rnd.Next(0, 1)));
                textBox3.Text = "Coil register    n.3  : " + mm.ReadCoils(unit_id, 2, 1).First().ToString();
                // Exec the cicle each 2 seconds
                Thread.Sleep(2000);
            }
        }