void button1_Click(object sender, EventArgs e) { // sprawdzenie ID urządzenia byte deviceId = (byte)numericUpDown1.Value; if (StepperDevice == null || StepperDevice.DeviceId != deviceId) { if (_configuration.ExistsDevice(_interf, deviceId)) { MessageBox.Show(this, string.Format("Podany identyfikator urządzenia jest już używany na tym interfejsie."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); numericUpDown1.Focus(); return; } } string id = StepperDevice == null?Guid.NewGuid().ToString() : StepperDevice.Id; string description = textBox2.Text.Trim(); // sprawdzenie ID string motor1Id = textBox1.Text.Trim(); if (motor1Id.Length == 0) { MessageBox.Show(this, "Nie podano identyfikatora silnika 1.", "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox1.Focus(); return; } string motor2Id = textBox5.Text.Trim(); if (motor2Id.Length == 0) { MessageBox.Show(this, "Nie podano identyfikatora silnika 2.", "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox5.Focus(); return; } // sprawdzenie unikalności identyfikatorów if (StepperDevice == null || StepperDevice.Motor1.Id != motor1Id) { bool jest = false; foreach (StepperDevice sd in _configuration.StepperDevices) { if (sd.Motor1.Id == motor1Id) { jest = true; break; } if (sd.Motor2.Id == motor2Id) { jest = true; break; } } if (jest) { MessageBox.Show(this, "Identyfikator silnika 1 nie jest unikalny.", "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox1.Focus(); return; } } if (StepperDevice == null || StepperDevice.Motor2.Id != motor2Id) { bool jest = false; foreach (StepperDevice sd in _configuration.StepperDevices) { if (sd.Motor1.Id == motor2Id) { jest = true; break; } if (sd.Motor2.Id == motor2Id) { jest = true; break; } } if (jest) { MessageBox.Show(this, "Identyfikator silnika 2 nie jest unikalny.", "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox5.Focus(); return; } } // opisy string motor1Description = textBox3.Text; string motor2Description = textBox4.Text; if (StepperDevice == null) { StepperDevice = new SimpleStepperDevice(); StepperDevice.Motor1 = new StepperMotor(); StepperDevice.Motor2 = new StepperMotor(); List <StepperDevice> sds = new List <StepperDevice>(_configuration.StepperDevices); sds.Add(StepperDevice); _configuration.StepperDevices = sds.ToArray(); } StepperDevice.Description = description; StepperDevice.DeviceId = deviceId; StepperDevice.Id = id; StepperDevice.Interface = _interf; StepperDevice.Motor1.Description = motor1Description; StepperDevice.Motor1.Device = StepperDevice; StepperDevice.Motor1.HasZeroSensor = checkBox1.Checked; StepperDevice.Motor1.Id = motor1Id; StepperDevice.Motor1.InvertZeroSensor = checkBox2.Checked; StepperDevice.Motor1.KeepTourque = false; StepperDevice.Motor1.MinStepInterval = (byte)numericUpDown3.Value; StepperDevice.Motor1.MotorIndex = 0; StepperDevice.Motor1.ReverseDirection = checkBox3.Checked; StepperDevice.Motor1.StepsFor360 = (int)numericUpDown2.Value; StepperDevice.Motor2.Description = motor2Description; StepperDevice.Motor2.Device = StepperDevice; StepperDevice.Motor2.HasZeroSensor = checkBox6.Checked; StepperDevice.Motor2.Id = motor2Id; StepperDevice.Motor2.InvertZeroSensor = checkBox5.Checked; StepperDevice.Motor2.KeepTourque = false; StepperDevice.Motor2.MinStepInterval = (byte)numericUpDown4.Value; StepperDevice.Motor2.MotorIndex = 1; StepperDevice.Motor2.ReverseDirection = checkBox4.Checked; StepperDevice.Motor2.StepsFor360 = (int)numericUpDown5.Value; DialogResult = DialogResult.OK; Close(); }
private void button1_Click(object sender, EventArgs e) { // sprawdzenie poprawności danych byte deviceId = (byte)numericUpDown1.Value; byte ledsCount = (byte)numericUpDown2.Value; string id = _servoDeviceIndex < 0 ? Guid.NewGuid().ToString() : _configuration.ServoDevices[_servoDeviceIndex].Id; string description = textBox2.Text.Trim(); textBox2.Text = description; if (description.Length == 0) { MessageBox.Show(this, string.Format("Nie podano opisu."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox2.Focus(); return; } // sprawdzenie czy na tym interfejsie jest już urządzenie o takim ID if (_servoDeviceIndex == -1 || _configuration.ServoDevices[_servoDeviceIndex].DeviceId != deviceId) { if (_configuration.ExistsDevice(_interf, deviceId)) { MessageBox.Show(this, string.Format("Podany identyfikator urządzenia jest już używany na tym interfejsie."), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); numericUpDown1.Focus(); return; } } // sprawdzenie poprawności identyfikatorów diod List <string> ids = new List <string>(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { string id2 = (string)dataGridView1.Rows[i].Cells[1].Value; id2 = id2.Trim(); if (id2.Length == 0) { MessageBox.Show(this, string.Format("Nie podano identyfikatora dla serwomechanizmu '{0}'.", (i + 1)), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (ids.Contains(id2)) { MessageBox.Show(this, string.Format("Identyfikator '{0}' został użyty więcj niż jeden raz.", id2), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } ids.Add(id2); } // // sprawdzenie czy identyfikatory diod // for (int i = 0; i < _configuration.ServoDevices.Length; i++) // { // ServoDevice servoD = _configuration.ServoDevices[i]; // if (servoD.Id == id) // { // continue; // } // // if (ids.FindIndex(delegate(string o) // { // return o == led.ID; // }) > -1) // { // MessageBox.Show(this, string.Format("Identyfikator '{0}' jest już wykorzystywany.", led.ID), "Uwaga", MessageBoxButtons.OK, MessageBoxIcon.Warning); // return; // } // } if (_servoDeviceIndex > -1) { ServoDevice dev = _configuration.ServoDevices[_servoDeviceIndex]; dev.Description = description; dev.DeviceId = deviceId; for (int i = 0; i < dataGridView1.Rows.Count; i++) { byte dindex = (byte)i;// byte.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()) - 1; string did = (string)dataGridView1.Rows[i].Cells[1].Value; string ddescription = (string)dataGridView1.Rows[i].Cells[2].Value; // aktualizacja ustawień wyświetlacza lub dodanie nowego Servo servo = Array.Find <Servo>(dev.Servos, delegate(Servo o) { return(o.Index == dindex); }); if (servo != null) { servo.Id = did; servo.Description = ddescription; } else { servo = new Servo() { Description = ddescription, Id = did, Index = dindex, Device = dev }; List <Servo> lcds2 = new List <Servo>(dev.Servos); lcds2.Add(servo); dev.Servos = lcds2.ToArray(); } } // // usunięcie diod // List<LED> diodyOld = new List<LED>(_configuration.LEDs); // diodyOld.RemoveAll(delegate(LED o) // { // return o.LEDDevice == dev && o.Index >= dataGridView1.Rows.Count; // }); // _configuration.LEDs = diodyOld.ToArray(); } else { // dodanie nowego urządzenia i wyświetlaczy ServoDevice dev = new ServoDevice() { Description = description, DeviceId = deviceId, Id = id, Interface = _interf }; List <ServoDevice> devsAll = new List <ServoDevice>(_configuration.ServoDevices); devsAll.Add(dev); _configuration.ServoDevices = devsAll.ToArray(); AddedServoDevice = dev; for (int i = 0; i < dataGridView1.Rows.Count; i++) { byte dindex = (byte)i;// byte.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()); string did = (string)dataGridView1.Rows[i].Cells[1].Value; string ddescription = (string)dataGridView1.Rows[i].Cells[2].Value; Servo lcd4 = new Servo() { Description = ddescription, Id = did, Index = dindex, Device = dev }; List <Servo> lcds2 = new List <Servo>(dev.Servos); lcds2.Add(lcd4); dev.Servos = lcds2.ToArray(); } } DialogResult = DialogResult.OK; Close(); }