Example #1
0
        public static byte[][] ReadRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints, IModbusMaster master, uint wordSize, Func <byte[], byte[]> endianConverter, bool wordSwap = false)
        {
            var registerMultiplier = RegisterFunctions.GetRegisterMultiplier(wordSize);
            var registersToRead    = (ushort)(numberOfPoints * registerMultiplier);
            var values             = master.ReadHoldingRegisters(slaveAddress, startAddress, registersToRead);

            if (wordSwap)
            {
                Array.Reverse(values);
            }
            return(RegisterFunctions.ConvertRegistersToValues(values, registerMultiplier).Select(endianConverter).ToArray());
        }
Example #2
0
        public static void WriteRegistersFunc(byte slaveAddress, ushort startAddress, byte[][] data, IModbusMaster master, uint wordSize, Func <byte[], byte[]> endianConverter, bool wordSwap = false)
        {
            var wordByteArraySize = RegisterFunctions.GetRegisterMultiplier(wordSize) * 2;

            if (data.Any(e => e.Length != wordByteArraySize))
            {
                throw new ArgumentException("All data values must be of the correct word length.");
            }
            var dataCorrectEndian = data.Select(endianConverter).ToArray();
            var registerValues    = RegisterFunctions.ConvertValuesToRegisters(dataCorrectEndian);

            if (wordSwap)
            {
                Array.Reverse(registerValues);
            }
            master.WriteMultipleRegisters(slaveAddress, startAddress, registerValues);
        }
Example #3
0
        private static byte[] PadBytesToWordSize(uint wordSize, byte[] source, bool frontPadding)
        {
            var targetLength = RegisterFunctions.GetRegisterMultiplier(wordSize) * 2;
            var target       = new byte[targetLength];

            if (source.Length > target.Length)
            {
                throw new ArgumentException("Source bytes can not greater than target");
            }
            var offset = frontPadding
        ? target.Length - source.Length
        : 0;

            Array.Copy(
                source, 0, target, offset, source.Length);
            return(target);
        }
Example #4
0
        public static byte[][] CharsToByteValueArrays(char[] data, uint wordSize, bool frontPadding = true, bool singleCharPerRegister = true)
        {
            var bytesPerWord = RegisterFunctions.GetRegisterMultiplier(wordSize) * 2;

            if (!singleCharPerRegister)
            {
                var remainder     = data.Length % bytesPerWord;
                var registerBytes = remainder > 0
          ? data.Length + (bytesPerWord - remainder)
          : data.Length;
                var byteArray = new byte[registerBytes];
                for (var index = 0; index < byteArray.Length; index++)
                {
                    byteArray[index] = index < data.Length
            ? Convert.ToByte(data[index])
            : Convert.ToByte('\0'); //Unicode Null Charector
                }
                var byteValueArrays = new byte[byteArray.Length / bytesPerWord][];
                for (var index = 0; index < byteValueArrays.Length; index++)
                {
                    var offset = index * bytesPerWord;
                    byteValueArrays[index] = new ArraySegment <byte>(byteArray, offset, bytesPerWord).ToArray();
                }
                return(byteValueArrays);
            }
            return((frontPadding)
        ? data.Select(e =>
            {
                var bytes = new byte[bytesPerWord];
                bytes[bytes.Length - 1] = Convert.ToByte(e);
                return bytes;
            }).ToArray()
        : data.Select(e =>
            {
                var bytes = new byte[bytesPerWord];
                bytes[0] = Convert.ToByte(e);
                return bytes;
            }).ToArray());
        }
Example #5
0
 public static byte[][] FloatToByteValueArrays(float[] data, uint wordSize, bool frontPadding = true)
 => data.Select(e => RegisterFunctions.PadBytesToWordSize(
                    wordSize, BitConverter.GetBytes(e), frontPadding)).ToArray();