Code :
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 UsbLibrary; namespace USB_Term { public partial class Form1 : Form { byte[] RecievedData = new byte[65]; byte Tam = 0; byte Onda = 0; byte Tam0 = 0; byte Tam00 = 0; bool statut = false; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.usbHidPort1.ProductId = Int32.Parse("1111", System.Globalization.NumberStyles.HexNumber); this.usbHidPort1.VendorId = Int32.Parse("1111", System.Globalization.NumberStyles.HexNumber); usbHidPort1.CheckDevicePresent(); if (statut) { toolStripStatusLabel1.ForeColor = Color.Green; toolStripStatusLabel1.Text = "USB connecté"; } else { toolStripStatusLabel1.ForeColor = Color.Red; toolStripStatusLabel1.Text = "USB non connecté"; } } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); usbHidPort1.RegisterHandle(Handle); } protected override void WndProc(ref Message m) { usbHidPort1.ParseMessages(ref m); base.WndProc(ref m); } private void usbHidPort1_OnDataRecieved(object sender, DataRecievedEventArgs args) { if (InvokeRequired) { try { Invoke(new DataRecievedEventHandler(usbHidPort1_OnDataRecieved), new object[] { sender, args }); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } // else { usbHidPort1_OnSpecifiedDeviceArrived(sender, args); RecievedData = args.data; Tam = RecievedData[2]; Onda = RecievedData[3]; label2.Text = Tam.ToString() + "," + Onda.ToString()+"°C"; Tam = RecievedData[4]; Onda = RecievedData[5]; label3.Text = Tam.ToString() + "," + Onda.ToString() + " Bar"; Tam = RecievedData[6]; Tam0 = RecievedData[7]; Tam00 = RecievedData[8]; Onda = RecievedData[9]; label5.Text = Tam.ToString() +Tam0.ToString() + Tam00.ToString()+ "," + Onda.ToString() + " m^3/h"; } } private void usbHidPort1_OnDataSend(object sender, EventArgs e) { } private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e) { } private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e) { } private void usbHidPort1_OnSpecifiedDeviceArrived(object sender, EventArgs e) { toolStripStatusLabel1.ForeColor = Color.Green; toolStripStatusLabel1.Text = "USB connecté"; statut = true; } private void usbHidPort1_OnSpecifiedDeviceRemoved(object sender, EventArgs e) { toolStripStatusLabel1.ForeColor = Color.Red; toolStripStatusLabel1.Text = "USB non connecté"; label2.Text = "--,-°C"; label3.Text = "--,-- Bar"; label5.Text = "--,-- m^3/h"; statut = false; } private void label1_Click(object sender, EventArgs e) { } private void toolStripStatusLabel1_Click(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void label6_Click(object sender, EventArgs e) { } } } |