public void Connect() { try { IsConnected = true; Console.WriteLine(string.Format("STARTED: {0}", ++COUNTER)); threads = new Thread[Channels.Count]; if (threads == null) { throw new NullReferenceException("No Data"); } for (int i = 0; i < Channels.Count; i++) { threads[i] = new Thread((chParam) => { IDriverAdapter DriverAdapter = null; Channel ch = (Channel)chParam; switch (ch.ChannelTypes) { case "Modbus": switch (ch.Mode) { case "RTU": DriverAdapter = rtu[ch.ChannelName]; break; case "ASCII": DriverAdapter = ascii[ch.ChannelName]; break; case "TCP": DriverAdapter = mbe[ch.ChannelName]; break; } break; case "LSIS": switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = cnet[ch.ChannelName]; break; case "Ethernet": DriverAdapter = FENET[ch.ChannelName]; break; } break; case "Panasonic": DriverAdapter = Panasonic[ch.ChannelName]; break; case "Siemens": switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = _PLCPPI[ch.ChannelName]; break; case "Ethernet": DriverAdapter = _PLCS7[ch.ChannelName]; break; } break; default: break; } DriverAdapter.Connection(); IsConnected = DriverAdapter.IsConnected; while (IsConnected) { foreach (Device dv in ch.Devices) { foreach (DataBlock db in dv.DataBlocks) { if (!IsConnected) { break; } switch (ch.ChannelTypes) { case "Modbus": SendPackageModbus(DriverAdapter, dv, db); break; case "LSIS": SendPackageLSIS(DriverAdapter, ch, dv, db); break; case "Panasonic": SendPackagePanasonic(DriverAdapter, dv, db); break; case "Siemens": SendPackageSiemens(DriverAdapter, dv, db); break; default: break; } } } } }); threads[i].IsBackground = true; threads[i].Start(Channels[i]); } } catch (Exception ex) { EventscadaException?.Invoke(this.GetType().Name, ex.Message); } }
public void Connect() { try { IsConnected = true; taskArray = new Task[Channels.Count]; if (taskArray == null) { throw new NullReferenceException("No Data"); } for (int i = 0; i < Channels.Count; i++) { taskArray[i] = new Task((chParam) => { IDriverAdapter DriverAdapter = null; Channel ch = (Channel)chParam; switch (ch.ConnectionType) { case "SerialPort": DriverAdapter = cnet[ch.ChannelName]; break; case "Ethernet": DriverAdapter = FENET[ch.ChannelName]; break; } //======Connection to PLC================================== DriverAdapter.Connection(); while (IsConnected) { try { try { foreach (Device dv in ch.Devices) { foreach (DataBlock db in dv.DataBlocks) { if (!IsConnected) { break; } SendPackageLSIS(DriverAdapter, db); } } } catch (Exception ex) { Disconnect(); EventscadaException?.Invoke(GetType().Name, ex.Message); } } catch (Exception) { Disconnect(); objConnectionState = ConnectionState.DISCONNECT; eventConnectionState?.Invoke(objConnectionState, string.Format("Server disconnect with PLC.")); } if (IsConnected && objConnectionState == ConnectionState.DISCONNECT) { objConnectionState = ConnectionState.CONNECT; eventConnectionState?.Invoke(objConnectionState, string.Format("PLC connected to Server.")); } else if (!IsConnected && objConnectionState == ConnectionState.CONNECT) { objConnectionState = ConnectionState.DISCONNECT; eventConnectionState?.Invoke(objConnectionState, string.Format("Server disconnect with PLC.")); } } }, Channels[i]); taskArray[i].Start(); foreach (Task task in taskArray) { Channel data = task.AsyncState as Channel; if (data != null) { EventscadaException?.Invoke(GetType().Name, $"Task #{data.ChannelId} created at {data.ChannelName}, ran on thread #{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}."); } } } } catch (Exception ex) { EventscadaException?.Invoke(GetType().Name, ex.Message); } }