public SettingWL(Conection parent) { this._Conection = parent; // this.wavelength1 = wavelength; // this.newPort = newPort1; int byteRecieved = _Conection.newPort.ReadBufferSize; Thread.Sleep(500); byte[] buffer = new byte[byteRecieved]; _Conection.newPort.Read(buffer, 0, byteRecieved); string GW1 = ""; for (int i = 0; i <= 50; i++) { GW1 = GW1 + Convert.ToChar(buffer[i]); } var GWarr = GW1.Split("\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); _Conection.GW1_2 = GWarr[2]; _Conection.GWNew.Text = _Conection.GW1_2; _Conection.versionPribor = GWarr[1]; if (_Conection.wavelength1 == Convert.ToString(0) || _Conection.wavelength1 == "") { _Conection.wavelength1 = _Conection.GW1_2; } else { bool dlinavoln = true; if (_Conection.versionPribor.Contains("V")) { if (Convert.ToDouble(_Conection.wavelength1.Replace(".", ",")) < 315) { MessageBox.Show("Установленая длина волны выходит за пределы диапазона спектрофотометра, измените настройки градуировки!"); dlinavoln = false; } if (Convert.ToDouble(_Conection.wavelength1.Replace(".", ",")) > 1050) { MessageBox.Show("Установленая длина волны выходит за пределы диапазона спектрофотометра, измените настройки градуировки!"); dlinavoln = false; } } else { if (_Conection.versionPribor.Contains("U") && _Conection.versionPribor.Contains("2")) { if (Convert.ToDouble(_Conection.wavelength1.Replace(".", ",")) < 190) { MessageBox.Show("Установленая длина волны выходит за пределы диапазона спектрофотометра, измените настройки градуировки!"); dlinavoln = false; } if (Convert.ToDouble(_Conection.wavelength1.Replace(".", ",")) > 1050) { MessageBox.Show("Установленая длина волны выходит за пределы диапазона спектрофотометра, измените настройки градуировки!"); dlinavoln = false; } } else { if (Convert.ToDouble(_Conection.wavelength1.Replace(".", ",")) < 200) { MessageBox.Show("Установленая длина волны выходит за пределы диапазона спектрофотометра, измените настройки градуировки!"); dlinavoln = false; } if (Convert.ToDouble(_Conection.wavelength1.Replace(".", ",")) > 1050) { MessageBox.Show("Установленая длина волны выходит за пределы диапазона спектрофотометра, измените настройки градуировки!"); dlinavoln = false; } } } if (dlinavoln == true) { SW(); } } }
// public SettingPort(bool nonPort, string portsName) public SettingPort(Conection parent) { InitializeComponent(); this._Conection = parent; //this.nonPort = nonPort; //this.portsName = portsName; //CO(); // SW(); // InitializeTimer(); string[] ports = SerialPort.GetPortNames(); try { for (int i = 0; i < ports.Length; i++) { SerialPort newPort = new SerialPort(); // настройки порта (Communication interface) newPort.PortName = ports[i]; newPort.BaudRate = 19200; newPort.DataBits = 8; newPort.Parity = System.IO.Ports.Parity.None; newPort.StopBits = System.IO.Ports.StopBits.One; // Установка таймаутов чтения/записи (read/write timeouts) newPort.ReadTimeout = 100; newPort.WriteTimeout = 100; // newPort.DataReceived += new SerialDataReceivedEventHandler(newPort_DataReceived); newPort.RtsEnable = false; newPort.DtrEnable = true; newPort.Open();// MessageBox.Show("ПОРТ ОТКРЫТ " + newPort.PortName); newPort.Write("^*^\r"); int byteRecieved = newPort.ReadBufferSize; System.Threading.Thread.Sleep(50); byte[] buffer = new byte[byteRecieved]; try { newPort.Read(buffer, 0, byteRecieved); newPort.DiscardInBuffer(); newPort.DiscardOutBuffer(); newPort.Close(); } // Читаем ответ(если ничего не пришло отваливаемся по ReadTimeout = 500 catch (TimeoutException) { /* Девайса нет */ newPort.DiscardInBuffer(); newPort.DiscardOutBuffer(); newPort.Close(); ports[i] = null; ports = ports.Where(x => x != null).ToArray(); i--; } } string s1 = ""; StreamReader fs = new StreamReader(@"openport.port"); string s = ""; s = fs.ReadLine(); s1 = s; fs.Close(); selectPort.Items.Clear(); selectPort.Items.AddRange(ports); if (ports.Length != 0 && s1 == Convert.ToString(0)) { selectPort.SelectedIndex = 0; _Conection.nonPort = true; } else { if (ports.Length != 0 && s != Convert.ToString(0)) { int index = selectPort.FindString(s1); if (index != -1) { selectPort.SelectedIndex = index; _Conection.nonPort = true; } else { selectPort.SelectedIndex = 0; _Conection.nonPort = true; } } else { MessageBox.Show("Подсоедините спектрофотометр и попробуйте подключиться снова!"); _Conection.nonPort = false; Close(); // Dispose(); } } } catch { MessageBox.Show("Порт занят! Освободите порт!"); } }