Пример #1
0
 /// <summary>
 /// Write Coils (function 15)
 /// </summary>
 /// <param name="address">First Coil address</param>
 /// <param name="coils">Coils Value</param>
 public void WriteCoils(ushort address, bool[] coils)
 {
     try
     {
         cmd.function   = 15;
         cmd.regAddress = address;
         cmd.length     = (ushort)coils.Length;
         int paramLen = cmd.length / 8;
         if (cmd.length % 8 > 0)
         {
             paramLen++;
         }
         cmd.param = new ushort[paramLen];
         for (int i = 0; i < cmd.length; i++)
         {
             cmd.param[i / 8] += (byte)(Convert.ToByte(coils[i]) << (i % 8));
         }
         sendAndWait();
     }
     catch (Exception err)
     {
         CommException Error = new CommException(err.Message, packetSended, packetReceived);
         throw Error;
     }
 }
Пример #2
0
        /// <summary>
        /// Write Coil (function 5)
        /// </summary>
        /// <param name="address">Coil address</param>
        /// <param name="coil">Coil Value</param>
        public void WriteCoil(ushort address, bool coil)
        {
            try
            {
                cmd.function   = 5;
                cmd.regAddress = address;
                cmd.length     = 1;
                cmd.param      = new ushort[1];
                if (coil)
                {
                    cmd.param[0] = 1;
                }
                else
                {
                    cmd.param[0] = 0;
                }

                sendAndWait();
            }
            catch (Exception err)
            {
                CommException Error = new CommException(err.Message, packetSended, packetReceived);
                throw Error;
            }
        }
Пример #3
0
 /// <summary>
 /// Write Registers (function 16)
 /// </summary>
 /// <param name="address">First Register address</param>
 /// <param name="registers">Register values</param>
 public void WriteRegisters(ushort address, ushort[] registers)
 {
     try
     {
         cmd.function   = 16;
         cmd.regAddress = address;
         cmd.length     = (ushort)registers.Length;
         cmd.param      = registers;
         sendAndWait();
     }
     catch (Exception err)
     {
         CommException Error = new CommException(err.Message, packetSended, packetReceived);
         throw Error;
     }
 }
Пример #4
0
        /// <summary>
        /// Write Register (function 6)
        /// </summary>
        /// <param name="address">Register address</param>
        /// <param name="register">Register value</param>
        public void WriteRegister(ushort address, ushort register)
        {
            try
            {
                cmd.function   = 6;
                cmd.regAddress = address;
                cmd.length     = 1;
                cmd.param      = new ushort[1];
                cmd.param[0]   = register;

                sendAndWait();
            }
            catch (Exception err)
            {
                CommException Error = new CommException(err.Message, packetSended, packetReceived);
                throw Error;
            }
        }
Пример #5
0
        /// <summary>
        /// Read Input registers Extended (function 66)
        /// </summary>
        /// <param name="address">Address of first Register</param>
        /// <param name="length">Register count</param>
        /// <returns>Register values</returns>
        public ushort[] ReadInputRegistersExt(ushort address, ushort length)
        {
            try
            {
                cmd.function   = 66;
                cmd.regAddress = address;
                cmd.length     = length;

                sendAndWait();

                return(regList);
            }
            catch (Exception err)
            {
                CommException Error = new CommException(err.Message, packetSended, packetReceived);
                throw Error;
            }
        }
Пример #6
0
        /// <summary>
        /// Read Discrete Inputs (function 2)
        /// </summary>
        /// <param name="address">First discrete input address</param>
        /// <param name="length">Discete inputs count</param>
        /// <returns>Discete input values</returns>
        public bool[] ReadDiscreteInputs(ushort address, ushort length)
        {
            try
            {
                cmd.function   = 2;
                cmd.regAddress = address;
                cmd.length     = length;

                sendAndWait();

                return(coilList);
            }
            catch (Exception err)
            {
                CommException Error = new CommException(err.Message, packetSended, packetReceived);
                throw Error;
            }
        }
Пример #7
0
        /// <summary>
        /// Read Input registers (function 4)
        /// </summary>
        /// <param name="address">Address of first Register</param>
        /// <param name="length">Register count</param>
        /// <returns>Register values</returns>
        public ushort[] ReadInputRegisters(ushort address, ushort length)
        {
            try
            {
                ushort   toSend = length;
                ushort[] array  = new ushort[0];
                ushort[] tmpArray;

                while (toSend > 0)
                {
                    cmd.function   = 4;
                    cmd.regAddress = address;
                    if (toSend > 120)
                    {
                        cmd.length = 120;
                        address   += 120;
                    }
                    else
                    {
                        cmd.length = toSend;
                    }
                    toSend -= cmd.length;

                    sendAndWait();

                    tmpArray = new ushort[array.Length + regList.Length];
                    array.CopyTo(tmpArray, 0);
                    regList.CopyTo(tmpArray, array.Length);
                    array = tmpArray;
                }
                return(array);
            }
            catch (Exception err)
            {
                CommException Error = new CommException(err.Message, packetSended, packetReceived);
                throw Error;
            }
        }