Autor Beitrag
IsabelleZimmermann
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Fr 09.11.18 12:14 
Hallo,

ich habe einen Button in meiner Windows Forms anwenden programmiert.
Dieser soll beim 1. Klick die Verbindung zu SPS aufbauen. Das passiert auch.
Beim 2. Klick soll die Verbindung abgebaut werden. Das passiert noch nicht.

Ich weiß schon mal, dass es an meinen if-Bedingungen liegen muss. Normalerweise sollte ich 2 getrennte if-Anweisungen in einer Funktion haben, eine zum Verbindungsaufbau und eine zum Verbindungsabbau. Wenn ich das mache, stürzt aber mein WindowsForms-Fenster ab bei Klick auf den Button.

Mein Quellcode ist hier:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
 public class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        

        public class Form1 : Form
        {
            public Button btn_Verbindung_Ein_Aus;

            public Form1()
            {
                btn_Verbindung_Ein_Aus = new Button();
                btn_Verbindung_Ein_Aus.Text = "Verbindung -> SPS EIN / AUS";
                btn_Verbindung_Ein_Aus.Size = new Size(21050);
                btn_Verbindung_Ein_Aus.Location = new Point(90200); // vorher 377 statt 255
                this.Controls.Add(btn_Verbindung_Ein_Aus);
                btn_Verbindung_Ein_Aus.Click += new EventHandler(btn_Verbindung_Ein_Aus_Click);
                btn_Verbindung_Ein_Aus.Click += new EventHandler(Form_FormClosing); // funktioniert noch nicht.
            }
            
            // Dauerhafte Verbindung zur CPU ein-, bzw. ausschalten
            
            public void SPS_Verbindung_Ein_Aus(string zsEinAus)
            {

                if ((Verbindung_aktiv == false) && (zsEinAus == "Ein"))
                {


                    // Verbindung zur SPS aufbauen
                    if ((Verbindung_aufbauen("TCP", verbindung.ziSPS_Port, verbindung.zsSPS_IpAdr, verbindung.ziSPS_MpiAdrLokal,
                                                       verbindung.ziSPS_MpiAdrPLC, verbindung.ziSPS_Rack, verbindung.ziSPS_Slot, true) < 0) || (Verbindung_aufbauen("TCP", verbindung.ziSPS_Port, verbindung.zsSPS_IpAdr, verbindung.ziSPS_MpiAdrLokal,
                                                       verbindung.ziSPS_MpiAdrPLC, verbindung.ziSPS_Rack, verbindung.ziSPS_Slot, true) > 0))
                    {
                        Application.Exit();
                        //System.Environment.Exit(1);
                    }

                    Verbindung_aktiv = true;
                    
                    // Ablauftimer initialisieren und starten

                    Timer_Ablauf.Interval = 100// tbx_Ablauftakt.Text.ToCharArray().Length;

                    Timer_Ablauf.Enabled = true;

                    // Hintergrundfarbe der Schaltfläche "Verbindung Ein/Aus" grün = Verbindung zur SPS
                    Button btn_Verbindung_Ein_Aus = new Button();
                    btn_Verbindung_Ein_Aus.BackColor = Color.LimeGreen;

                    ziCntListe = 0;

                    



                    if ((Verbindung_aktiv == true) && (zsEinAus == "Aus"))
                    {

                       
                        // Verbindung zur SPS abbauen
                        if (Verbindung_abbauen(true) < 1 || Verbindung_abbauen(true) > 1)
                        {
                            System.Environment.Exit(1);
                        }




                        Verbindung_aktiv = false;

                        // Ablauftimer stoppen
                        Timer_Ablauf.Enabled = false;

                        // Hintergrundfarbe der Schaltfläche "Verbindung Ein/Aus" rot = keine Verbindung zur SPS

                        btn_Verbindung_Ein_Aus.BackColor = Color.Red;

                      
                    }
                }
            



        }

        //"Verbindungsaufbau, Verbindungsabbau, Verbindungsüberprüfung"

            //++++++++++++  Verbindung aufbauen  +++++++++++++++++++
            public int Verbindung_aufbauen(string zsVerbindungsart, int ziPort, string zsIpAdr,
                                             int ziMpiAdrLokal, int ziMpiAdrPLC,
                                             int ziRack, int ziSlot, bool zoMeldAusgabe)
            {

                int ziRetVal;

                //ziSPS_Protokoll ist in Abhängigkeit der Verbindungsart entweder
                //daveProtoISOTCP (TCP) oder daveProtoMPI_IBH (MPI)
                int ziSPS_Protokoll = 0;
                if (zsVerbindungsart == "MPI")
                {
                    ziSPS_Protokoll = libnodave_Net.daveProtoMPI_IBH;
                }
                if (zsVerbindungsart == "TCP")
                {
                    ziSPS_Protokoll = libnodave_Net.daveProtoISOTCP;
                }

                if (zoMeldAusgabe == true)
                {

                    lbx_Meldungen.Items.Add("");
                    lbx_Meldungen.Items.Add("VERBINDUNG AUFBAUEN");
                    
                }


                ziPH = libnodave.openSocket(ziPort, zsIpAdr);
                if (zoMeldAusgabe == true)
                {
                    lbx_Meldungen.Items.Add("openSocket:");
                    lbx_Meldungen.Items.Add(ziPH);
                    
                }

                ziDI = libnodave_Net.daveInterface.daveNewInterface(ziPH, ziPH, "IF1", ziMpiAdrLokal, ziSPS_Protokoll, libnodave_Net.daveSpeed187k);
                if (zoMeldAusgabe == true)
                {
                    lbx_Meldungen.Items.Add("daveNewInterface: ");
                    lbx_Meldungen.Items.Add(ziDI);
                   
                }

                ziRetVal = libnodave_Net.daveInterface.daveInitAdapter(ziDI);
                if (zoMeldAusgabe == true)
                {
                    lbx_Meldungen.Items.Add("daveInitAdapter: ");
                    lbx_Meldungen.Items.Add(ziRetVal);
                    
                }

                ziDC = libnodave_Net.daveConnection.daveNewConnection(ziDI, ziMpiAdrPLC, ziRack, ziSlot);
                if (zoMeldAusgabe == true)
                {
                    lbx_Meldungen.Items.Add("daveNewConnection: ");
                    lbx_Meldungen.Items.Add(ziDC);
                  
                }

                ziRetVal = libnodave_Net.daveConnection.daveConnectPLC(ziDC);
                if (zoMeldAusgabe == true)
                {
                    lbx_Meldungen.Items.Add("daveConnectPLC: ");
                    lbx_Meldungen.Items.Add(ziRetVal);
                    
                }

                if (ziRetVal < 0 || ziRetVal > 0)
                {
                    
                    MessageBox.Show("Verbindung zur SPS konnte nicht aufgebaut werden!""HINWEIS");
                    


                }

                return ziRetVal;
            }

            //++++++++++  Verbindung abbauen  +++++++++++
            public int Verbindung_abbauen(bool zoMeldAusgabe)
            {
                System.IntPtr ziRetVal;
                int ziRetVal_return;

                if (zoMeldAusgabe == true)
                {
                    lbx_Meldungen.Items.Add("");
                    lbx_Meldungen.Items.Add("VERBINDUNG ABBAUEN");
                }

                Automatische_Neuverbindung = false;
                Verbindung_aktiv = false;
                try
                {

                    ziRetVal = (System.IntPtr)libnodave_Net.daveConnection.daveDisconnectPLC(ziDC);
                    if (zoMeldAusgabe == true)
                    {
                        lbx_Meldungen.Items.Add("daveDisconnectPLC: ");
                        lbx_Meldungen.Items.Add(ziRetVal);
                    }
                    libnodave_Net.daveConnection.daveFree(ziDC);

                    //ziDC = 0;

                    ziRetVal = libnodave_Net.daveInterface.daveDisconnectAdapter(ziDI);
                    if (zoMeldAusgabe == true)
                    {
                        lbx_Meldungen.Items.Add("daveDisconnectAdapter: ");
                        lbx_Meldungen.Items.Add(ziRetVal);
                    }
                    libnodave_Net.daveConnection.daveFree(ziDI);
                    //ziDI = 0;

                    if (ziPH > 100)
                    {
                        ziRetVal = (System.IntPtr)libnodave.closePort(ziPH);
                        if (zoMeldAusgabe == true)
                        {
                            lbx_Meldungen.Items.Add("closePort: ");
                            lbx_Meldungen.Items.Add(ziPH);
                            lbx_Meldungen.Items.Add("Ergebnis: ");
                            lbx_Meldungen.Items.Add(ziRetVal);
                        }
                        ziPH = 0;
                    }


                }
                catch (IOException ex)
                {
                    int ziRetValziRetVal_return = 99;
                }
                ziRetVal_return = 1;
                if (ziRetVal_return > 1 || ziRetVal_return < 1)
                {
                    // vbOKOnly, 
                    MessageBox.Show("Die Verbindung konnte nicht beendet werden!""HINWEIS");
                }

                return ziRetVal_return;

            }

            // "SPS_Kommunikation"

            // Dauerhafte Verbindung zur CPU ein-, bzw. ausschalten
            void btn_Verbindung_Ein_Aus_Click(object sender, EventArgs e)
            {


                if (Verbindung_aktiv == false)
                {
                    // Verbindung zur SPS aufbauen
                    SPS_Verbindung_Ein_Aus("Ein");

                }
                else
                {
                    
                    // Verbindung zur SPS abbauen
                    SPS_Verbindung_Ein_Aus("Aus");
                }



            }


Vielleicht sind das auch nur Grundlagen, aber ich hoffe trotzdem dass mir jemand weiterhelfen kann.
Vielen Dank.

Gruß,
Isabelle

Moderiert von user profile iconMathematiker: C#-Tags eingefügt
IsabelleZimmermann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Fr 09.11.18 12:23 
Ich hab auch schon die Funktion SPS_Verbindung_Ein_Aus(string zsEinAus) in den Konstruktor public Form1(){} verschoben, aber das gab dann etliche Fehlermeldungen.

Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 09.11.18 14:58 
ausblenden C#-Quelltext
1:
Button btn_Verbindung_Ein_Aus = new Button();					

Diese Zeile muß weg. Du erstellst einen neuen Button hier. Das ist nicht der gleiche der bereits auf der Form ist nur weil die Methodenvariable genauso heißt wie die Klassenvariable.
New aufrufen bedeutet immer das es auch ein neues Object ist auch bei graphischen Elementen.

Diese Bedingung
ausblenden C#-Quelltext
1:
if ((Verbindung_aktiv == true) && (zsEinAus == "Aus"))					

steckt in dieser
ausblenden C#-Quelltext
1:
if ((Verbindung_aktiv == false) && (zsEinAus == "Ein"))					

heißt der Verbindung_aktiv Fall kann nie erreicht werden. Denn möchtest du vermutlich auch eher im else Zweig haben.

Also eher

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
//Pseudocode
if (Verbindung_aktiv && zsEinAus == "Aus"))
{
...
}
else if (!Verbindung_aktiv && zsEinAus == "Ein"))
{
...
}


Wobei der zsEinAus Parameter völlig überflüssig. Nach deiner Programmierung ist der direkt gekoppelt an die Verbindung_aktiv Variable und kann weg.
Dann bleibt ein simples
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
//Pseudocode
if (Verbindung_aktiv)
{
...
}
else 
{
...
}
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 09.11.18 15:30 
Außerdem fehlt die Zeile
ausblenden C#-Quelltext
1:
InitializeComponent();					

im Konstruktor.
Und warum erstellst du überhaupt per Code den Button und nicht per Designer?

Aber was sehe ich jetzt erst beim Darüberschauen über deinen Code: Warum hast du innerhalb der Hauptklasse Program die Form als Unterklasse erstellt?
Erstelle dafür eine eigene Datei, so daß du dann auch den Designer benutzen kannst.
IsabelleZimmermann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54



BeitragVerfasst: Fr 09.11.18 16:04 
Ok, ich versuche es über den Designer.