public static EthernetListener GetChannel(int port) { try { foreach (Channel c in m_channels) { if (c.GetType() == typeof(EthernetListener)) { EthernetListener listener = c as EthernetListener; if (listener.LocalPort == port) { return(listener); } } } return(null); } catch (ComDriveExceptions ex) { throw; } catch (Exception ex) { string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace; ComDriverLogger.LogExceptions(DateTime.Now, exceptionText); throw; } }
public static Channel GetChannel(Channel channel) { if (channel is Serial) { Serial serial = channel as Serial; return(GetChannel(serial.PortName)); } else if (channel is Ethernet) { Ethernet ethernet = channel as Ethernet; return(GetChannel(ethernet.RemoteIP, ethernet.RemotePort)); } else if (channel is EthernetListener) { EthernetListener listener = channel as EthernetListener; return(GetChannel(listener.LocalPort)); } else if (channel is ListenerServer) { ListenerServer listenerServer = channel as ListenerServer; ListenerServer result; GetChannel(listenerServer.LocalPort, out result); return(result); } else { return(null); } }
public static void GetPLC(EthernetListener ethernetListener) { try { bool isConnected = false; Channel plcChannel = getChannel(ethernetListener, ref isConnected); EthernetListener listener = plcChannel as EthernetListener; if (listener == null) { throw new ComDriveExceptions("Ethernet Listener could not be intialized due to unexpected error", ComDriveExceptions.ComDriveException.UnexpectedError); } listener.Listen(); } catch (ComDriveExceptions ex) { throw; } catch (Exception ex) { string exceptionText = ex.GetType().ToString() + ": " + ex.Message + "\n\n" + ex.StackTrace; ComDriverLogger.LogExceptions(DateTime.Now, exceptionText); throw; } }
internal override bool IsEquivalentChannel(Channel anotherChanel) { EthernetListener ethernetListener = anotherChanel as EthernetListener; if (ethernetListener.LocalPort == m_LocalPort) { return(true); } else { return(false); } }
internal static bool ValidateChannelPropertyChange(EthernetListener channel, int newPort) { if (!m_channels.Contains(channel)) { return(true); } else { EthernetListener listener = GetChannel(newPort); if (listener == null) { return(true); } else { return(false); } } }
private static Channel getChannel(Channel channel, ref bool isConnected) { lock (objectLocker) { if (m_channels.Contains(channel) || channel is ListenerClient) { //there is already a channel with the same configuration try { isConnected = channel.Connected; } catch { } return(channel); } else { if (m_channels.Count == 0) //no channels { try { isConnected = channel.Connected; } catch { } if (channel.GetType() == typeof(EthernetListener)) { EthernetListener listener = channel as EthernetListener; //listener.Listen(); listener.AlreadyInitialized = false; m_channels.Add(channel); return(channel); } else { channel.Connect(); channel.AlreadyInitialized = false; m_channels.Add(channel); return(channel); } } else { foreach (Channel c in m_channels) { if (c.GetType() == channel.GetType()) { if (c.IsEquivalentChannel(channel)) { try { isConnected = c.Connected; } catch { } // if Serial port name is the same or Ethernet IP:Port is the same then... if (c.GetType() == typeof(Serial)) { Serial openedChannel = c as Serial; Serial newChannel = channel as Serial; if (c.Connected == true) { c.AlreadyInitialized = true; if ((openedChannel.PortName == newChannel.PortName) && ((openedChannel.BaudRate != newChannel.BaudRate) || (openedChannel.Retry != newChannel.Retry) || (openedChannel.TimeOut != newChannel.TimeOut) || (openedChannel.Parity != newChannel.Parity) || (openedChannel.DataBits != newChannel.DataBits) || (openedChannel.StopBits != newChannel.StopBits))) { throw new ComDriveExceptions( "Opened Serial connection parameters cannot be modified", ComDriveExceptions.ComDriveException.CommunicationParamsException); } } else { // return the c channel and change its properties c.AlreadyInitialized = false; openedChannel.BaudRate = newChannel.BaudRate; openedChannel.Parity = newChannel.Parity; openedChannel.DataBits = newChannel.DataBits; openedChannel.StopBits = newChannel.StopBits; openedChannel.TimeOut = newChannel.TimeOut; openedChannel.Retry = newChannel.Retry; openedChannel.AutoDetectComParams = newChannel.AutoDetectComParams; openedChannel.Connect(); } } else if (c.GetType() == typeof(Ethernet)) { Ethernet newChannel = channel as Ethernet; Ethernet openedChannel = c as Ethernet; if (c.Connected == true) { c.AlreadyInitialized = true; if ((openedChannel.Retry != newChannel.Retry) || (openedChannel.TimeOut != newChannel.TimeOut) || (openedChannel.Protocol != newChannel.Protocol)) { throw new ComDriveExceptions( "Opened Ethernet parameters cannot be modified", ComDriveExceptions.ComDriveException.CommunicationParamsException); } } else { // return the c channel and change its properties c.AlreadyInitialized = false; openedChannel.Protocol = newChannel.Protocol; openedChannel.TimeOut = newChannel.TimeOut; openedChannel.Retry = newChannel.Retry; openedChannel.Connect(); } } else if (c.GetType() == typeof(EthernetListener)) { EthernetListener newChannel = channel as EthernetListener; EthernetListener openedChannel = c as EthernetListener; if (c.Connected == true) { c.AlreadyInitialized = true; if ((openedChannel.Retry != newChannel.Retry) || (openedChannel.TimeOut != newChannel.TimeOut)) { throw new ComDriveExceptions( "Opened Ethernet parameters cannot be modified", ComDriveExceptions.ComDriveException.CommunicationParamsException); } } else { // return the c channel and change its properties c.AlreadyInitialized = false; openedChannel.TimeOut = newChannel.TimeOut; openedChannel.Retry = newChannel.Retry; //openedChannel.Listen(); } } return(c); } } } isConnected = channel.Connected; if (!channel.Connected) { channel.Connect(); channel.AlreadyInitialized = false; m_channels.Add(channel); return(channel); } return(null); } } } }