コード例 #1
0
        private byte[] modbusRead(byte id, ushort addr, ushort length)
        {
            byte[] data = new byte[length];
            // get the most significant digit
            int pow    = (int)Math.Pow(10, Math.Floor(Math.Log10(addr)));
            int ldigit = (int)Math.Floor((double)addr / pow);

            addr = (pow > 1) ? (ushort)(addr % pow) : addr;
            // four operation type
            switch (ldigit)
            {
            case 1:
                socket.ReadCoils(1, id, addr, length, ref data);
                return(data);

            case 2:
                socket.ReadDiscreteInputs(2, id, addr, length, ref data);
                return(data);

            case 3:
                socket.ReadInputRegister(3, id, addr, length, ref data);
                return(data);

            case 4:
                socket.ReadHoldingRegister(4, id, addr, length, ref data);
                return(data);

            default:
                throw new Exception();
            }
        }