private void DeviceThread(DataRow dr)
        {
            DateTime SrvDate;
            DateTime ddd;
            Boolean  DeviceOK;
            Int16    archType_moment = 1;
            Int16    ncall           = 0;
            Int16    nmaxcall        = 5;
            Int16    minrepeat       = 5;

            try
            {
                #region "init"
                ncall     = Convert.ToInt16(dr["ncall"]);
                nmaxcall  = Convert.ToInt16(dr["nmaxcall"]);
                minrepeat = Convert.ToInt16(dr["minrepeat"]);
                Int32 id_bdc;

                id_bdc = Convert.ToInt32(dr["id_bd"].ToString());

                SrvDate = DateTime.Now;
                try
                {
                    SrvDate = Convert.ToDateTime(dr["ServerDate"].ToString());
                }
                catch {
                }



                {
                    TvMain.ClearDuration();
                    if (TvMain.LockDevice(id_bdc, 60 * 40, false))
                    {
                        if (TvMain.DeviceInit(id_bdc))
                        {
                            DeviceOK = true;
                            //TvMain.SaveLog(id_bdc,0,"??",1,"Инициализация транспортного уровня:OK");
                        }
                        else
                        {
                            bool SkipErr = false;
                            if (TvMain.TVD != null)
                            {
                                if (TvMain.TVD.Transport == 0)
                                {
                                    if (TvMain.TVD.ComPort == "")
                                    {
                                        SkipErr = true;
                                    }
                                    if (TvMain.PortBusy)
                                    {
                                        SkipErr = true;
                                    }
                                }
                            }
                            if (!SkipErr)
                            {
                                string tError = "";
                                try
                                {
                                    tError = TvMain.ConnectStatus();
                                }
                                catch (Exception)
                                {
                                    tError = "";
                                }


                                if (tError != "")
                                {
                                    TvMain.WriteErrToDB(id_bdc, DateTime.Now, tError);
                                    TvMain.SaveLog(id_bdc, 0, "??", 1, tError);
                                }
                                else
                                {
                                    if (TvMain.TVD != null)
                                    {
                                        if (TvMain.TVD.DriverTransport != null)
                                        {
                                            tError = TvMain.TVD.DriverTransport.GetError;
                                        }
                                    }
                                    TvMain.WriteErrToDB(id_bdc, DateTime.Now, "Ошибка транспорта. " + tError);
                                    TvMain.SaveLog(id_bdc, 0, "??", 1, "Ошибка транспорта. " + tError);
                                }
                            }
                            DeviceOK = false;
                            TvMain.UnLockDevice(id_bdc);
                            TvMain.SaveLog(id_bdc, 0, "??", 1, "Снятие блокировки устройства");
                        }
                    }
                    else
                    {
                        TvMain.SaveLog(id_bdc, 0, "??", 1, "Тепловычислитель занят");
                        DeviceOK = false;
                    }
                    if (DeviceOK)
                    {
                        TvMain.connect();
                        if (TvMain.isConnected() == false)
                        {
                            string tError = "";
                            try
                            {
                                tError = TvMain.ConnectStatus();
                            }
                            catch (Exception)
                            {
                                tError = "";
                            }
                            if (tError != "")
                            {
                                TvMain.WriteErrToDB(id_bdc, DateTime.Now, tError);
                                TvMain.SaveLog(id_bdc, 0, "??", 1, tError);
                            }
                            else
                            {
                                TvMain.WriteErrToDB(id_bdc, DateTime.Now, "Ошибка соединения. " + tError);
                                TvMain.SaveLog(id_bdc, 0, "??", 1, "Ошибка соединения. " + tError);
                            }

                            if (ncall + 1 < nmaxcall)
                            {
                                TvMain.SetNCALLToPlanCall(id_bdc.ToString(), ncall + 1);
                                TvMain.SetTimeToPlanCall(id_bdc.ToString(), "dlock", DateTime.Now);
                            }
                            else
                            {
                                TvMain.SetNCALLToPlanCall(id_bdc.ToString(), 0);

                                ddd = SrvDate;
                                try
                                {
                                    ddd = Convert.ToDateTime(dr["dnextcurr"].ToString());
                                }
                                catch (System.Exception ex)
                                {
                                    InfoReport("Прибор ID=  " + id_bdc.ToString() + " error converting dnextcurr :" + dr["dnextcurr"].ToString());
                                    TvMain.SaveLog(id_bdc, 3, "??", 0, "Ошибка преобразования даты (dnextcurr) :" + dr["dnextcurr"].ToString() + " " + ex.Message);
                                }
                                while (ddd < SrvDate)
                                {
                                    ddd = ddd.AddHours(1);
                                }
                                ddd = ddd.AddMinutes(-minrepeat);

                                TvMain.SetTimeToPlanCall(id_bdc.ToString(), "dlock", ddd);
                            }

                            ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " Counter initialization Error!");
                            DeviceOK = false;
                        }
                        else
                        {
                            TvMain.SaveLog(id_bdc, 0, "??", 1, "Соединение с тепловычислителем:OK");
                        }
                    }
                    #endregion "init"
                    if (DeviceOK)
                    {
                        TvMain.SetNCALLToPlanCall(id_bdc.ToString(), 0);


                        #region "moment"


                        ddd = SrvDate;

                        if (TvMain.TVD.IsConnected())
                        {
                            InfoReport("Прибор ID=  " + id_bdc.ToString() + " чтение текущих архивов на " + ddd.ToString());
                            try
                            {
                                if (TvMain.LockDevice(id_bdc, 400, true))
                                {
                                    TvMain.HoldLine();
                                    String str;
                                    TvMain.ClearDuration();
                                    str = TvMain.readmarch();
                                    if (str.Length == 0)
                                    {
                                        TvMain.WriteErrToDB(id_bdc, SrvDate, "Ошибка чтения архива");
                                        TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Ошибка чтения текущего архива");
                                    }
                                    else
                                    {
                                        if (str.Substring(0, 6) != "Ошибка")
                                        {
                                            if (TvMain.TVD.isMArchToDBWrite)
                                            {
                                                TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Текущий архив" + ":OK");
                                                TvMain.WritemArchToDB();
                                            }
                                            //);


                                            TvMain.SetTimeToPlanCall(id_bdc.ToString(), "dnextcurr", SrvDate.AddMilliseconds(500));
                                        }
                                        else
                                        {
                                            TvMain.WriteErrToDB(id_bdc, SrvDate, str);
                                            TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Ошибка чтения текущего архива " + str);



                                            TvMain.SetTimeToPlanCall(id_bdc.ToString(), "dnextcurr", SrvDate.AddMilliseconds(500));
                                        }
                                    }
                                    //TvMain.UnLockDevice(id_bdc);
                                }
                            }//try
                            catch (Exception Ex)
                            {
                                TvMain.WriteErrToDB(id_bdc, SrvDate, "Ошибка чтения архива:" + Ex.Message);
                                ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " read arch failed, " + Ex.Message);
                            }
                        }//if (ccurr)



                        #endregion "moment"



                        #region "commands"
                        if (TvMain.TVD.IsConnected())
                        {
                            InfoReport("Прибор ID=  " + id_bdc.ToString() + " Отработка очереди комманд ");
                            try
                            {
                                if (TvMain.LockDevice(id_bdc, 2000, true))
                                {
                                    TvMain.HoldLine();
                                    TvMain.ClearDuration();
                                    if (TvMain.TVD.ProcessComands() > 0)
                                    {
                                        TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Отработка очереди комманд: OK");
                                    }
                                }
                            }//try
                            catch (Exception Ex)
                            {
                                TvMain.SaveLog(id_bdc, archType_moment, "??", 1, "Отработка очереди комманд:" + Ex.Message);
                                ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " commands processing error, " + Ex.Message);
                            }
                        }
                        #endregion

                        TvMain.UnLockDevice(id_bdc);
                        string transpname = "";
                        if (TvMain.TVD != null)
                        {
                            if (TvMain.TVD.Transport == 0)
                            {
                                transpname = TvMain.TVD.ComPort;
                            }
                        }
                        TvMain.SaveLog(id_bdc, 0, "??", 1, "Закрытие канала. " + transpname);
                    }
                    else
                    {
                        ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " transport initialization Error!");
                    }
                }
            }
            catch (System.Exception threadEx)
            {
                ErrorReport("Прибор ID " + dr["id_bd"].ToString() + " thread failed, " + threadEx.Message);
            }
        }