Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace USB_GUI_Template_0
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         // alles was hier deklariert kann auf allen Ebenen verwendet werden
  16.  
  17.         // Simulation Ausgabe  
  18.         private Label[] Simulation = new Label[8];
  19.         // Blinklicht
  20.         private Timer TimerBlinklicht = new Timer();
  21.         private int counter = 0;
  22.         private bool blinkStatus = false;
  23.         // Lauflicht
  24.         private Timer TimerLauflicht = new Timer();
  25.         private int LauflichtPosition = 0;
  26.         private bool Lauflichtstatus = false;
  27.         public Form1()
  28.         {
  29.             InitializeComponent();
  30.             //Array für Simulation Items belegen
  31.             Simulation[0] = labDigOut1;
  32.             Simulation[1] = labDigOut2;
  33.             Simulation[2] = labDigOut3;
  34.             Simulation[3] = labDigOut4;
  35.             Simulation[4] = labDigOut5;
  36.             Simulation[5] = labDigOut6;
  37.             Simulation[6] = labDigOut7;
  38.             Simulation[7] = labDigOut8;
  39.             //TimerBlinklicht intervall + timer_tick deklarieren
  40.             TimerBlinklicht.Interval = 500;                                     // in ms
  41.             TimerBlinklicht.Tick += new EventHandler(TimerBlinklicht_Tick);     // wird ausgeführt nach ablauf Intervallzeit
  42.             //TimerLauflicht intervall + timer_tick deklarieren
  43.             TimerLauflicht.Interval = 500;
  44.             TimerLauflicht.Tick += new EventHandler(TimerLauflicht_Tick);          
  45.         }
  46.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47.         /// Simulation Ausgabe
  48.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49.         private void SimClearAllDigital()
  50.         { // Simulation Ausgabe: alle Digitalen Ausgänge löschen
  51.             for (int x = 0; x < Simulation.Length; x++)
  52.             {              
  53.                 Simulation[x].BackColor = Color.White;
  54.             }
  55.         }
  56.         private void SimSetAllDigital()
  57.         {// Simualtion Ausgabe : alle Digitalen Ausgänge setzten
  58.             for (int x = 0; x < Simulation.Length; x++)
  59.             {
  60.                 Simulation[x].BackColor = Color.Yellow;
  61.             }
  62.         }
  63.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64.         ///
  65.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66.         private void butVerbinden_Click(object sender, EventArgs e)
  67.         {
  68.             if ( UsbPInvokeWrapper.OpenDevice(0) == 0 )
  69.             {              
  70.                 MessageBox.Show("Verbindung ok", "USB-Board-Status", MessageBoxButtons.OK);
  71.             }
  72.             else
  73.             {              
  74.                 MessageBox.Show("Keine Verbindung möglich", "USB-Board-Status", MessageBoxButtons.OK, MessageBoxIcon.Error);              
  75.             }            
  76.         }
  77.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  78.         ///
  79.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80.         private void butAn_Click(object sender, EventArgs e)
  81.         {                      
  82.                 //Simulation Ausgabe
  83.                 SimSetAllDigital();
  84.                 //USB-Board
  85.                 UsbPInvokeWrapper.SetAllDigital();                        
  86.         }
  87.  
  88.         private void butLöschen_Click(object sender, EventArgs e)
  89.         {
  90.             //Simulation Ausgabe
  91.             SimClearAllDigital();
  92.             //USB-Board
  93.             UsbPInvokeWrapper.ClearAllDigital();
  94.         }
  95.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  96.         /// Blinklicht
  97.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98.         private void butBlinklicht_Click(object sender, EventArgs e)
  99.         {        
  100.             butLauflicht.Enabled = false;
  101.             //Simulation Ausgabe
  102.             SimClearAllDigital();
  103.             //USB-Board
  104.             UsbPInvokeWrapper.ClearAllDigital();
  105.             //
  106.             if (butBlinklicht.Enabled == true)
  107.             {
  108.                 TimerBlinklicht.Start();  // startet timer und läuft durchgehend bis timer.stopp()
  109.             }
  110.             else
  111.             {
  112.                 // wenn counter == 5 , timer stoppen, Lampe aus, counter=0 setzen;      
  113.                 TimerBlinklicht.Stop();
  114.                 blinkStatus = false;
  115.                 //Simulation Ausgabe
  116.                 SimClearAllDigital();
  117.                 //USB-Board
  118.                 UsbPInvokeWrapper.ClearAllDigital();
  119.                 counter = 0;
  120.                 // Button Lauflicht wieder auf true setzen
  121.                 butLauflicht.Enabled = true;
  122.             }
  123.         }
  124.         private void TimerBlinklicht_Tick(object sender, EventArgs e)
  125.         {
  126.             // Zähler für 5 mal Blinken anlegen
  127.             if (counter >= 5)
  128.             {
  129.                 // wenn counter == 5 , timer stoppen, Lampe aus, counter=0 setzen;      
  130.                 TimerBlinklicht.Stop();                
  131.                 blinkStatus = false;                                
  132.                 //Simulation Ausgabe
  133.                 SimClearAllDigital();
  134.                 //USB-Board
  135.                 UsbPInvokeWrapper.ClearAllDigital();
  136.                 counter = 0;
  137.                 // Button Lauflicht wieder auf true setzen
  138.                 butLauflicht.Enabled = true;
  139.             }
  140.             else if (blinkStatus == false)
  141.             {
  142.                 blinkStatus = true;
  143.                 //Simulation Ausgabe
  144.                 SimSetAllDigital();
  145.                 //USB-Board
  146.                 UsbPInvokeWrapper.SetAllDigital();    
  147.                 //
  148.                 counter++;
  149.             }
  150.             else
  151.             {
  152.                 blinkStatus = false;
  153.                 //Simulation Ausgabe
  154.                 SimClearAllDigital();
  155.                 //USB-Board
  156.                 UsbPInvokeWrapper.ClearAllDigital();
  157.  
  158.             }
  159.         }
  160.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  161.         /// Lauflicht
  162.         //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163.         private void butLauflicht_Click(object sender, EventArgs e)
  164.         {
  165.             butBlinklicht.Enabled = false;
  166.             //Simulation Ausgabe
  167.             SimClearAllDigital();
  168.             //USB-Board
  169.             UsbPInvokeWrapper.ClearAllDigital();
  170.             TimerLauflicht.Start();
  171.         }
  172.         private void TimerLauflicht_Tick(object sender, EventArgs e)
  173.         {                    
  174.             if (LauflichtPosition >= 8)                                    
  175.             {
  176.                 // wenn counter == 5 , timer stoppen, Lampe aus, counter=0 setzen;
  177.                 TimerLauflicht.Stop();
  178.                 Lauflichtstatus = false;
  179.                 //Simulation Ausgabe
  180.                 SimClearAllDigital();
  181.                 //USB-Board
  182.                 UsbPInvokeWrapper.ClearAllDigital();
  183.                 LauflichtPosition = 0;
  184.                 // Button Blinklicht wieder auf true setzen
  185.                 butBlinklicht.Enabled = true;
  186.             }
  187.             else if (Lauflichtstatus == false)
  188.             {
  189.                 Lauflichtstatus = true;
  190.                 Simulation[LauflichtPosition].BackColor = Color.Yellow;
  191.                 //USB-Board
  192.                 UsbPInvokeWrapper.SetDigitalChannel(LauflichtPosition+1);
  193.                 LauflichtPosition++;                
  194.             }
  195.             else
  196.             {
  197.                 Lauflichtstatus = false;
  198.                 //Simulation Ausgabe
  199.                 Simulation[LauflichtPosition-1].BackColor = Color.White;
  200.                 //USB-Board
  201.                 UsbPInvokeWrapper.ClearDigitalChannel(LauflichtPosition);
  202.  
  203.             }
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement