#include <message_modbus.h>
#include <message_terminals_can.h>
#include <message2.h>
#include <modbus_hmi.h>
#include <project.h>
#include <vector.h>
#include <modbus_hmi_update.h>
#include "CAN_Setup.h"
#include "global_time.h"
#include "modbus_table_v2.h"
#include "oscil_can.h"
#include "RS_modbus_pult.h"
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "DSP281x_Device.h"   // DSP281x Headerfile Include File
#include "CRC_Functions.h"
#include "RS_Function_terminal.h"
#include "RS_modbus_svu.h"
#include "TuneUpPlane.h"
#if (USE_CONTROL_STATION==1)
#include "control_station.h"
#endif
#include <message2can.h>
#include <edrk_main.h>

#include "pwm_test_lines.h"
#include "params.h"
#include "logs_hmi.h"


//#include "can_setup_21300.h"
//#include "modbus_can.h"

//static int err_modbus3=1;
//static int err_modbus16=1;
//static int cmd_3_or_16=0;

int enable_can = 0;

void write_all_data_to_mpu_485(int run_force)
{
    static unsigned int time_tick_modbus = 0;
    static unsigned int old_PWM_ticks = 0;
    static int count_write_to_modbus = 0;
    static int cur_position_buf_modbus16 = 0;

    if (global_time.pwm_tics != old_PWM_ticks)
    {
        if (global_time.pwm_tics > old_PWM_ticks)
            time_tick_modbus = time_tick_modbus + (global_time.pwm_tics - old_PWM_ticks);
        else
            time_tick_modbus++;
    }

    if (time_tick_modbus > TIME_PAUSE_MODBUS_MPU)
    {
        //   pause_1000()

        time_tick_modbus = 0;
        pause_1000(10);
        //        SendCommandModbus3(&rs_b, 0x1, 0xc012,1);
        //		rs_b.flag_LEADING = 0;
        if (!rs_b.flag_LEADING)
        {
            time_tick_modbus = 0;
            //Fast answer to SVU when command changed
            //		  if (flag_send_answer_rs) {
            //			  flag_send_answer_rs = 0;
            //			  err_modbus16++;
            //			  SendCommandModbus16(&rs_b,1,210,SIZE_BUF_WRITE_TO_MODBUS16);
            //			  return;
            //		  }
            if (err_modbus16 == 0)
                cur_position_buf_modbus16 = cur_position_buf_modbus16 + SIZE_BUF_WRITE_TO_MODBUS16_VPU;

            if (cur_position_buf_modbus16 >= SIZE_MODBUS_TABLE)
                cur_position_buf_modbus16 = 0;

            if ((cur_position_buf_modbus16 + SIZE_BUF_WRITE_TO_MODBUS16_VPU) > SIZE_MODBUS_TABLE)
                count_write_to_modbus = SIZE_MODBUS_TABLE - cur_position_buf_modbus16;
            else
                count_write_to_modbus = SIZE_BUF_WRITE_TO_MODBUS16_VPU;

            //          count_write_to_modbus = SIZE_BUF_WRITE_TO_MODBUS16_VPU;
            //		  cur_position_buf_modbus=0;

            err_modbus16++;

            if (err_modbus16 > MAX_COUNT_ERROR_FROM_RS_MPU)
                f.RS_MPU_ERROR = 1;

            ModbusRTUsend16(&rs_b, 1,
                            ADR_MODBUS_TABLE + cur_position_buf_modbus16,
                            count_write_to_modbus);
            //		  SendCommandModbus16(&rs_b,1,ADR_MODBUS_TABLE+cur_position_buf_modbus16,count_write_to_modbus);
            //		  SendCommandModbus16(&rs_b,1,1,30);

            //
            //          SendCommandModbus16(&rs_b,1,0xc001,0x64);
            //		  err_modbus
        }
    }
}

void read_all_data_from_mpu_485(int run_force)
{
    static unsigned int time_tick_modbus = 0;
    static unsigned int old_PWM_ticks = 0;
    static unsigned int count_write_to_modbus = 0;
    static int cur_position_buf_modbus3 = 0;

    if (global_time.pwm_tics != old_PWM_ticks)
    {
        if (global_time.pwm_tics > old_PWM_ticks)
            time_tick_modbus = time_tick_modbus + (global_time.pwm_tics - old_PWM_ticks);
        else
            time_tick_modbus++;
    }

    old_PWM_ticks = global_time.pwm_tics;

    if (TIME_PAUSE_MODBUS_MPU < time_tick_modbus)
    {
        //   pause_1000()

        pause_1000(10);
        //        SendCommandModbus3(&rs_b, 0x1, 0xc012,1);
        //rs_b.flag_LEADING = 0;
        if (!rs_b.flag_LEADING)
        {
            time_tick_modbus = 0;

            if (err_modbus3 == 0)
                cur_position_buf_modbus3 = cur_position_buf_modbus3 + SIZE_BUF_WRITE_TO_MODBUS16_VPU;

            if (cur_position_buf_modbus3 >= SIZE_MODBUS_TABLE)
                cur_position_buf_modbus3 = 0;

            if ((cur_position_buf_modbus3 + SIZE_BUF_WRITE_TO_MODBUS16_VPU) > SIZE_MODBUS_TABLE)
                count_write_to_modbus = SIZE_MODBUS_TABLE - cur_position_buf_modbus3;
            else
                count_write_to_modbus = SIZE_BUF_WRITE_TO_MODBUS16_VPU;

            //          count_write_to_modbus = SIZE_BUF_WRITE_TO_MODBUS16_VPU;
            //		  cur_position_buf_modbus=0;

            err_modbus3++;

            if (err_modbus3 > MAX_COUNT_ERROR_FROM_RS_MPU)
                f.RS_MPU_ERROR = 1;

            //		  SendCommandModbus3(&rs_b,1,ADR_MODBUS_TABLE+cur_position_buf_modbus3,count_write_to_modbus);
            ModbusRTUsend3(&rs_b, 1,
                           ADR_MODBUS_TABLE + cur_position_buf_modbus3,
                           count_write_to_modbus);
        }
    }

    //  time_tick_modbus++;
}

void write_all_data_to_mpu_can(int run_force, unsigned int pause)
{
//    static int time_tick_modbus_can = 0;
    static unsigned int old_time = 0;
    static int count_write_to_modbus_can = 0;
//    static int time_send_to_can = 0;
 //   static unsigned int counter_max_I = 0, counter_max_M = 0;

    static int cur_position_buf_modbus16_can = 0, prev_send_to_can = 0;
    int real_mbox;
    unsigned int i;

    real_mbox = get_real_out_mbox(MPU_TYPE_BOX, edrk.flag_second_PCH);

    if (prev_send_to_can && CAN_cycle_full_free(real_mbox,CAN_BOX_STAT_OFF)==0)
    {
        old_time = (unsigned int)global_time.miliseconds;
        return;
    }
    prev_send_to_can = 0;

    if (!detect_pause_milisec(pause,&old_time))
        return;


//
//
//    if (cmd_3_or_16 == 1 || run_force)
//    {
//
//        if (global_time.pwm_tics != old_PWM_ticks)
//        {
//            if (global_time.pwm_tics > old_PWM_ticks)
//                time_tick_modbus_can = time_tick_modbus_can + (global_time.pwm_tics - old_PWM_ticks);
//            else
//                time_tick_modbus_can++;
//        }
//
//        old_PWM_ticks = global_time.pwm_tics;
//    }
//    else
//    {
//        old_PWM_ticks = global_time.pwm_tics;
//        return;
//    }
//
//
//
//    if (CAN_cycle_free(real_mbox))
//    {
//        if (time_send_to_can == 0)
//            time_send_to_can = time_tick_modbus_can;
//
//        //    time_tick_modbus_can=0;
//
//    }
    //  if(f.Prepare && CAN_cycle_free(real_mbox))
    //  {
    //	CAN_cycle_send( MPU_TYPE_BOX, 0, 198, &modbus_table_can_out[197].all, 1);
    //  }

  
//    if (time_tick_modbus_can > TIME_PAUSE_MODBUS_CAN)
//    {
//        time_tick_modbus_can = 0;
//        time_send_to_can = 0;
//        //	 pause_1000(300);


        if (CAN_cycle_full_free(real_mbox,CAN_BOX_STAT_ON))
        {
            /////////////////////////////////////////////////////////////////////////////////////////////

            ////////////////////////////////////////////////////////////////////////////////////////////


            if (cur_position_buf_modbus16_can >= SIZE_MODBUS_TABLE)
            {
                cur_position_buf_modbus16_can = 0;
 //               modbus_table_can_out[ADR_CAN_TEST_PLUS_ONE].all++;
            }

            // ��������� �� ��������� ������ ����� ������� �������� ����� ������� ����� ������ �� �������� �� ����� ��������
            if (cur_position_buf_modbus16_can == 0)
            {
                for (i=0;i<SIZE_MODBUS_TABLE;i++)
                    modbus_table_can_out_temp[i] = modbus_table_can_out[i];
            }

            if ((cur_position_buf_modbus16_can + SIZE_BUF_WRITE_TO_MODBUS16_CAN) >= SIZE_MODBUS_TABLE)
                count_write_to_modbus_can = SIZE_MODBUS_TABLE - cur_position_buf_modbus16_can;
            else
                count_write_to_modbus_can = SIZE_BUF_WRITE_TO_MODBUS16_CAN;

            if (CAN_cycle_full_free(real_mbox,CAN_BOX_STAT_ON))
            {
                //	    	modbus_table_can_out[0x124].all++;
                //		    CAN_cycle_send(MPU_CAN_DEVICE, cur_position_buf_modbus16_can+1, &modbus_table_can_out[cur_position_buf_modbus16_can].all, count_write_to_modbus_can);

                CAN_cycle_send(
                    MPU_TYPE_BOX,
                    edrk.flag_second_PCH,
                    cur_position_buf_modbus16_can + 1,
//                    &modbus_table_can_out[cur_position_buf_modbus16_can].all,
                    &modbus_table_can_out_temp[cur_position_buf_modbus16_can].all,
                    count_write_to_modbus_can, CAN_BOX_STANDART_ADR, CAN_BOX_PRIORITY_NORMAL);

                cur_position_buf_modbus16_can = cur_position_buf_modbus16_can + SIZE_BUF_WRITE_TO_MODBUS16_CAN;
            }
        }

//    }

}

//void test_rs_can_with_svu_mpu()
//{
//    int test_a, test_b;
//
//    test_a = test_rs_live(&rs_a);
//    test_b = test_rs_live(&rs_b);
//
//    if (test_a == 0 && test_b == 0 && f.status_MODE_WORK_SVU == 0)
//    {
//        /* ���� ����� �� �������� �� ���������� ��������� � ��������� ������������ RS232 */
//        if (test_can_live_mpu() == 0)
//        {
//            //                  if (f.cmd_to_go == 0) f.cmd_to_go = ERROR_CMD_GO_1;
//            edrk.Go = 0; /*	���������	*/
//        }
//    }
//
//    if (test_a == 0 && test_b == 0 && f.status_MODE_WORK_MPU == 0 /*&& f.status_SPEED_SELECT_KEY==0*/)
//    {
//        /* ���� ����� �� �������� �� ���������� ��������� � ��������� ������������ RS232 */
//        if (test_can_live_mpu() == 0)
//        {
//            //                  if (f.cmd_to_go == 0) f.cmd_to_go = ERROR_CMD_GO_2;
//            edrk.Go = 0; /*	���������	*/
//        }
//    }
//
//    if (test_a == 2)
//    {
//        /* ���� ����� �� �������� �� ���������� ��������� � ��������� ������������ RS232 */
//        //				  edrk.Go=0;			/*	���������	*/
//        resetup_rs(&rs_a);
//    }
//
//    if (test_b == 2)
//    {
//        /* ���� ����� �� �������� �� ���������� ��������� � ��������� ������������ RS232 */
//        resetup_rs(&rs_b);
////		flag_waiting_answer = 0; //���������, ����� ����� ������� �� ��������� ������,
//								//���� ���� slave ����, ������ ����� ��������.
//    }
//
//    if (test_b == 5)
//    {
//        /* ���� ����� �� �������� �� ���������� ��������� � ��������� ������������ RS232 */
//        resetup_rs(&rs_b);
////		flag_waiting_answer = 0; //���������, ����� ����� ������� �� ��������� ������,
//								//���� ���� slave ����, ������ ����� ��������.
//    }
//
//    if (test_a == 4 && test_b == 4) //TODO: && SPEED_SELECT_REMOTE==1)
//    {
//        // ���� ����� �� �������� �� ���������� ��������� � ��������� ������������ RS232
//        if (test_can_live_mpu() == 0)
//        {
//            //		           if (f.cmd_to_go == 0) f.cmd_to_go = ERROR_CMD_GO_3;
//            edrk.Go = 0; //	���������
//        }
//        resetup_mpu_rs(&rs_a);
//        resetup_mpu_rs(&rs_b);
////		flag_waiting_answer = 0;
//    }
//
//    if (test_a == 4 && test_b == 4) //TOD: && SPEED_SELECT_REMOTE==0)
//    {
//        // ���� ����� �� �������� �� ���������� ��������� � ��������� ������������ RS232
//        //		          if (test_can_live_mpu()==0)
//
//        // test_can_live_terminal  //
//
//        {
//            //		           if (f.cmd_to_go == 0) f.cmd_to_go = ERROR_CMD_GO_20;
//            edrk.Go = 0; //	���������
//        }
//        resetup_mpu_rs(&rs_a);
//        resetup_mpu_rs(&rs_b);
//    }
//
//    if (test_a == 0 && f.status_MODE_WORK_MPU == 0) // && SPEED_SELECT_REMOTE==0)
//    {
//        /* ���� ����� �� �������� �� ���������� ��������� � ��������� ������������ RS232 */
//        //		         if (test_can_live_mpu()==0)
//        //                  if (f.cmd_to_go == 0) f.cmd_to_go = ERROR_CMD_GO_4;
//        edrk.Go = 0; /*	���������	*/
//    }
//
//    //         if (CAN_timeout[]==1)
//    //		   f.CAN_MPU_ERROR=CAN_timeout[MPU_CAN_DEVICE];
//}



#define TIME_REINIT_PULT_INGETEAM 5


void test_alive_pult_485(void)
{
    static unsigned int time_pause = 0, old_time = 0;

//
//    if (control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485]>control_station.setup_time_detect_active_resend_485[CONTROL_STATION_INGETEAM_PULT_RS485])
//    {
//        resetup_mpu_rs(&rs_b);
//        control_station.flag_waiting_answer[CONTROL_STATION_INGETEAM_PULT_RS485]   = 0;
//        control_station.flag_message_sent[CONTROL_STATION_INGETEAM_PULT_RS485]   = 0;
//        control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
//    }
/*
    if (control_station.time_detect_active[CONTROL_STATION_INGETEAM_PULT_RS485]>control_station.setup_time_detect_active_resend_485[CONTROL_STATION_INGETEAM_PULT_RS485])
    {
        resetup_mpu_rs(&rs_b);
        control_station.flag_waiting_answer[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
   //     control_station.time_detect_active[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
    }
*/

//    if (control_station.time_detect_active[CONTROL_STATION_INGETEAM_PULT_RS485]>control_station.setup_time_detect_active[CONTROL_STATION_INGETEAM_PULT_RS485])
//    {
//        resetup_mpu_rs(&rs_b);
//        control_station.flag_waiting_answer[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
//        control_station.time_detect_active[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
//    }


/*
    if (!detect_pause_milisec(CONTROL_STATION_TIME_WAIT,&old_time))
       return;

    time_pause++;

    if (time_pause>=TIME_REINIT_PULT_INGETEAM)
    {
        flag_waiting_answer = 0; //���������, ����� ����� ������� �� ��������� ������
    }
*/

}


#define MAX_COUNT_WORK_IN_LOG   150


int modbusNetworkSharing(int flag_update_only_hmi)
{
    static unsigned int old_time = 0 , old_time_refresh = 0, time_pause = TIME_PAUSE_MODBUS_REMOUTE;
    static unsigned int old_time_status_3 = 0, time_pause_status_3 = 500;
    int final_code=0;
    static unsigned int status=0;
    static int numberInT=0, enable_send_cmd = 0;

    static int run_pause = 1, flag_next = 0, prev_flag_next = 0;
    static int last_ok_cmd=0;
    static int flag_only_one_cmd=0;
    static int status_ok = 1, status_err = 0, flag_work_rs_send_log = 0, count_free = 0, count_work_in_log = 0;


#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
    PWM_LINES_TK_20_ON;
#endif

    RS232_WorkingWith(0,1,0);

    // ��� ��� ������-����� ��� ������� ��������
    //  control_station.flag_waiting_answer[CONTROL_STATION_INGETEAM_PULT_RS485] = 1;

    control_station_test_alive_all_control();

 //   if (detect_pause_milisec(100,&old_time_refresh))
       control_station.update_timers(&control_station);

    detecting_cmd_from_can();

 //   test_rs_can_with_svu_mpu();
//    test_alive_pult_485();


 //   if (rs_b.RS_DataSended==0 && rs_b.RS_DataReadyAnswerAnalyze==0)
  //      status = 0;

    control_station.flag_message_sent[CONTROL_STATION_INGETEAM_PULT_RS485] = rs_b.RS_DataSended;

    final_code = 0;


    switch(status)
    {
    case 0:

        old_time = global_time.miliseconds;
        status = 1;
        if (time_pause==0)
            status = 2;

            break;
    case 1:
        //        if (numberInT==0)
        //        {
                    if (detect_pause_milisec(time_pause,&old_time))
                        status = 2;
        //        }
        //        else
        //            status = 2;

            break;
    case 2:
        enable_send_cmd = 1;
        control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
        status = 3;
        old_time_status_3 = global_time.miliseconds;

            break;
    case 3:
        //        // ���� �� � �������� ����� � ��� ������ ������� ��� ������ �� rs485
        //        if (flag_work_rs_send_log)
        //        {
        //            status = 0;
        //            status_ok++;
        //            if (status_ok<0) status_ok=1;
        //        }

        // �� ���� ������� �� rs485 ������ � ������ ��� ������!
        if (rs_b.RS_DataWillSend2 == 0 && enable_send_cmd == 0)
        {
            status = 0;
            status_ok++;
            count_free++;
        }
        else
        if (rs_b.RS_DataReadyAnswerAnalyze)
        {
//            i_led2_on_off(0);

            control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
            control_station.count_ok_modbus[CONTROL_STATION_INGETEAM_PULT_RS485]++;
            rs_b.RS_DataReadyAnswerAnalyze = 0;
            rs_b.RS_DataWillSend2 = 0;
            status = 0;

            if (last_ok_cmd==4) // ������ readAnalogDataFromRemote() �� ����
            {
                edrk.get_new_data_from_hmi = 1;// ����� ������� ������?
                edrk.get_new_data_from_hmi2 = 1;// ����� ������� ������?
            }
            final_code = last_ok_cmd;//numberInT+1;

            //status_err = 0;
            status_ok++;
        }
        else
        {
            if ( (control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485]>control_station.setup_time_detect_active_resend_485[CONTROL_STATION_INGETEAM_PULT_RS485])
                 || (detect_pause_milisec(time_pause_status_3,&old_time_status_3)) )
            {
                resetup_mpu_rs(&rs_b);
                control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
                rs_b.RS_DataWillSend2 = 0;
                status = 0;
                control_station.count_error_modbus[CONTROL_STATION_INGETEAM_PULT_RS485]++;
                status_err++;// = 1;
            }

        }

        if (status_ok<0) status_ok=1;

            break;
    case 4:

            break;
    case 5:

            break;
    case 6:

            break;
    default:
            break;



    }

/*
    if (status==0)
    {
        old_time = global_time.miliseconds;
        status = 1;
        if (time_pause==0)
            status = 2;
    }

    if (status==1)
    {
//        if (numberInT==0)
//        {
            if (detect_pause_milisec(time_pause,&old_time))
                status = 2;
//        }
//        else
//            status = 2;
    }

    if (status==2)
    {
        enable_send_cmd = 1;
        control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
        status = 3;
        old_time_status_3 = global_time.miliseconds;
    }

    if (status==3)
    {
        if (rs_b.RS_DataWillSend2 == 0)
        {

            count_free++;

        }

        // ���� �� � �������� ����� � ��� ������ ������� ��� ������ �� rs485
        if (flag_work_rs_send_log)
        {
            status = 0;
            status_ok++;
            if (status_ok<0) status_ok=1;
        }

        if (rs_b.RS_DataReadyAnswerAnalyze)
        {
//            i_led2_on_off(0);

            control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
            control_station.count_ok_modbus[CONTROL_STATION_INGETEAM_PULT_RS485]++;
            rs_b.RS_DataReadyAnswerAnalyze = 0;
            rs_b.RS_DataWillSend2 = 0;
            status = 0;

            if (last_ok_cmd==4) // ������ readAnalogDataFromRemote() �� ����
                edrk.get_new_data_from_hmi = 1;// ����� ������� ������?
            final_code = last_ok_cmd;//numberInT+1;

            //status_err = 0;
            status_ok++;
            if (status_ok<0) status_ok=1;
        }
        else
        {
            if ( (control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485]>control_station.setup_time_detect_active_resend_485[CONTROL_STATION_INGETEAM_PULT_RS485])
                 || (detect_pause_milisec(time_pause_status_3,&old_time_status_3)) )
            {
                resetup_mpu_rs(&rs_b);
                control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
                rs_b.RS_DataWillSend2 = 0;
                status = 0;
                control_station.count_error_modbus[CONTROL_STATION_INGETEAM_PULT_RS485]++;
                status_err++;// = 1;
            }

        }

    }
*/
//    switch (status)
//    {
//    case 0 : status = 1;
//             break;
//
//    case 1 : old_time = global_time.miliseconds;
//             status = 2;
//             break;
//
//    case 2 :
//             if (run_pause)
//             {
//                 status = 3;
//                 run_pause = 0;
//             }
//             if (detect_pause_milisec(time_pause,&old_time))
//                 status = 3;
//             break;
//
//    case 3 :
//             enable_send_cmd = 1;
////             control_station.count_error_modbus_15[CONTROL_STATION_INGETEAM_PULT_RS485]++;
//             control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
//             status = 4;
//             break;
//
//
//    case 4 :
//             if (rs_b.RS_DataReadyAnswerAnalyze)
//             {
//                 control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
//                 control_station.count_ok_modbus[CONTROL_STATION_INGETEAM_PULT_RS485]++;
//                 rs_b.RS_DataReadyAnswerAnalyze = 0;
//                 status = 1;
//                 final_code = numberInT+1;
//             }
//             else
//             {
//                 if (control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485]>control_station.setup_time_detect_active_resend_485[CONTROL_STATION_INGETEAM_PULT_RS485])
//                 {
//                     resetup_mpu_rs(&rs_b);
//                     control_station.time_detect_answer_485[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
//                     status = 1;
//                     control_station.count_error_modbus[CONTROL_STATION_INGETEAM_PULT_RS485]++;
//                 }
//             }
//             break;
//
//
//    case 5 : break;
//
//
//    case 6 : break;
//
//    default : break;
//    }


//
//
//    if (control_station.flag_message_sent[CONTROL_STATION_INGETEAM_PULT_RS485] == 1)
//    {
//        if (control_station.flag_waiting_answer[CONTROL_STATION_INGETEAM_PULT_RS485] == 0 && prev_flag_waiting_answer == 1)
//        {
//            old_time = global_time.miliseconds;
//        }
//
//        if (control_station.flag_waiting_answer[CONTROL_STATION_INGETEAM_PULT_RS485] == 0)
//        {
//            if (detect_pause_milisec(time_pause,&old_time))
//            {
//                control_station.flag_message_sent[CONTROL_STATION_INGETEAM_PULT_RS485] = 0;
//            }
//
//        }
//    }
//    prev_flag_waiting_answer = control_station.flag_waiting_answer[CONTROL_STATION_INGETEAM_PULT_RS485];
//
//
//    if (control_station.flag_message_sent[CONTROL_STATION_INGETEAM_PULT_RS485] == 0 &&
//            control_station.flag_waiting_answer[CONTROL_STATION_INGETEAM_PULT_RS485] == 0   )
//    {
//        enable_send_cmd = 1;
////        numberInT++;
////        if (numberInT>3)
////            numberInT = 1;
//    }
//


// ��� �������� ��� ����� ������
    if (flag_only_one_cmd)
        numberInT=flag_only_one_cmd-1;
//


    if (enable_send_cmd
//            && (log_to_HMI.send_log == 0)
            )
    {
//i_led2_on_off(1);
            last_ok_cmd = numberInT;
            switch (numberInT)
            {

            case 0:

                if ((flag_update_only_hmi==0) && (edrk.flag_enable_update_hmi))
                     update_tables_HMI_discrete();

                writeDiscreteDataToRemote();
                edrk.test++;

                numberInT++;
                enable_send_cmd = 0;
                break;

            case 1:

                if ((flag_update_only_hmi==0) && (edrk.flag_enable_update_hmi))
//                  if (edrk.flag_enable_update_hmi)
                    update_tables_HMI_analog();

                writeAnalogDataToRemote(); // 1 ����� �������

//                if (flag_update_only_hmi==1)
//                // ���������� ���� �������
//                    numberInT = 0;
//                else
                    numberInT++;

                enable_send_cmd = 0;
                break;

            case 2:

//                if (edrk.flag_enable_update_hmi)
//                    update_tables_HMI_analog();

                writeAnalogDataToRemote(); // 2 ����� �������

//                if (flag_update_only_hmi==1)
//                // ���������� ���� �������
//                    numberInT = 0;
//                else
                    numberInT++;

                enable_send_cmd = 0;
                break;


            case 3:
                readAnalogDataFromRemote(); // 1 �����
                numberInT++;
                enable_send_cmd = 0;
                break;

            case 4:
                readAnalogDataFromRemote(); // 2 �����

                if (log_to_HMI.send_log)
                {
                     numberInT++;
                }
                else
                    // ���������� ���� �������
                        numberInT = 0;

                enable_send_cmd = 0;
                count_work_in_log = 0; // ���������� ��� �����
                break;

            case 5:
                if (log_to_HMI.send_log)
                {
                    time_pause = 2;
        //            ccc[0] = 1;
                    flag_work_rs_send_log = !sendLogToHMI(status_ok);
                    edrk.flag_slow_in_main = 1;
                    enable_send_cmd = 0;

                    if (count_work_in_log>MAX_COUNT_WORK_IN_LOG)
                    {
                        count_work_in_log = 0;
                        numberInT = 0; // ��������� ������ ����

                    }
                    else
                    {
                        count_work_in_log++;
                        // �������� � ���� ����� �����
                    }

                }
                else
                {
                   time_pause = TIME_PAUSE_MODBUS_REMOUTE;
     //               ccc[0] = 0;
                    numberInT = 0;
                    enable_send_cmd = 0;
                    edrk.flag_slow_in_main = 0;
                }
                break;


            default:
                enable_send_cmd = 0;
                break;
            }


//i_led2_on_off(0);

    }
    //sendLogToHMI();


#if(_ENABLE_PWM_LINES_FOR_TESTS_RS)
    PWM_LINES_TK_20_OFF;
#endif

    if (flag_update_only_hmi)
        return final_code;


     return 0;
}



int modbusNetworkSharingCAN(void)
{
//    static unsigned int old_time1 = 0 , time_pause1 = TIME_PAUSE_NETWORK_CAN1;
//    static unsigned int old_time2 = 0 , time_pause2 = TIME_PAUSE_NETWORK_CAN2;
//    static unsigned int old_time3 = 0 , time_pause3 = TIME_PAUSE_NETWORK_CAN3;
    static unsigned int time_pause_modbus_can_terminals = TIME_PAUSE_MODBUS_CAN_TERMINALS;



#if (ENABLE_CAN_SEND_TO_MPU_FROM_MAIN)
//    if (detect_pause_milisec(time_pause1,&old_time1))
    static unsigned int time_pause_modbus_can_mpu = TIME_PAUSE_MODBUS_CAN_MPU;
    write_all_data_to_mpu_can(1, time_pause_modbus_can_mpu);
#endif

#if (ENABLE_CAN_SEND_TO_TERMINAL_FROM_MAIN)
//    if (detect_pause_milisec(time_pause2,&old_time2))
    write_all_data_to_terminals_can(1, time_pause_modbus_can_terminals);
#endif


#if (ENABLE_CAN_SEND_TO_TERMINAL_OSCIL)
//    if (detect_pause_milisec(time_pause3,&old_time3))
        oscil_can.pause_can = TIME_PAUSE_MODBUS_CAN_OSCIL;
        oscil_can.send(&oscil_can);
#endif

    return 0;
}