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; } }
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; } }
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(); } }