sitemap link mailform link home

WindowsForms-LED-Lauflicht

Download Visual Studio 2010 Express ISO (CD mit 731 MByte):

Externer Link  http://www.chip.de/downloads/Visual-Studio-2010-Express_36594560.html

Registrierung für Visual Studio Express 2010: PQT8W-68YB2-MPY6C-9JV9X-42WJV

Mit dem Parallelport-Monitor aus "Bauanleitungen" wollen wir
ein LED-Lauflicht programmieren und Binärzahlen von 0 bis 255 anzeigen:

Parallelport-Monitor

Das grafische Interface ist ganz simpel:

WindowsForms

Und hier der Quellcode:

 


 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; // Nicht vergessen - für den DLL-Import!

namespace LEDLauflicht
{
    public partial class Form1 : Form
    {
        // Im HEADER -> Festlegen sprechender Namen:
        public int LPT_PORT_ADDDRESS = 0x0378;
        public int ARRAY_COUNTER = 0; // Zählt bei jedem Timer-Tick eins höher
        public int[] LauflichtArray = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

        public int Counter = 0; // Zählt die Binärzahlen hoch

        public Form1()
        {
            InitializeComponent();
            CParallelPort.Output(0x378, 0);
        }

        private void buttonON_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void buttonOFF_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer2.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int OutputValue = LauflichtArray[ARRAY_COUNTER]; //Auslesen eines Wertes des Arrays
            CParallelPort.Output(0x378, OutputValue);
            ARRAY_COUNTER++; // weiterzählen
            if (ARRAY_COUNTER > 7)
            {
                ARRAY_COUNTER = 0;
            }
        }

        private void buttonCounter_Click(object sender, EventArgs e)
        {
            timer2.Enabled = true;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            CParallelPort.Output(0x378, Counter);
            Counter++; // weiterzählen
            if (Counter > 255)
            {
                Counter = 0;
            }
        }
    }
}

class CParallelPort
{
    // inpout32.dll muss im /bin/ebug-Ordner vorhanden sein!
    // Dieser Code funktioniert nur mit Windows XP
    // Für Windows 7 wird eine 64-Bit-DLL inpoutx64.dll benötigt!
    [DllImport("inpout32.dll", EntryPoint = "Out32")]
    public static extern void Output(int adress, int value);
    [DllImport("inpout32.dll", EntryPoint = "Inp32")]
    public static extern int Input(int adress);
}

 


Lauflicht und Binärzähler

Erstellt am 26. November 2012


 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; // Für Import der DLL!

namespace Uebung1
{
    public partial class Form1 : Form
    {
        // Header für sprechende Namen
        public int LPT_PORT_ADDRESS = 0x0378;
        public int Counter = 0; // Zähler für Binärzahlen
        public int[] StepArray = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
        public int ArrayCounter = 0; // Zählt das Lauflicht weiter...


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer2.Enabled = true; // Timer aktivieren
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false; // Timer aktivieren
            timer2.Enabled = false; // Timer aktivieren
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            CParallelPort.Output(LPT_PORT_ADDRESS, Counter);
            Counter++;
            if (Counter > 255)
            {
                Counter = 0;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true; // Timer aktivieren
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            int OutputValue = StepArray[ArrayCounter]; // Ein Wert wird aus dem Array geholt
            CParallelPort.Output(LPT_PORT_ADDRESS, OutputValue);
            ArrayCounter++;
            Counter++;
            if (ArrayCounter > 7)
            {
                ArrayCounter = 0;
            }

        }

      
    }
}

class CParallelPort
{
    // Bindet inpout32.dll als Klasse ein
    [DllImport("inpout32.dll", EntryPoint = "Out32")]
    public static extern void Output(int adress, int value);
    [DllImport("inpout32.dll", EntryPoint = "Inp32")]
    public static extern void Input(int adress);

}

.

 

Letzte Änderung:
March 23. 2023 21:04:40
«    top    »