コード例 #1
0
        public Result WriteRegisters(
            byte unitId,
            byte function,
            ushort address,
            ushort quantity,
            short[] registers,
            int offset)
        {
            if (function < 1 || function > 127)
            {
                return(Result.FUNCTION);
            }
            if (quantity < 1 || quantity > 123)
            {
                return(Result.QUANTITY);
            }
            if (quantity + offset > registers.GetLength(0))
            {
                return(Result.QUANTITY);
            }
            var array  = new byte[265];
            var array2 = new byte[8];

            array[0] = unitId;
            array[1] = function;
            array[2] = ByteAccess.HiByte(address);
            array[3] = ByteAccess.LoByte(address);
            array[4] = ByteAccess.HiByte(quantity);
            array[5] = ByteAccess.LoByte(quantity);
            array[6] = (byte)(quantity * 2);
            for (var i = 0; i < quantity; i++)
            {
                array[7 + i * 2]     = ByteAccess.HiByte((ushort)registers[i + offset]);
                array[7 + i * 2 + 1] = ByteAccess.LoByte((ushort)registers[i + offset]);
            }

            var result = this.TxRx.TxRx(array, array[6] + 7, array2, 6);

            if (result == Result.SUCCESS && array[0] != 0)
            {
                for (var j = 0; j < 6; j++)
                {
                    if (array[j] != array2[j])
                    {
                        result = Result.RESPONSE;
                    }
                }
            }
            return(result);
        }
コード例 #2
0
        public Result DetectDevice(byte unitId, out string name)
        {
            name = string.Empty;
            byte   function = 66;
            ushort address  = 1;
            ushort quantity = 40;
            var    array    = new byte[8];
            var    array2   = new byte[261];

            array[0] = unitId;
            array[1] = (byte)(function & 255);
            array[2] = ByteAccess.HiByte(address);
            array[3] = ByteAccess.LoByte(address);
            array[4] = ByteAccess.HiByte(quantity);
            array[5] = ByteAccess.LoByte(quantity);
            var responseLength = 3 + quantity;
            var result         = this.TxRx.TxRx(array, 6, array2, responseLength);

            if (result == Result.SUCCESS)
            {
                if (array[0] != array2[0] || array[1] != array2[1])
                {
                    result = Result.RESPONSE;
                }
                else if (quantity != array2[2])
                {
                    result = Result.BYTECOUNT;
                }
                else
                {
                    for (var i = 0; i < quantity && array2[i + 3] > 0; i++)
                    {
                        name += (char)array2[i + 3];
                    }
                }
            }

            return(result);
        }
コード例 #3
0
        public Result ReadWriteMultipleRegisters(
            byte unitId,
            ushort readAddress,
            ushort readSize,
            short[] readRegisters,
            ushort writeAddress,
            ushort writeSize,
            short[] writeRegisters)
        {
            if (readSize < 1 || readSize > 123)
            {
                return(Result.QUANTITY);
            }
            if (writeSize < 1 || writeSize > 121)
            {
                return(Result.QUANTITY);
            }
            if (readSize > readRegisters.GetLength(0))
            {
                return(Result.QUANTITY);
            }
            if (writeSize > writeRegisters.GetLength(0))
            {
                return(Result.QUANTITY);
            }
            var array  = new byte[269];
            var array2 = new byte[261];

            array[0]  = unitId;
            array[1]  = 23;
            array[2]  = ByteAccess.HiByte(readAddress);
            array[3]  = ByteAccess.LoByte(readAddress);
            array[4]  = ByteAccess.HiByte(readSize);
            array[5]  = ByteAccess.LoByte(readSize);
            array[6]  = ByteAccess.HiByte(writeAddress);
            array[7]  = ByteAccess.LoByte(writeAddress);
            array[8]  = ByteAccess.HiByte(writeSize);
            array[9]  = ByteAccess.LoByte(writeSize);
            array[10] = (byte)(writeSize * 2);
            var responseLength = 3 + readSize * 2;

            for (var i = 0; i < writeSize; i++)
            {
                array[11 + i * 2]     = ByteAccess.HiByte((ushort)writeRegisters[i]);
                array[11 + i * 2 + 1] = ByteAccess.LoByte((ushort)writeRegisters[i]);
            }

            var result = this.TxRx.TxRx(array, array[10] + 11, array2, responseLength);

            if (result == Result.SUCCESS)
            {
                if (array[0] != array2[0] || array[1] != array2[1])
                {
                    result = Result.RESPONSE;
                }
                else
                {
                    for (var j = 0; j < readSize; j++)
                    {
                        readRegisters[j] = (short)ByteAccess.MakeWord(array2[2 * j + 4], array2[2 * j + 3]);
                    }
                }
            }

            return(result);
        }