private HRESULT Scan()
        {
            StringBuilder prefixScanFileName = new StringBuilder(stSerOb.ServerScanDataFolderName + "\\rmu1_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"));
            StringBuilder serverUri          = new StringBuilder(stSerOb.ServerUri);

            IAsyncResult initDeviceRes = null;

            ScanResultError = 0;
            lock (progressBar)
            {
                progressBar.Value = 0;
            }
            nSumBufTotalInt = 0;
            UInt16[] buf_frame     = null;
            UInt32[] buf_frame_int = null;
            //Int32 overlap = (290 * (45 - stSerOb.OverlapZone)) / 45;//110417
            Int32 overlap = 390 - stSerOb.OverlapZone;     //step motor steps 370 - 720

            try
            {
                // XML-RPC initialization RMU1 device on server
                initDeviceRes = delegateInitDevice.BeginInvoke(serverUri, framesCount, stSerOb.VolltageSource, stSerOb.CurrentSource, stSerOb.ExpositionTimeFrame, stSerOb.TargetTimeFrame, overlap, Convert.ToInt32(stSerOb.Reverse), prefixScanFileName, callbackInitDevice, null);

                buf_frame     = new UInt16[width_frame_bmp * height_frame_bmp];
                buf_frame_int = new UInt32[(width_frame_bmp + stSerOb.CropZone * 2) * height_frame_bmp];
            }
            catch (Exception e)
            {
                MessageBox.Show("Error by array initialization or XML-RPC initialization RMU1 device on server. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Result = HRESULT.S_ERROR;
            }

            if (arAvgFrame.Count != 0)
            {
                arAvgFrame.Clear();
            }

            current_frame = 0;
            int   rem;
            Int16 itemsCount = (Int16)Math.DivRem(framesCount, coefficient, out rem);

            if (rem == 0)
            {
                rem = coefficient;
            }
            itemWidthLast = (Int16)(rem * width_frame_bmp);

            if (im != null)
            {
                lock (im)
                {
                    im.Dispose();
                    im = null;
                }
            }
            DisposeBitmapArray(ref bmp);

            if (buf_total_int != null)
            {
                buf_total_int = null;
            }

            // Set timeout in the case of connection check call for prevent server fail
            if (connectionCheckProcessing)
            {
                Debug.WriteLine("connection device end wait");
                Thread.Sleep(timerConnection.Interval / 2);
            }

            stateTimer.Elapsed += OnTimeoutExceptionEvent;

            // Цикл покадрового считывания
            while ((ScanResultError == 0) && (current_frame < framesCount) && (Result == HRESULT.S_OK) && !backgroundWorker.CancellationPending)
            {
                Boolean bIsDownload = false;
                Byte[]  bufP        = null;
                stateTimer.Enabled = true;

                // Download the file with frame data from server
                while ((ScanResultError == 0) && !backgroundWorker.CancellationPending && !bIsDownload)
                {
                    //                 try
                    {
                        String scanFileName = stSerOb.ServerPath + "\\" + prefixScanFileName + "_" + current_frame.ToString();
                        if (File.Exists(@scanFileName))
                        {
                            FDataSave_ReadFile(scanFileName, ref bufP);
                            stateTimer.Stop();
                            bIsDownload = true;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    //                 catch
                    {
                        // Timeout if the file is not ready (until frame is not scanning or error is occured)
                        //                     Thread.Sleep(2000);
                    }
                }     // while

                if ((bufP != null) && (ScanResultError == 0) && !backgroundWorker.CancellationPending)
                {
                    Result = FDataConvertion_BufferConversion(0, ref bufP, ref buf_frame_int);

                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }
                    buf_frame.Initialize();
                    // Заполнение кадра
                    // Create the buffer more size
                    Result = TotalBufCreate();
                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }
                    try
                    {
                        // Добавление кадра в общий буфер
                        if (buf_total_int.Length > 0)
                        {
                            for (int j = 0; j < height_frame_bmp; j++)
                            {
                                Array.Copy(buf_frame_int, ((width_frame_bmp + stSerOb.CropZone + stSerOb.CropZone) * j) + stSerOb.CropZone, buf_total_int, width_frame_bmp * height_frame_bmp * current_frame + width_frame_bmp * j, width_frame_bmp);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error by copy array. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        Result = HRESULT.S_ERROR;
                    }
                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }

                    if (stSerOb.UseAdjustment)
                    {
                        arAvgFrame.Add(new long[2]);
                    }

                    if (bmp == null)
                    {
                        int r;
                        int itemsBmpCount = (Int16)(Math.DivRem(framesCount, coefficient, out r) + (r > 0 ? 1 : 0));
                        bmp = new Bitmap[itemsBmpCount];
                    }

                    int item = Math.DivRem(current_frame, coefficient, out rem);

                    FDataModification dataTransform = new FDataModification();
                    dataTransform.SetMode(Mode);
                    dataTransform.SetAdjustmentParameters(stSerOb.UseAdjustment, item);

                    // Преобразование кадра в 16-битовое число
                    Result = dataTransform.SetBufTotalToBufForBitmap((int)nShift.Value, width_frame_bmp * height_frame_bmp * current_frame, width_frame_bmp * height_frame_bmp, current_frame, ref buf_frame);
                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }

                    // Установка ширины и создание текущей bitmap
                    Int16 itemWidthCurrent = itemWidth;
                    if (rem == 0)
                    {
                        if (item == (bmp.Length - 1))
                        {
                            itemWidthCurrent = itemWidthLast;
                        }

                        bmp[item] = new Bitmap(itemWidthCurrent, height_frame_bmp);
                    }

                    // Добавление кадра в bitmap
                    Result = dataTransform.SetBitmap(current_frame, ref buf_frame, ref bmp[item]);

                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }

                    // Отображение bitmap с новым кадром на image
                    FDataScan_DrawImage(ref item, ref itemWidthCurrent);

                    // Отображение image на экран
                    SetRectShowSize();
                    lock (progressBar)
                    {
                        progressBar.Increment(1);
                    }

                    current_frame++;
                } // if buf != null
            }     // while

            stateTimer.Elapsed -= OnTimeoutExceptionEvent;
            if ((ScanResultError == 0) && (Result == HRESULT.S_OK))
            {
                if (!backgroundWorker.CancellationPending)
                {
                    FDataSave_AutoSaveData();
                    for (int i = 0; i < framesCount; i++)
                    {
                        String scanFileName = stSerOb.ServerPath + prefixScanFileName + "_" + i.ToString();
                        if (File.Exists(scanFileName))
                        {
                            File.Delete(scanFileName);
                        }
                    }
                }
            }

            return(Result);
        }