public static byte[] DeviceMakeSendReadCommand(SlaveDevice _device, byte modbusCommand)
        {
            SlaveDevice device = _device;

            byte[]     packageToSlave              = new byte[20];
            byte[]     packageFromSlave            = new byte[20];
            bool       packageSentSuccessful       = false;
            byte       currentAttempt              = 0;
            const byte numberOfAttempts            = 5;
            byte       packageReceivedSuccessfully = (byte)ModbusCommand.PackageReceivedSuccessfully;

            packageToSlave = MakePackage(device.DeviceAddress, modbusCommand);
            do
            {
                bool packageSent = SendPackage(device.GetSerialPort, packageToSlave);
                if (packageSent)
                {
                    if (ReadAnswer(device.GetSerialPort, packageFromSlave))
                    {
                        const byte commandPosition = 1;
                        if (packageFromSlave[commandPosition] == packageReceivedSuccessfully)
                        {
                            packageSentSuccessful = true;
                        }
                    }
                    else
                    {
                        currentAttempt++;
                        packageSentSuccessful = false;
                    }
                }
                else
                {
                    currentAttempt++;
                    packageSentSuccessful = false;
                }

                if (device.GetSerialPort.IsOpen)
                {
                    device.GetSerialPort.Close();
                }
            }while ((!packageSentSuccessful) && (currentAttempt < numberOfAttempts));

            //return packageFromSlave;
            if (packageSentSuccessful && (currentAttempt < numberOfAttempts))
            {
                return(packageFromSlave);
            }
            else
            {
                return(null);
            }
        }
        public static bool DeviceFinishedMeasurements(SlaveDevice _device)
        {
            SlaveDevice device = _device;

            byte[] packageToSlave   = new byte[20];
            byte[] packageFromSlave = new byte[20];
            //bool packageSentSuccessful = false;
            byte       currentAttempt     = 0;
            const byte numberOfAttempts   = 5;
            const byte commandPosition    = 1;
            const byte answerCodePosition = 3;

            //byte packageReceivedSuccessfully = (byte)ModbusCommand.PackageReceivedSuccessfully;

            packageToSlave = MakePackage(device.DeviceAddress, (byte)ModbusCommand.ReadyToSendData);
            do
            {
                //bool packageSent = SendPackage(device.GetSerialPort, packageToSlave);
                if (SendPackage(device.GetSerialPort, packageToSlave))
                {
                    if (!ReadAnswer(device.GetSerialPort, packageFromSlave))
                    {
                        currentAttempt++;
                    }
                }
                else
                {
                    currentAttempt++;
                    //packageSentSuccessful = false;
                }

                if (device.GetSerialPort.IsOpen)
                {
                    device.GetSerialPort.Close();
                }
            }while ((Enum.IsDefined(typeof(ModBusErrors), packageFromSlave[commandPosition])) && (currentAttempt < numberOfAttempts));
            //while ((!packageSentSuccessful) && (currentAttempt < numberOfAttempts));

            //return packageFromSlave;
            if (packageFromSlave[answerCodePosition] == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static int IsActiveCountersInDevice(SlaveDevice device)
        {
            SlaveDevice _device        = device;
            int         index          = 0;
            int         activeCounters = 0;

            foreach (var counter in _device.counter)
            {
                if (counter.Active)
                {
                    activeCounters |= 1 << index;
                }
                ++index;
            }
            return(activeCounters);
        }
        //public static byte[] DeviceMakeSendReadCommand(SlaveDevice _device, byte modbusCommand)
        //{
        //    SlaveDevice device = _device;
        //    byte[] packageToSlave = new byte[20];
        //    byte[] packageFromSlave = new byte[20];
        //    bool packageSentSuccessful = false;
        //    byte currentAttempt = 0;
        //    const byte numberOfAttempts = 5;
        //    byte packageReceivedSuccessfully = (byte)ModbusCommand.PackageReceivedSuccessfully;

        //    packageToSlave = MakePackage(device.DeviceAddress, modbusCommand);
        //    do
        //    {
        //        bool packageSent = SendPackage(device.GetSerialPort, packageToSlave);
        //        if (packageSent)
        //        {
        //            if (ReadAnswer(device.GetSerialPort, packageFromSlave))
        //            {
        //                const byte commandPosition = 1;
        //                if (packageFromSlave[commandPosition] == packageReceivedSuccessfully)
        //                {
        //                    packageSentSuccessful = true;
        //                }
        //            }
        //            else
        //            {
        //                currentAttempt++;
        //                packageSentSuccessful = false;
        //            }
        //        }
        //        else
        //        {
        //            currentAttempt++;
        //            packageSentSuccessful = false;
        //        }

        //        if (device.GetSerialPort.IsOpen)
        //        {
        //            device.GetSerialPort.Close();
        //        }
        //    }
        //    while ((!packageSentSuccessful) && (currentAttempt < numberOfAttempts));

        //    //return packageFromSlave;
        //    if (packageSentSuccessful && (currentAttempt < numberOfAttempts))
        //        return packageFromSlave;
        //    else
        //        return null;
        //}
        public static void ActivateDeactivateCountersInDevice(SlaveDevice device)
        {
            SlaveDevice _device        = device;
            int         index          = 0;
            int         activeCounters = 0;

            foreach (var counter in _device.counter)
            {
                if (counter.Active)
                {
                    activeCounters |= 1 << index;
                }
                ++index;
            }

            byte[] packageToSlave   = new byte[20];
            byte[] packageFromSlave = new byte[20];
            packageToSlave = MakePackage(_device.DeviceAddress, (byte)ModbusCommand.EnableCounters, (byte)activeCounters);
            SendPackage(_device.GetSerialPort, packageToSlave);
        }