Hi vadim_cnc,

Very good!

That obviously works but you are still using the & operator unnecessarily. Its ok to set the bit only once before the wait loop rather than continuously in the wait loop. And the indentation is incorrect. Here is how I would code and format it:
Code:
#include "KMotionDef.h"

main()
{
    for (;;)  //loop forever
    {
        WaitNextTimeSlice();

#define DESIRED_ENCODER_VALUE_1 100000.0
#define DESIRED_ENCODER_VALUE_2  -50000.0

        SetBit(144);  //Drive Forward
        ClearBit(145);
        while (ch4->Position < DESIRED_ENCODER_VALUE_1);  // wait doing nothing until at Position


        SetBit(145);  //Drive Reverse
        ClearBit(144);
        while (ch4->Position > DESIRED_ENCODER_VALUE_2);  // wait doing nothing until at Position

    }
}


This code works well and properly switches the relay in the band in which he was set. But there is one small remark, perhaps due to feature of the mechanics of movement of the hydraulic cylinder is observed tool going beyond the operating range.
Is it possible to increase the speed of reading data from an optical linear encoder? For faster switching relays.
I don't think the overshoot problem has to do with the time it takes to read the encoder (that is much less than a millisecond). The problem is the time it takes to stop. It is like driving your car and waiting until you are exactly at a Stop sign to begin stopping. There is no way to avoid overshooting the stop sign. You could start stopping some distance before the position you need to stop. You can monitor the speed and make the stopping point variable depending on the current speed. Maybe you could add another Relay+Valve to slow down when you get close? A proportional valve would be best.



And one more important question of how to implement the transmission of values from KmotionCNC in the control code hydraulic cylinder ?
Well you haven't explained how you expect the machine to operate and what other functions you need. But the Add example in the KMotionCNC Screen Editor should be very similar to what you need. When you push a button it runs a C Program to read two numbers from the Screen and add them. In your case you might just use the two values as your set points. Such as:
Code:
#include "KMotionDef.h"

#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"


main()
{
    double d1, d2;
    char s[80];
    // Read double from a KMotionCNC Edit Control
    // Persist Var identifies the Control and contents specifies 
    // where the string data should be placed in the 
    // Gather Buffer as an offset in words
    if (GetEditControlDouble(&d1, 160, 1000))
    {
        printf("Error Reading Edit Control Var=160\n");
        DROLabel(1000, 162, "???");
        return;
    }
    // Read double from a KMotionCNC Edit Control
    // Persist Var identifies the Control and contents specifies 
    // where the string data should be placed in the 
    // Gather Buffer as an offset in words
    if (GetEditControlDouble(&d2, 161, 1100))
    {
        printf("Error Reading Edit Control Var=161\n");
        DROLabel(1000, 162, "???");
        return;
    }

    for (;;)  //loop forever
    {
        WaitNextTimeSlice();

        SetBit(144);  //Drive Forward
        ClearBit(145);
        while (ch4->Position < d1);  // wait doing nothing until at Position


        SetBit(145);  //Drive Reverse
        ClearBit(144);
        while (ch4->Position > d2);  // wait doing nothing until at Position

    }
}


P.S.
I tried experimenting with KmotionCNC and Screen Editor, but nothing is working did not work.
What exactly did you try? What didn't work? What happened?

Regards