I koden som følger er meningen at jeg skal sette "digitalchannel" aktiv slik som dette:
den skal aktivere 1,2,3,4,5,6,7, og 7,6,5,4,3,2 og 1 i loop.
Den skal også slå av den foregående den slo på.
Så outputtet på vellemann brettet blinker 1 etter en opp og ned.
den skal aktivere 1,2,3,4,5,6,7, og 7,6,5,4,3,2 og 1 i loop.
Den skal også slå av den foregående den slo på.
Så outputtet på vellemann brettet blinker 1 etter en opp og ned.
Kode
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Windows.Forms; using System.Runtime.InteropServices; namespace testmedformsapp { public partial class Form1 : Form { Thread workerThread = null; ManualResetEvent threadInterrupt = new ManualResetEvent(false); public Form1() { InitializeComponent(); } [DllImport("k8055d.dll")] public static extern int OpenDevice(int CardAddress); [DllImport("k8055d.dll")] public static extern void CloseDevice(); [DllImport("k8055d.dll")] public static extern int ReadAnalogChannel(int Channel); [DllImport("k8055d.dll")] public static extern void ReadAllAnalog(ref int Data1, ref int Data2); [DllImport("k8055d.dll")] public static extern void OutputAnalogChannel(int Channel, int Data); [DllImport("k8055d.dll")] public static extern void OutputAllAnalog(int Data1, int Data2); [DllImport("k8055d.dll")] public static extern void ClearAnalogChannel(int Channel); [DllImport("k8055d.dll")] public static extern void SetAllAnalog(); [DllImport("k8055d.dll")] public static extern void ClearAllAnalog(); [DllImport("k8055d.dll")] public static extern void SetAnalogChannel(int Channel); [DllImport("k8055d.dll")] public static extern void WriteAllDigital(int Data); [DllImport("k8055d.dll")] public static extern void ClearDigitalChannel(int Channel); [DllImport("k8055d.dll")] public static extern void ClearAllDigital(); [DllImport("k8055d.dll")] public static extern void SetDigitalChannel(int Channel); [DllImport("k8055d.dll")] public static extern void SetAllDigital(); [DllImport("k8055d.dll")] public static extern bool ReadDigitalChannel(int Channel); [DllImport("k8055d.dll")] public static extern int ReadAllDigital(); [DllImport("k8055d.dll")] public static extern int ReadCounter(int CounterNr); [DllImport("k8055d.dll")] public static extern void ResetCounter(int CounterNr); [DllImport("k8055d.dll")] public static extern void SetCounterDebounceTime(int CounterNr, int DebounceTime); [DllImport("k8055d.dll")] public static extern int Version(); [DllImport("k8055d.dll")] public static extern int SearchDevices(); [DllImport("k8055d.dll")] public static extern int SetCurrentDevice(int lngCardAddress); [DllImport("k8055d.dll")] public static extern int ReadBackDigitalOut(); [DllImport("k8055d.dll")] public static extern void ReadBackAnalogOut(int[] Buffer); private void button1_Click(object sender, EventArgs e) { OpenDevice(0); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { CloseDevice(); } private void timer2_Tick(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { if (this.workerThread == null) { this.threadInterrupt.Reset(); this.workerThread = new Thread(() => { int i = 0; while (!this.threadInterrupt.WaitOne(0)) { SetDigitalChannel(i); ClearDigitalChannel(i - 1); Thread.Sleep(100); if (i==8) { i = 0; } else { i = i++; } } this.workerThread = null; // worker thread finished in here.. }); this.workerThread.IsBackground = true; // start worker thread in here this.workerThread.Start(); } else { // stop worker thread in here threadInterrupt.Set(); } } } }