private void cmdAddTCPIPConnection_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("This Connection type is for Communication via AG_SEND/AG_RECIEVE to a siemens PLC, for use with direct reading over TCP/IP use the PLC-Connection !");
            string val = "Connection_" + (grdConnections.Items.Count + 1);

            if (DotNetSiemensPLCToolBoxLibrary.General.InputBox.Show("Connectionname", "Name of the Connection", ref val) == DialogResult.OK)
            {
                foreach (ConnectionConfig plcConnectionConfiguration in ProtokollerConfiguration.ActualConfigInstance.Connections)
                {
                    if (plcConnectionConfiguration.Name.ToLower().Trim() == val.ToLower().Trim())
                    {
                        MessageBox.Show("A Connection with this Name already Exists!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }

                TCPIPConfig myConfig = new TCPIPConfig()
                {
                    Name = val
                };
                ProtokollerConfiguration.ActualConfigInstance.Connections.Add(myConfig);
                //grdConnections.Items.Add(myConfig);
            }
        }
Пример #2
0
        private void OpenStoragesAndCreateTriggers(bool CreateTriggers, bool StartedAsService)
        {
            foreach (DatasetConfig datasetConfig in akConfig.Datasets)
            {
                try
                {
                    IDBInterface akDBInterface = null;

                    akDBInterface = StorageHelper.GetStorage(datasetConfig, RemotingServer.ClientComms.CallNotifyEvent);
                    akDBInterface.ThreadExceptionOccured += new ThreadExceptionEventHandler(tmpTrigger_ThreadExceptionOccured);

                    DatabaseInterfaces.Add(datasetConfig, akDBInterface);

                    Logging.LogText("DB Interface: " + datasetConfig.Name + " is starting...", Logging.LogLevel.Information);

                    akDBInterface.Initiate(datasetConfig);

                    if (CreateTriggers)
                    {
                        if (datasetConfig.Trigger == DatasetTriggerType.Tags_Handshake_Trigger)
                        {
                            PLCTagTriggerThread tmpTrigger = new PLCTagTriggerThread(akDBInterface, datasetConfig, ConnectionList, StartedAsService);
                            tmpTrigger.StartTrigger();
                            tmpTrigger.ThreadExceptionOccured += tmpTrigger_ThreadExceptionOccured;
                            myDisposables.Add(tmpTrigger);
                        }
                        else if (datasetConfig.Trigger == DatasetTriggerType.Time_Trigger)
                        {
                            TimeTriggerThread tmpTrigger = new TimeTriggerThread(akDBInterface, datasetConfig, ConnectionList, StartedAsService);
                            tmpTrigger.StartTrigger();
                            tmpTrigger.ThreadExceptionOccured += tmpTrigger_ThreadExceptionOccured;
                            myDisposables.Add(tmpTrigger);
                        }
                        else if (datasetConfig.Trigger == DatasetTriggerType.Time_Trigger_With_Value_Comparison)
                        {
                            TimeTriggerWithCheckForChangesThread tmpTrigger = new TimeTriggerWithCheckForChangesThread(akDBInterface, datasetConfig, ConnectionList, StartedAsService);
                            tmpTrigger.StartTrigger();
                            tmpTrigger.ThreadExceptionOccured += tmpTrigger_ThreadExceptionOccured;
                            myDisposables.Add(tmpTrigger);
                        }
                        else if (datasetConfig.Trigger == DatasetTriggerType.Quartz_Trigger)
                        {
                            QuartzTriggerThread tmpTrigger = new QuartzTriggerThread(akDBInterface, datasetConfig, ConnectionList, StartedAsService);
                            tmpTrigger.StartTrigger();
                            tmpTrigger.ThreadExceptionOccured += tmpTrigger_ThreadExceptionOccured;
                            myDisposables.Add(tmpTrigger);
                        }
                        else if (datasetConfig.Trigger == DatasetTriggerType.Triggered_By_Incoming_Data_On_A_TCPIP_Connection)
                        {
                            TCPIPConfig tcpipConnConf = datasetConfig.TriggerConnection as TCPIPConfig;

                            tcpipConnConf.MultiTelegramme = tcpipConnConf.MultiTelegramme <= 0 ? 1 : tcpipConnConf.MultiTelegramme;

                            if (tcpipConnConf.MultiTelegramme == 0)
                            {
                                tcpipConnConf.MultiTelegramme = 1;
                            }
                            TCPFunctionsAsync tmpConn = new TCPFunctionsAsync(null, tcpipConnConf.IPasIPAddress, tcpipConnConf.Port, !tcpipConnConf.PassiveConnection, tcpipConnConf.DontUseFixedTCPLength ? -1 : ReadData.GetCountOfBytesToRead(datasetConfig.DatasetConfigRows) * tcpipConnConf.MultiTelegramme);
                            tmpConn.AllowMultipleClients          = tcpipConnConf.AcceptMultipleConnections;
                            tmpConn.UseKeepAlive                  = tcpipConnConf.UseTcpKeepAlive;
                            tmpConn.AsynchronousExceptionOccured += tmpTrigger_ThreadExceptionOccured;
                            tmpConn.AutoReConnect                 = true;
                            var conf = datasetConfig;
                            tmpConn.DataRecieved += (bytes, tcpClient) =>
                            {
                                if (tcpipConnConf.MultiTelegramme == 0)
                                {
                                    tcpipConnConf.MultiTelegramme = 1;
                                }
                                for (int j = 1; j <= tcpipConnConf.MultiTelegramme; j++)
                                {
                                    var    ln     = bytes.Length / tcpipConnConf.MultiTelegramme;
                                    byte[] tmpArr = new byte[ln];
                                    Array.Copy(bytes, ((j - 1) * ln), tmpArr, 0, ln);

                                    IEnumerable <object> values = ReadData.ReadDataFromByteBuffer(conf, conf.DatasetConfigRows, tmpArr, StartedAsService);
                                    if (values != null)
                                    {
                                        akDBInterface.Write(values);
                                    }
                                }
                            };
                            tmpConn.ConnectionEstablished += (TcpClient tcp) =>
                            {
                                Logging.LogText("Connection established: " + tcpipConnConf.IPasIPAddress + ", " + tcpipConnConf.Port, Logging.LogLevel.Information);
                            };
                            tmpConn.ConnectionClosed += (TcpClient tcp) =>
                            {
                                Logging.LogText("Connection closed: " + tcpipConnConf.IPasIPAddress + ", " + tcpipConnConf.Port, Logging.LogLevel.Information);
                            };
                            Logging.LogText("Connection prepared: " + tcpipConnConf.IPasIPAddress + ", " + tcpipConnConf.Port, Logging.LogLevel.Information);
                            tmpConn.Start();
                            ConnectionList.Add(tcpipConnConf, tmpConn);
                            myDisposables.Add(tmpConn);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogText("Error in OpenStorragesAndCreateTriggers occured!", ex, Logging.LogLevel.Error);
                }
            }
        }
Пример #3
0
        private void EstablishConnections()
        {
            foreach (ConnectionConfig connectionConfig in akConfig.Connections)
            {
                LibNoDaveConfig plcConnConf   = connectionConfig as LibNoDaveConfig;
                TCPIPConfig     tcpipConnConf = connectionConfig as TCPIPConfig;
                DatabaseConfig  dbConnConf    = connectionConfig as DatabaseConfig;


                if (plcConnConf != null)
                {
                    Logging.LogText("Connection: " + connectionConfig.Name + " is starting...", Logging.LogLevel.Information);

                    PLCConnection tmpConn = new PLCConnection(plcConnConf.Configuration);
                    try
                    {
                        tmpConn.Connect();
                        if (!plcConnConf.StayConnected)
                        {
                            tmpConn.Disconnect();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.LogText("Connection: " + connectionConfig.Name, ex, Logging.LogLevel.Warning);
                    }

                    ConnectionList.Add(connectionConfig, tmpConn);
                }
                else if (dbConnConf != null)
                {
                    var tmpConn = new DatabaseConnection(dbConnConf);
                    try
                    {
                        tmpConn.Connect();
                    }
                    catch (Exception ex)
                    {
                        Logging.LogText("Connection: " + connectionConfig.Name, ex, Logging.LogLevel.Warning);
                    }
                    ConnectionList.Add(connectionConfig, tmpConn);
                }
                else if (tcpipConnConf != null)
                {
                    //todo: legth of tcp conn
                    //TCPFunctionsAsync tmpConn = new TCPFunctionsAsync(new SynchronizationContext(), tcpipConnConf.IPasIPAddres, tcpipConnConf.Port, !tcpipConnConf.PassiveConnection, 0);

                    //tmpConn.Connect();

                    //ConnectionList.Add(connectionConfig, tmpConn);
                }
            }

            myReEstablishConnectionsThreads = new List <Thread>();
            foreach (ConnectionConfig connectionConfig in akConfig.Connections)
            {
                if (connectionConfig is LibNoDaveConfig)
                {
                    var thrd = new Thread(new ParameterizedThreadStart(ReEstablishConnectionsThreadProc))
                    {
                        Name = "EstablishConnectionsThreadProc"
                    };
                    thrd.Start(connectionConfig as LibNoDaveConfig);
                    this.myReEstablishConnectionsThreads.Add(thrd);
                }
            }
        }