private void SetDataRTU(byte SlaveAddress, ushort StartAddr, DataRecievedRTU DataRecievedRTU, RequestPriority RequestPriority, object param, params ushort[] Data)
        {
            if ((Data.Length > 64) || (Data.Length < 1))
            {
                if (DataRecievedRTU != null)
                {
                    DataRecievedRTU(false, new ushort[0], null);
                }
                return;
            }
            byte[] buffer = new byte[9 + Data.Length * 2];
            buffer[0] = SlaveAddress;
            buffer[1] = 0x10;
            buffer[2] = (byte)((StartAddr >> 8) & 0xFF);
            buffer[3] = (byte)(StartAddr & 0xFF);
            buffer[4] = (byte)((Data.Length >> 8) & 0xFF);
            buffer[5] = (byte)(Data.Length & 0xFF);
            buffer[6] = (byte)(2 * Data.Length);


            for (int i = 0; i < Data.Length; i++)
            {
                buffer[7 + i * 2] = (byte)((Data[i] >> 8) & 0xFF);
                buffer[8 + i * 2] = (byte)(Data[i] & 0xFF);
            }

            byte[] buffer1;
            ModBusCRC.CalcCRC(buffer, 7 + Data.Length * 2, out buffer1);
            AddRequest(buffer1, 8, DataRecievedRTU, RequestPriority, param);
        }
/*
 *      public void GetData(byte SlaveAddress, ushort StartAddr, ushort WordCount, DataRecieved DataRecieved)
 *      {
 *          byte[] buffer = new byte[6];
 *          buffer[0] = SlaveAddress;
 *          buffer[1] = 0x03;
 *          buffer[2] = (byte)((StartAddr >> 8) & 0xFF);
 *          buffer[3] = (byte)(StartAddr & 0xFF);
 *          buffer[4] = (byte)((WordCount >> 8) & 0xFF);
 *          buffer[5] = (byte)(WordCount & 0xFF);
 *          byte[] buffer1;
 *          ModBusCRC.CalcCRC(buffer, 6, out buffer1);
 *          AddRequest(buffer1, WordCount * 2 + 5, DataRecieved);
 *      }
 *
 *      public void GetData(ushort StartAddr, ushort WordCount, DataRecieved DataRecieved)
 *      {
 *          GetData(SlaveAddr, StartAddr, WordCount, DataRecieved);
 *      }
 */
        private void GetDataRTU(byte SlaveAddress, ushort StartAddr, ushort WordCount, DataRecievedRTU DataRecievedRTU, RequestPriority RequestPriority, object param)
        {
            byte[] buffer = new byte[6];
            buffer[0] = SlaveAddress;
            buffer[1] = 0x03;
            buffer[2] = (byte)((StartAddr >> 8) & 0xFF);
            buffer[3] = (byte)(StartAddr & 0xFF);
            buffer[4] = (byte)((WordCount >> 8) & 0xFF);
            buffer[5] = (byte)(WordCount & 0xFF);
            byte[] buffer1;
            ModBusCRC.CalcCRC(buffer, 6, out buffer1);
            AddRequest(buffer1, WordCount * 2 + 5, DataRecievedRTU, WordCount, RequestPriority, param);
        }
        void SendDataBody(RequestUnit RequestUnit)
        {
            if (portError)
            {
                return;
            }
            if (serialPortMode == SerialPortModes.TCPMode)
            {
                if (RequestUnit.PortAnswerType != PortAnswerType.RTU)
                {
                    throw new Exception("Invalid request  type");
                }
            }


            if (serialPortMode == SerialPortModes.RSMode)
            {
                try
                {
                    if (serialPort.BytesToRead != 0)
                    {
                        byte[] buff = new byte[serialPort.BytesToRead + 1];
                        serialPort.Read(buff, 0, serialPort.BytesToRead);
                    }
                    Thread.Sleep(3);
                    serialPort.Write(RequestUnit.TxBuffer, 0, RequestUnit.TxBuffer.Length);
                }
                catch
                {
                    portError = true;
                    if (SerialPortError != null)
                    {
                        SerialPortError(serialPort, null);
                    }
                    return;
                }
            }

            tcpReadData = null;
            tcpWriteOk  = false;
            if (serialPortMode == SerialPortModes.TCPMode)
            {
                if (RequestUnit.GetTCPFunction() == TCPFunctions.TCPRead)
                {
                    tcpMaster.ReadHoldingRegister(1, RequestUnit.GetSlaveAddr(), RequestUnit.GetTCPStartAddr(), RequestUnit.GetTCPReadCount());
                }
                else
                {
                    tcpMaster.WriteMultipleRegister(1, RequestUnit.GetSlaveAddr(), RequestUnit.GetTCPStartAddr(), RequestUnit.GetTCPWriteData());
                }
            }

            if (serialPortMode == SerialPortModes.RSMode)
            {
                waitSerialData.WaitOne(TimeSpan.FromMilliseconds(100));

                for (int i = 0; i < 100; i++)
                {
                    if (serialPort.BytesToRead < RequestUnit.ReceivedBytesThreshold)
                    {
                        waitSerialData.WaitOne(TimeSpan.FromMilliseconds(10));
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (serialPortMode == SerialPortModes.TCPMode)
            {
                waitSerialData.WaitOne(TimeSpan.FromMilliseconds(3000));
            }

            bool dataOk = false;

            #region RecieveRS232
            if (serialPortMode == SerialPortModes.RSMode)
            {
                byte[] rxBuffer = new byte[0];

                if (serialPort.BytesToRead >= RequestUnit.ReceivedBytesThreshold)
                {
                    int count = serialPort.BytesToRead;
                    rxBuffer = new byte[count];

                    try
                    {
                        serialPort.Read(rxBuffer, 0, count);
                    }
                    catch
                    {
                        portError = true;
                        if (SerialPortError != null)
                        {
                            SerialPortError(serialPort, null);
                        }
                        return;
                    }

                    dataOk = true;
                }
                else
                {
                    // System.Windows.Forms.MessageBox.Show(RequestUnit.ReceivedBytesThreshold.ToString());
                }

                switch (RequestUnit.PortAnswerType)
                {
                case PortAnswerType.Byte:
                {
                    if (RequestUnit.DataRecieved != null)
                    {
                        RequestUnit.DataRecieved(dataOk, rxBuffer);
                    }
                    break;
                }

                case PortAnswerType.RTU:
                {
                    ushort[] ubuff = new ushort[0];

                    if (!ModBusCRC.CheckCRC(rxBuffer, rxBuffer.Length))
                    {
                        // System.Windows.Forms.MessageBox.Show("Ошибка CRC  " + rxBuffer.Length.ToString());
                        dataOk = false;
                    }
                    else
                    {
                        if (rxBuffer[1] == 0x03)
                        {
                            ModBusCRC.RemoveData(rxBuffer, 3, RequestUnit.RTUReadCount, out ubuff);
                        }
                        else
                        {
                            ubuff = new ushort[0];
                        }
                        dataOk = true;
                    }

                    if (RequestUnit.DataRecievedRTU != null)
                    {
                        RequestUnit.DataRecievedRTU(dataOk, ubuff, RequestUnit.Param);
                    }
                } break;
                }
            }
            #endregion
            #region RecieveTCP
            if (serialPortMode == SerialPortModes.TCPMode)
            {
                if (RequestUnit.GetTCPFunction() == TCPFunctions.TCPRead)
                {
                    dataOk = (tcpReadData != null);
                    ushort[] us = new ushort[0];
                    if (dataOk)
                    {
                        ModBusCRC.RemoveData(tcpReadData, 0, RequestUnit.RTUReadCount, out us);
                    }
                    if (RequestUnit.DataRecievedRTU != null)
                    {
                        RequestUnit.DataRecievedRTU(dataOk, us, RequestUnit.Param);
                    }
                }
                else
                {
                    ushort[] us = new ushort[0];
                    if (RequestUnit.DataRecievedRTU != null)
                    {
                        RequestUnit.DataRecievedRTU(tcpWriteOk, us, RequestUnit.Param);
                    }
                }
            }
            #endregion

            lock (locker)
            {
                bool b = CheckQueue(false);
                portBusy = b;
            }
        }