/// <summary> /// Предоставляет метод для соединения оборудования /// по локальному подключению /// </summary> /// <param name="ip">Адрес устройства для подключения</param> /// <param name="port">Порт для подключения</param> private void ConnectionMethod(string ip, int port) { //if (ObjTcpConnect == null) // ObjTcpConnect = new TcpClient(); objClientIpAddress = IPAddress.Parse(ip); foreach (var item in TcpClientColleciton) { objEndPointConnect = (IPEndPoint)item.Client.RemoteEndPoint; if (objEndPointConnect.Address == objClientIpAddress) { MessageBox.Show($"Соединение с утройством по адресу {objEndPointConnect.Address} уже произведено...", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } } TcpClientColleciton.Add(new TcpClient()); try { //ObjTcpConnect.Connect( ip, port ); TcpClientColleciton[TcpClientColleciton.Count - 1].Connect(ip, port); IoC.Get <CollectionViewModels>().ConnectedNetworkStream.Add(TcpClientColleciton[TcpClientColleciton.Count - 1].GetStream()); } catch (Exception objException) { MessageBox.Show(objException.ToString()); return; } finally { if (TcpClientColleciton[TcpClientColleciton.Count - 1].Connected) { ConnectedIP.Add(new ConnectionViewModel() { ConnectedIPString = ip }); ConnectedPage.ConnectedPageCollection.Add(new ConnectedPage() { Name = ip, RefPage = new PulsePage(new PulseDesignModel()) }); MainViewModel.ConnectedButtonCollection.Add( new ConnectedIPButtonViewModel { ConnectedString = ip, OpenConnectedIPProgramPage = new RCommand(() => { var target = ConnectedPage.ConnectedPageCollection .Where(x => x.Name == ip).FirstOrDefault() as ConnectedPage; IoC.Get <CollectionViewModels>().CurrentPage = ConnectedPage.ConnectedPageCollection[ConnectedPage.ConnectedPageCollection.IndexOf(target)].RefPage; }) }); Request.SetSystemValue(TcpClientColleciton[TcpClientColleciton.Count - 1].GetStream(), SystemCommands.Remote); } } }
/// <summary> /// Предоставляет метод отключения связи с выбранным оборудованием /// </summary> private void DisconnectionMethod() { if (TcpClientColleciton[ConnectionIPIndex].Connected) { Request.SetSystemValue(TcpClientColleciton[ConnectionIPIndex].GetStream(), SystemCommands.LoadOff); Request.SetSystemValue(TcpClientColleciton[ConnectionIPIndex].GetStream(), SystemCommands.Local); TcpClientColleciton[ConnectionIPIndex].GetStream().Close(); TcpClientColleciton[ConnectionIPIndex].Close(); TcpClientColleciton[ConnectionIPIndex] = null; TcpClientColleciton.RemoveAt(ConnectionIPIndex); ConnectedPage.ConnectedPageCollection.RemoveAt(ConnectionIPIndex); MainViewModel.ConnectedButtonCollection.RemoveAt(ConnectionIPIndex); ConnectedIP.RemoveAt(ConnectionIPIndex); } }