Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        public void Action_Jog(byte unit_id, ushort axis, ushort pos)
        {
            if (axis > DEF_MAX_AXIS)
            {
                return;
            }

            ushort address = Convert.ToUInt16(REG_HOLDING_START + eMbData.DEF_INX_TARPOS1 + axis - 1);

            mm.WriteSingleRegister(unit_id, address, pos);

            Thread.Sleep(10);

            address = Convert.ToUInt16(REG_HOLDING_START + eMbData.DEF_INX_OPMODE);
            mm.WriteSingleRegister(unit_id, address, Convert.ToUInt16(eOPMode.JOG));
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 5
0
 private void up_Click(object sender, EventArgs e)
 {
     up.Select();
     try
     {
         mm.WriteSingleRegister(1, MB_UP, (ushort)BUTTON_ON);
     }
     catch (Exception) { }
 }