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 {
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(210, 50); btn_Verbindung_Ein_Aus.Location = new Point(90, 200); 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); } public void SPS_Verbindung_Ein_Aus(string zsEinAus) {
if ((Verbindung_aktiv == false) && (zsEinAus == "Ein")) {
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(); }
Verbindung_aktiv = true; Timer_Ablauf.Interval = 100; Timer_Ablauf.Enabled = true;
Button btn_Verbindung_Ein_Aus = new Button(); btn_Verbindung_Ein_Aus.BackColor = Color.LimeGreen;
ziCntListe = 0;
if ((Verbindung_aktiv == true) && (zsEinAus == "Aus")) {
if (Verbindung_abbauen(true) < 1 || Verbindung_abbauen(true) > 1) { System.Environment.Exit(1); }
Verbindung_aktiv = false;
Timer_Ablauf.Enabled = false;
btn_Verbindung_Ein_Aus.BackColor = Color.Red;
} }
}
public int Verbindung_aufbauen(string zsVerbindungsart, int ziPort, string zsIpAdr, int ziMpiAdrLokal, int ziMpiAdrPLC, int ziRack, int ziSlot, bool zoMeldAusgabe) {
int ziRetVal;
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; }
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);
ziRetVal = libnodave_Net.daveInterface.daveDisconnectAdapter(ziDI); if (zoMeldAusgabe == true) { lbx_Meldungen.Items.Add("daveDisconnectAdapter: "); lbx_Meldungen.Items.Add(ziRetVal); } libnodave_Net.daveConnection.daveFree(ziDI); 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) { MessageBox.Show("Die Verbindung konnte nicht beendet werden!", "HINWEIS"); }
return ziRetVal_return;
}
void btn_Verbindung_Ein_Aus_Click(object sender, EventArgs e) {
if (Verbindung_aktiv == false) { SPS_Verbindung_Ein_Aus("Ein");
} else { SPS_Verbindung_Ein_Aus("Aus"); }
} |