// ------------------------------------------------------------------------
        // Create modbus header for write action
        private byte[] CreateWriteHeader(ushort id, byte unit, ushort startAddress, ushort numData, ushort numBytes, EnumModbusFunctionCode function)
        {
            byte[] data = new byte[numBytes + 11];

            byte[] _id = BitConverter.GetBytes((short)id);
            data[0] = _id[1];                           // Slave id high byte
            data[1] = _id[0];                           // Slave id low byte
            byte[] _size = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)(5 + numBytes)));
            data[4] = _size[0];                         // Complete message size in bytes
            data[5] = _size[1];                         // Complete message size in bytes
            data[6] = unit;                             // Slave address
            data[7] = (byte)function;                   // Function code
            byte[] _adr = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)startAddress));
            data[8] = _adr[0];                          // Start address
            data[9] = _adr[1];                          // Start address
            if (function >= EnumModbusFunctionCode.WriteMultipleCoils)
            {
                byte[] _cnt = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)numData));
                data[10] = _cnt[0];                     // Number of bytes
                data[11] = _cnt[1];                     // Number of bytes
                data[12] = (byte)(numBytes - 2);
            }
            return(data);
        }
        // ------------------------------------------------------------------------
        // Create modbus header for read action
        private byte[] CreateReadHeader(ushort id, byte unit, ushort startAddress, ushort length, EnumModbusFunctionCode function)
        {
            byte[] data = new byte[12];

            byte[] _id = BitConverter.GetBytes((short)id);
            data[0] = _id[1];                       // Slave id high byte
            data[1] = _id[0];                       // Slave id low byte
            data[5] = 6;                            // Message size
            data[6] = unit;                         // Slave address
            data[7] = (byte)function;               // Function code
            byte[] _adr = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)startAddress));
            data[8] = _adr[0];                      // Start address
            data[9] = _adr[1];                      // Start address
            byte[] _length = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)length));
            data[10] = _length[0];                  // Number of data to read
            data[11] = _length[1];                  // Number of data to read
            return(data);
        }