Menu

modbusClient.ReadCoils

Anonymous
2016-12-28
2017-01-07
  • Anonymous

    Anonymous - 2016-12-28

    I want to perform the status of a digital input, but the value that returns is false.

    Coil value # I1: is always True

    The sample code:

    using System;
    using EasyModbus;
    
    namespace ModbusRS485Master
    {
        class Program
        {
            public static void Main(string[] args)
            {
                ModbusClient modbusClient = new ModbusClient("COM3");
                modbusClient.UnitIdentifier = 1;// Not necessary since default slaveID = 1;
                modbusClient.Baudrate = 38400;  // Not necessary since default baudrate = 9600
                modbusClient.Parity = System.IO.Ports.Parity.None;
                modbusClient.StopBits = System.IO.Ports.StopBits.Two;
                modbusClient.ConnectionTimeout = 3000;
                modbusClient.Connect();
                string teste = "";
                while (teste != "false")
                {
                    //Console.WriteLine("Value of Discr. Input #1: " + modbusClient.ReadDiscreteInputs(11264, 1)[0].ToString());  //Reads Discrete Input #1
                    //Console.WriteLine("Value of Input Reg. #10: " + modbusClient.ReadInputRegisters(11264, 1)[0].ToString());   //Reads Inp. Reg. #10
    
                      modbusClient.WriteSingleCoil(11136, true);      // Writes Coil #5
                                                                      // modbusClient.WriteSingleRegister(19, 4711); //Writes Holding Reg. #20
                      teste = modbusClient.ReadCoils(11264,1)[0].ToString();
    
                      Console.WriteLine("Value of Coil # I1: " + modbusClient.ReadCoils(11264,1)[0].ToString());
    
                }
                Console.Write("Press any key to continue . . . ");
                Console.ReadKey(true);
            }
        }
    }
    
     
    • Rossmann Engineering

      Hi!

      I was just trying, and it worked perfectly. Are you sure about connection parameter (Baudrate, Parity, stopbits)?

      But your while loop does not work, because you are casting a bool value to a string, and then compare. But the .ToString returns "False" if the coil turned to zero (Note the Capital letter "F".

      But it would be better to compare the "bool" value:

      bool teste = true;
      while (teste != false)
      {
      ......
      teste = modbusClient.ReadCoils(11264,1)[0];
      }

       

Anonymous
Anonymous

Add attachments
Cancel