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.Windows.Forms; using System.IO; using System.Net; using System.Net.Http; using System.Timers; using Velleman.Kits; using System.Text.RegularExpressions; using System.Management; using System.Collections; namespace LaCourseAVingtBilles { public partial class Accueil : Form { static String cLigneLue = ""; static String cAncienneLigneLue = ""; K8090Board _board; public Accueil() { InitializeComponent(); } private void ButtonCommencer_Click(object sender, EventArgs e) { timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://magli.eu/Pages/Informatique/" + textBoxId.Text + ".txt"); request.MaximumAutomaticRedirections = 4; request.MaximumResponseHeadersLength = 4; request.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream receiveStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); cLigneLue = readStream.ReadToEnd(); if (cLigneLue != cAncienneLigneLue) { int iLong = cLigneLue.Length - 4; textBoxMessage.Text = textBoxMessage.Text + cLigneLue; if (cLigneLue.Substring(iLong - 1, 1) == "1") { FaitTomberUneBille(); } if (cLigneLue.Substring(iLong - 1, 1) == "2") { FaitTomberUneBille(); FaitTomberUneBille(); } cAncienneLigneLue = cLigneLue; } response.Close(); readStream.Close(); } public static string GetPort() { ManagementPath path = new ManagementPath(); ManagementClass devs = null; path.Server = "."; path.NamespacePath = @"root\CIMV2"; path.RelativePath = @"Win32_PnPentity"; using (devs = new ManagementClass(new ManagementScope(path), path, new ObjectGetOptions(null, new TimeSpan(0, 0, 0, 2), true))) { ManagementObjectCollection moc = devs.GetInstances(); var usbCOMDevices = ((IEnumerable)moc).OfType() .Where(i => i.ToString().Contains("USB") && i.Properties["Caption"].Value.ToString().Contains("COM")) .ToList(); if (usbCOMDevices.Count != 1) throw new Exception("Il n'y a pas de carte ou il y en a plusieurs !"); var res = Regex.Match(usbCOMDevices.Single().Properties["Caption"].Value.ToString(), "COM[0-9]"); if (!res.Success) throw new Exception("Format non reconnu !"); string port = res.Value; return port; } } public void FaitTomberUneBille() { // On ouvre et on ferme l'électro-aimant du haut this._board = new Velleman.Kits.K8090Board(this.components); this._board = new K8090Board(GetPort()); this._board.Connect(); this._board.SwitchRelayOn(1); System.Threading.Thread.Sleep(500); this._board.SwitchRelayOff(1); System.Threading.Thread.Sleep(500); // On ouvre et on ferme l'électro-aimant du bas this._board.SwitchRelayOn(2); System.Threading.Thread.Sleep(500); this._board.SwitchRelayOff(2); System.Threading.Thread.Sleep(500); this._board.Disconnect(); } } }