示例#1
0
        public AThread(ABaseblock _device, int _experimentGroup, ADatabase _db, int _powerDelay, int _commDelay, bool isStartWithInitialize = false)
        {
            try
            {
                // TODO: Complete member initialization
                device = _device;
                powerDelay = _powerDelay;
                commDelay = _commDelay;
                experimentGroup = _experimentGroup;
                db = _db;

                bw.DoWork += bw_DoWork;
                bw.ProgressChanged += bw_ProgressChanged;
                bw.RunWorkerCompleted += bw_RunWorkerCompleted;
                bw.WorkerReportsProgress = true;
                bw.WorkerSupportsCancellation = true;

                foreach (byte channel in device.channelsWithSensors)
                {
                    sensorsData.Add(new Sensor());
                    driver = new ADriver(device.comport, powerDelay);
                }

                if (isStartWithInitialize)
                {
                    Start();
                }
            }
            catch (Exception ex)
            {
                FileWorker.WriteEventFile(DateTime.Now, "AThread", "AThread", ex.Message);
            }
        }
示例#2
0
        public static List<string> CheckComportsForBaseblock(List<string> comportsWithCp210x, int powerDelay)
        {
            try
            {
                List<string> result = new List<string>();
                foreach (string comport in comportsWithCp210x)
                {
                    string error = "";
                    ADriver d = new ADriver(comport, powerDelay);
                    if (d.OpenPort(ref error))
                    {
                        if (d.CheckBaseblock(ref error))
                            result.Add(comport);
                        //else
                        //{
                        //    if(d.CheckSensor(ref error))
                        //        result.Add(comport);
                        //}

                    }
                    else
                    {
                        error = "Не удалось открыть порт: " + comport;
                        FileWorker.WriteEventFile(DateTime.Now, "ASearch", "CheckComportsForBaseblock", error);
                    }
                    if (!d.ClosePort())
                    {
                        error = "Не удалось закрыть порт:" + comport;
                        FileWorker.WriteEventFile(DateTime.Now, "ASearch", "CheckComportsForBaseblock", error);
                    }
                }
                return result;
            }
            catch(Exception ex)
            {
                FileWorker.WriteEventFile(DateTime.Now, "ASearch", "CheckComportsForBaseblock", ex.Message);
                return new List<string>();
            }
        }
        private static int WriteExperiment(ADatabase db, int groupID, int sysChannel, string comport, List<byte> channelsWithSensors, object calibValue, int i)
        {
            try
            {
                ADriver dr = new ADriver(comport, 50);
                string error = "";
                if (dr.OpenPort(ref error))
                {
                    if (dr.SetChannel(channelsWithSensors[i], ref error))
                    {
                        string serial = dr.GetSerial(ref error);
                        if (error != "")
                        {
                            return -1;
                        }
                        double firmware = dr.GetFirmware(ref error);
                        if (error != "")
                        {
                            return -2;
                        }

                        if (!dr.ClosePort())
                        {
                            return -3;
                        }

                        string calibValueString;
                        if (calibValue != null)
                            calibValueString = ConvertToDoubleWithCheck(calibValue.ToString(), -1).ToString();
                        else
                            calibValueString = "null";

                        string sql = "INSERT INTO Experiments (ExperimentGroupsID, SysChannelID, SensorSerial, SensorFirmware, CalibrationValue) VALUES (" + groupID + ","
                            + sysChannel + "," + serial + "," + firmware + "," + calibValueString + ")";
                        NpgsqlCommand command = new NpgsqlCommand(sql, db.Connection);
                        command.ExecuteNonQuery();

                        sql = "SELECT currval(pg_get_serial_sequence('Experiments', 'experimentid'));";
                        NpgsqlCommand readCommand = new NpgsqlCommand(sql, db.Connection);
                        NpgsqlDataReader myreader = readCommand.ExecuteReader();
                        myreader.Read();
                        int result = Convert.ToInt32(myreader[0].ToString());
                        myreader.Close();
                        return result;
                    }
                    else
                    {
                        return -4;
                    }

                }
                else
                {
                    return -5;
                }
            }
            catch (Exception ex)
            {
                FileWorker.WriteEventFile(DateTime.Now, "DatabaseWorker", "WriteExperiment", ex.Message);
                return -1;
            }
        }
示例#4
0
        internal static Dictionary<string, Dictionary<string, byte>> SearchSensors(List<string> comportsWithBaseblocks, int powerDelay)
        {
            try
            {
                Dictionary<string, Dictionary<string, byte>> result = new Dictionary<string, Dictionary<string, byte>>();
                Random r = new Random();
                foreach (string comport in comportsWithBaseblocks)
                {
                    string error = "";
                    string serial = "";
                    Dictionary<string, byte> channels = new Dictionary<string, byte>();
                    ADriver d = new ADriver(comport, powerDelay);
                    if (d.OpenPort(ref error))
                    {
                        //if (d.CheckSensor(ref error))
                        //{
                        //    result.Add(r.Next(0, 65532).ToString() + " (" + comport + ')', null);
                        //}
                        //else
                        //{

                            for (byte i = 0; i < maxChannelsInBaseblock; i++)
                            {
                                if (d.CheckSensor(i, ref error))
                                {
                                    serial = d.GetSerial(ref error);
                                    //serial = "Sensor ";
                                    channels.Add(serial + i.ToString(), i);
                                }
                                d.ResetChannel(ref error);
                            }
                            d.ResetChannel(ref error);
                            if (channels.Count > 0)
                            {
                                result.Add(r.Next(0, 65532).ToString() + " (" + comport + ')', channels);
                            }

                            if (!d.ClosePort())
                            {
                                error = "Не удалось закрыть порт: " + comport;
                                FileWorker.WriteEventFile(DateTime.Now, "ASearch", "SearchSensors", error);
                            }
                        //}
                    }
                    else
                    {
                        error = "Не удалось открыть порт: " + comport;
                        FileWorker.WriteEventFile(DateTime.Now, "ASearch", "SearchSensors", error);
                    }
                }
                return result;
            }
            catch(Exception ex)
            {
                FileWorker.WriteEventFile(DateTime.Now, "ASearch", "SearchSensors", ex.Message);
                return null;
            }
        }
示例#5
0
        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            string device = e.Argument as string;
            int comStart = device.IndexOf('(') + 1;
            int comEnd = device.IndexOf(')');
            int channelStart = device.IndexOf("CH") + 2;
            int channelEnd = device.IndexOf(" Д");
            string comport = device.Substring(comStart, comEnd - comStart);
            byte channel = Convert.ToByte(device.Substring(channelStart, channelEnd - channelStart));
            ADriver dr = new ADriver(comport, 100);
            string error = "";
            if (dr.OpenPort(ref error))
            {
                if (dr.SetChannel(channel, ref error))
                {
                    double[] reflectogram, delays, temperature;
                    double level = 0.0, sum = 0.0, delaySum = 0.0, tempSum = 0.0;
                    for (int i = 0; i < (int)average_numericUpDown.Value; i++)
                    {
                        dr.GetLevel(ref error);
                        reflectogram = dr.GetReflectogram(ref error);
                        temperature = dr.GetTemperature(ref error);
                        delays = ACalculate.CalculateDelays(reflectogram, twoZond ? ADelayCalculationType.TwoZond : ADelayCalculationType.OneZond);
                        level = ACalculate.Level(delays, twoZond ? ACalcType.TwoZond : ACalcType.OneZond);
                        sum += level;
                        delaySum += delays[1];
                        if (temperature != null && temperature.Length > 0)
                            tempSum += temperature[0];
                        List<double> temp = new List<double>();
                        temp.Add(sum);
                        temp.Add(delaySum);
                        temp.Add(tempSum);
                        temp.Add(i+1);

                        bw.ReportProgress(0, temp);
                    }
                }
                dr.ClosePort();
            }
        }
示例#6
0
        private void save_btn_Click(object sender, EventArgs e)
        {
            channel_combobox.Enabled = true;
            average_numericUpDown.Enabled = true;

            string device = channel_combobox.SelectedItem.ToString();
            int comStart = device.IndexOf('(') + 1;
            int comEnd = device.IndexOf(')');
            int channelStart = device.IndexOf("CH") + 2;
            int channelEnd = device.IndexOf(" Д");
            string comport = device.Substring(comStart, comEnd - comStart);
            byte channel = Convert.ToByte(device.Substring(channelStart, channelEnd - channelStart));
            ADriver dr = new ADriver(comport, 100);
            string error = "";
            if (dr.OpenPort(ref error))
            {
                if (dr.SetChannel(channel, ref error))
                {
                    double firmware = dr.GetFirmware(ref error);
                    string serial = dr.GetSerial(ref error);
                    int calibrationGroup =  ACalibrationDatabaseWorker.WriteCalibrationGroup(db, (int)average_numericUpDown.Value, firmware, serial);
                    for(int i = 0; i < dataGridView.RowCount; i++)
                    {
                        if (dataGridView.Rows[i].Cells[0].Value != null && dataGridView.Rows[i].Cells[0].Value.ToString() != "" &&
                            dataGridView.Rows[i].Cells[1].Value != null && dataGridView.Rows[i].Cells[1].Value.ToString() != "")
                        {
                            ACalibrationDatabaseWorker.WriteCalibrationData(db, calibrationGroup,
                                dataGridView.Rows[i].Cells[0].Value.ToString(), dataGridView.Rows[i].Cells[1].Value.ToString());
                        }
                    }
                }
                dr.ClosePort();
            }
        }